Is there a function to extract a 'column' from an array in PHP?

前端 未结 14 1382
鱼传尺愫
鱼传尺愫 2020-11-21 07:33

I have an array of arrays, with the following structure :

array(array(\'page\' => \'page1\', \'name\' => \'pagename1\')
      array(\'page\' => \'pa         


        
14条回答
  •  抹茶落季
    2020-11-21 08:02

    I wanted to post here, even if this is an old question, because it is still very relevant and many developers do not use PHP >= 5.5

    Let's say you have an array like this:

    Array
    (
        [files] => Array
            (
                [name] => Array
                    (
                        [0] => file 1
                        [1] => file 2
                        [2] => file 3
                    )
    
                [size] => Array
                    (
                        [0] => 1
                        [1] => 2
                        [2] => 3
                    )
    
                [error] => Array
                    (
                        [0] => abc
                        [1] => def
                        [2] => ghi
                    )
    
            )
    
    )
    

    and the output you want is something like this:

    Array
    (
        [0] => Array
            (
                [0] => file 1
                [1] => 1
                [2] => abc
            )
    
        [1] => Array
            (
                [0] => file 2
                [1] => 2
                [2] => def
            )
    
        [2] => Array
            (
                [0] => file 3
                [1] => 3
                [2] => ghi
            )
    
    )
    

    You can simply use the array_map() method without a function name passed as the first parameter, like so:

    array_map(null, $a['files']['name'], $a['files']['size'], $a['files']['error']);
    

    Unfortunately you cannot map the keys if passing more than one array.

提交回复
热议问题