dongzai2952 2013-10-05 08: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 08: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条)

报告相同问题?