dongou6632 2013-03-11 20:49
浏览 297
已采纳

如果键名不存在,array_column会返回什么?

According to https://wiki.php.net/rfc/array_column array_column is slated to be added to PHP soon. But I having trouble understanding the RFC. What will be returned if a named key doesn't exist?

Example:

$arr = array(
    array(
        'firstname' => 'Bob',
        'lastname'  => 'Tomato'
    ),
    array(
        'firstname' => 'Larry',
        'lastname'  => 'Cucumber'
    )
);

$middlenames = array_column($arr, 'middlename');
  • 写回答

2条回答 默认 最新

  • dongtiran7769 2013-04-05 01:32
    关注

    Introduction

    For you to understand the RFC you need to understand the problem first and the reason it was introduced.

    Your Array

    $arr = array(
            array(
                    'firstname' => 'Bob',    <--
                    'lastname' => 'Tomato'     |    <--
            ),                                 |      |
            array(                             |      |
                    'firstname' => 'Larry',  <--      |
                    'lastname' => 'Cucumber'        <-|
            )
    );
    

    Getting Column

    To get Bob & Larry or Tomato and Cucumber you have use more than one line of code examples are :

    $colums = array();
    foreach ( array_map(null, $arr[0], $arr[1]) as $value ) {
        $colums[] = $value;
    }
    print_r($colums);
    

    Output

    Array
    (
        [0] => Array
            (
                [0] => Bob
                [1] => Larry
            )
    
        [1] => Array
            (
                [0] => Tomato
                [1] => Cucumber
            )
    
    )
    

    Dynamic Version

    The code above would only work if you know number of elements another creative way would be

    $colums = array();
    array_unshift($arr, null);
    foreach (call_user_func_array("array_map", $arr) as $value ) {
        $colums[] = $value;
    }
    

    Live Test

    Or Better Sill use MultipleIterator

    $mi = new MultipleIterator(MultipleIterator::MIT_NEED_ALL);
    foreach ( $arr as $v ) {
        $mi->attachIterator(new ArrayIterator($v));
    }
    
    $colums = array();
    foreach ( $mi as $v ) {
        $colums[] = $v;
    }
    print_r($colums);
    

    Live Test

    Key Name

    If you need to get the key name here is another method

    $colums = array_reduce($arr, function ($a, $b) {
        foreach ( $b as $k => $v ) {
            $a[$k][] = $v;
        }
        return $a;
    });
    

    Live Test

    Back to array_column

    array_column intends simply the process and getting all columns with first name would be as simple as the following:

     print_r(array_column($arr,"lastname"));
                                   ^
                                   |
                                   +--- This get value with key "lastname"
    

    Live Test

    More Complex Senerio

    Imagine you want your array to have this output

    Array
    (
        [Bob] => Tomato
        [Larry] => Cucumber
    )
    

    Use Old methods you can have

    $colums = array();
    array_unshift($arr, null);
    foreach (call_user_func_array("array_map", $arr) as $value ) {
        $key = array_shift($value);
        $colums[$key] = current($value);
    }
    print_r($colums);
    

    Live Test

    Now you can see i had to use array_shift and current to get first 2 element .. as your array grows this can become complex but array_column would simplify this

    print_r(array_column($arr,"lastname","firstname"));
                                  ^           ^
                                  |           |
                                 Value       Key    (I wonder why position is backwards)
    

    Output

    Array
    (
        [Bob] => Tomato
        [Larry] => Cucumber
    )
    

    Finally Back to your Question

    What will be returned if a named key doesn't exist?

    Empty array ... From your example

     print_r(array_column($arr,"middlename"));
                                    ^
                                    |
                            it would try to check if any of your array has key middle man
    

    It returns

    Array        <------- Otherwise returns empty array 
    (
    )
    

    Conclusion

    I used so may different examples using loop , array_map , array_reduce and MultipleIterator to explain what array_column is trying to achieve.

    As you can see array_column is much more simplified but i would advice you play with the examples in the RFC a little and this would allow you understand it better if you still don't understand it, PHP is a flexible language you can always implement your own version

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c