dongzhi9574 2014-07-09 10:13
浏览 269
已采纳

将一个数组拆分为两个

I have an array of date like this

Array
(
    [0] => Array
        (
            [imageName] => WNgrRdqZ
            [alt] => alt text
        )

    [1] => Array
        (
            [imageName] => xoPS0udB
            [alt] => Alt
        )

)

And I'm wanted to format it so I get one array of imageNames, and one array of alt. I can do this simply with a foreach loop, but I'm curious if there's a build in PHP function I could use instead of rewriting something else. I've not found anything useful yet in the PHP manual.

EDIT: It could also be split into 1 array with 2 sub arrays, e.g.

Array
    (
        ['imageName'] => Array(..),
        ['alt'] => Array(...)
    )
  • 写回答

3条回答 默认 最新

  • duanpo2813 2014-07-09 10:17
    关注

    You can use array_column():

    $imageName = array_column($Array, 'imageName');
    $alt = array_column($Array, 'alt');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?