duanqiongchong0354 2017-05-29 13:27
浏览 46

如何在PHP中回显多维比较数组

I have two array:

$one = Array
(
    [0] => stdClass Object
        (
            [id] => 2
            [name] => Southampton
        )

    [1] => stdClass Object
        (
            [id] => 4
            [name] => Manchester United F.C
        )

)

and

$two = Array
(
    [0] => stdClass Object
        (
            [number] => 25555
            [slice_1] => 4
            [slice_2] => 4
            [slice_3] => 2
            [slice_4] => 4
            [status] => Published
        )

)

I want to output, if array $two->slice_1 same as array $one->id than output $one->name.

For example:

$two[0]->slice_1 (4) compare to $one[0]->id (4) will result in Manchester United F.C.

Because the array $one and $two will be have more than one array. Please don't answer this:

if($two[0]->slice_1 == $one[1]->id){echo $one[0]->name;}

I'm stucking here and can't think out some way. Please help. Thanks in advance

  • 写回答

1条回答 默认 最新

  • drjltlm156790 2017-05-29 13:42
    关注

    avoiding loops inside loops first create temp reference array then loop though second array outputting the result

    foreach($one as $id=>$data){
       $temp[$data['id']] = $data['name'];
    }
    foreach($two as $secondary_id=>$secondary_data){
      echo isset($temp[$secondary_data['slice_1']]) ? $temp[$secondary_data['slice_1']] : ''; 
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码