doumi4676 2018-03-16 17:41
浏览 45

JSON解码器2获取变量

how can i get this 2 variables? The $ingredients must get real price from $price, it must get name and price for example TV is amount 3 and price is 20, so it's 60. Here is code.

$ingredients = [
           ['name' => 'TV',                    'amount' => 3],
           ['name' => 'LAPTOP',                   'amount' => 2],
           ['name' => 'HAREM',                'amount' => 2],
           ['name' => 'OIL', 'amount' => 1],
           ['name' => 'Windows',                  'amount' => 1],
           ['name' => 'Something',                  'amount' => 1]
       ];

$prices = [
           'TV'                    => 20,
           'LAPTOP'                   => 20,
           'HAREM'                => 25,
           'OIL' => 20,
           'Windows'                  => 25,
           'Something'                  => 35
       ];
  • 写回答

1条回答 默认 最新

  • douciwang6819 2018-03-16 18:10
    关注

    With array_column you get all the 'name' from the array.
    This 'name' array can then be searched with array_search to get the key where 'item' is.
    Array_search returns false if not found, keep that in mind. 'false' == 0 in PHP.

    $name = array_column($ingredients,"name");
    Foreach($prices as $item => $price){
        $find = array_search($item, $name);
        If($find !== false) $res[$item] = $price * $ingredients[$find]['amount'];
    }
    
    Var_dump($res); 
    

    https://3v4l.org/oHCCN

    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能