PHP-通过特定键从数组中获取值
如何通过此数组中的特定键获取所有值PHP-通过特定键从数组中获取值,php,multidimensional-array,Php,Multidimensional Array,如何通过此数组中的特定键获取所有值 Array ( [0] => Array ( [0] => Key1 [1] => Value1 ) [1] => Array ( [0] => Key1 [1] => Value2 ) [2] => Array ( [0] => Key2 [1] => Value12 ) ) exp.:使用键1获取以下值:Value1,Value2 已尝试: $values = array_column($pa
Array (
[0] => Array ( [0] => Key1 [1] => Value1 )
[1] => Array ( [0] => Key1 [1] => Value2 )
[2] => Array ( [0] => Key2 [1] => Value12 )
)
exp.:使用键1获取以下值:Value1,Value2
已尝试:
$values = array_column($param, 'Key1');
print_r($values);
//empty array
您可以使用
array\u 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());
这将导致:
Array
(
[0] => Value1
[1] => Value2
)
医生:你可以试试这样的方法:
<?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
?>
这看起来像是家庭作业。键1甚至不是键,它是一个值0
是一个keyusearray\u列