dongzai2952 2013-10-05 00:49
浏览 109
已采纳

php循环数组返回总是第一个元素

easy question but i can't find a solution.

I have an array:

array
   0 => 
     array
       1 => string '25' (length=2)
   1 => 
     array
       1 => string '27' (length=2)

And i need to get:

    array
  0 => string '25' (length=2)
  1 => string '27' (length=2)

Yea if course i could do:

foreach($result as $each) {
    $resultIds[]=$each[1];
}

But i am pretty sure i saw this week somewhere a function for it something like...

array_something('current',$result);

What will loop over the array as foreach and return the first element so the result will be the same as the foreach solution. But i can't find it or remember it.

*What is the name of the function ? *

  • 写回答

2条回答 默认 最新

  • dongyi6845 2013-10-05 00:54
    关注

    You can use array_map or array_walk

    example of array_map

    <?php
    function first($n)
    {
        return $n[1];
    }
    
    $arr = array(
        array(1, 2, 4),
        array(1, 2, 3,),
    );
    var_export($arr);
    // call internal function
    $arr = array_map('current', $arr);
    var_export($arr);
    
    // call user function
    $arr = array_map('first', $arr);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部