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条)

报告相同问题?

悬赏问题

  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法