如何通过此数组中的特定键获取所有值?
Array (
[0] => Array ( [0] => Key1 [1] => Value1 )
[1] => Array ( [0] => Key1 [1] => Value2 )
[2] => Array ( [0] => Key2 [1] => Value12 )
)
exp。:使用Key1获取这些值:Value1,Value2
已经尝试过:
$values = array_column($param, 'Key1');
print_r($values);
//empty array
答案 0 :(得分:0)
您可以使用B
array_reduce
这将导致:
$arr = array (
array ( 'Key1', 'Value1' ),
array ( 'Key1', 'Value2' ),
array ( 'Key2', 'Value12' ),
);
$search = "Key1"; /* Update this to use */
$result = array_reduce($arr, function( $c, $v ) use ( $search ) {
if ( $v[0] == $search ) $c[] = $v[1];
return $c;
}, array());
答案 1 :(得分:0)
您可以尝试这样的事情:
<?php
$yourArray = Array(
0 => Array ( 0 => 'Key1', 1 => 'Value1' ),
1 => Array ( 0 => 'Key1', 1 => 'Value2' ),
2 => Array ( 0 => 'Key2', 1 => 'Value12' )
);
$desired_text = 'key1'; //your desired matching key
$values = array(); //variable to store captured data
foreach($yourArray as $innerarray)
{
//check if the first index has your desired text
if(strtolower($innerarray[0]) == $desired_text ) //converting to lower case and matching
{
$values[] = "{$innerarray[1]}"; //then grab the value
}
}
if(isset($values))
print_r($values); //show the values you've grabbed
?>
输出结果为:Array ( [0] => Value1 [1] => Value2 )