Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-通过特定键从数组中获取值_Php_Multidimensional Array - Fatal编程技术网

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
是一个keyuse
array\u列