How can I get the array of a single type from a multi dimensional array (without a loop)

后端 未结 2 2002
伪装坚强ぢ
伪装坚强ぢ 2021-01-18 12:56

I have the following array $foo

array(10) {
[0] => array(4) {

[\"merchantId\"] => string(5) \"12e21\"
[\"programId\"] => string(27) \"ffffd3333\"
[\"         


        
2条回答
  •  感动是毒
    2021-01-18 13:13

    As an alternative to array_column()

    $transpose = call_user_func_array(
        'array_map',
        array_merge(
            array(NULL),
            $data
        )
    );
    $result = $transpose[array_search("programId", array_keys($data[0]))];
    var_dump($result);
    

    Which can be done as a one-liner in PHP5.5

    $result = call_user_func_array('array_map',array_merge(array(NULL),$data))[array_search("programId", array_keys($data[0]))];
    var_dump($result);
    

    I'll confess, it's not exactly intuitive or readable though

提交回复
热议问题