douqiaosu0280 2015-03-08 14:41
浏览 70

如何在JSON中找到2个不同变量的最小值,并用该值减去其他元素

I need to find the smallest value of 2 variables in JSON object of particular key and subtract all keys with that value. Kindly lookinto following JSON Data

$print = '{"Table":[
{"Column":2,"Length":1,"Number":"A4","Row":12,"zindex":0}, 
{"Column":2,"Length":1,"Number":"A3","Row":11,"zindex":0},
{"Column":2,"Length":1,"Number":"A2","Row":9,"zindex":1},
{"Column":2,"Length":1,"Number":"A1","Row":8,"zindex":1},  
{"Column":3,"Length":1,"Number":"B4","Row":12,"zindex":0},
{"Column":3,"Length":1,"Number":"B3","Row":11,"zindex":0},
{"Column":3,"Length":1,"Number":"B2","Row":9,"zindex":0},
{"Column":3,"Length":1,"Number":"B1","Row":8,"zindex":0},
{"Column":4,"Length":1,"Number":"C4","Row":12,"zindex":0},
{"Column":4,"Length":1,"Number":"C3","Row":11,"zindex":0},  
{"Column":4,"Length":1,"Number":"C2","Row":9,"zindex":0},
{"Column":4,"Length":1,"Number":"C1","Row":8,"zindex":0}],"ResponseStatus":200,"Message":null}';

I want above JSON Data as given below.

$print = '{"Table":[
    {"Column":2,"Length":1,"Number":"A4","Row":1,"zindex":0}, 
    {"Column":2,"Length":1,"Number":"A3","Row":0,"zindex":0},
    {"Column":2,"Length":1,"Number":"A2","Row":1,"zindex":1},
    {"Column":2,"Length":1,"Number":"A1","Row":0,"zindex":1},  
    {"Column":3,"Length":1,"Number":"B4","Row":1,"zindex":0},
    {"Column":3,"Length":1,"Number":"B3","Row":0,"zindex":0},
    {"Column":3,"Length":1,"Number":"B2","Row":1,"zindex":1},
    {"Column":3,"Length":1,"Number":"B1","Row":0,"zindex":1},
    {"Column":4,"Length":1,"Number":"C4","Row":1,"zindex":0},
    {"Column":4,"Length":1,"Number":"C3","Row":0,"zindex":0},  
    {"Column":4,"Length":1,"Number":"C2","Row":1,"zindex":1},
    {"Column":4,"Length":1,"Number":"C1","Row":0,,"zindex":1}],"ResponseStatus":200,"Message":null}';

In the above JSON data kindly look into Row and zindex. zindex has two values "0 and 1", here I want php code to find smallest value of zindex 0 separately and subtract remaining values of zindex 0 alone and same with zindex 1.

I have a PHP code that finds minimum value of row and subtract remaining rows with that value. But I dont know how to do it separately for different zindexs.

$rows = array();
$print = json_decode($print, true); // decode
foreach($print['Table'] as $val) { $rows[] = $val['Row']; }
$least = min($rows); // get least // $least = min(array_column($print['Table'], 'Row'));
foreach($print['Table'] as &$val) {
    $val['Row'] -= $least; // make subtractions
}
$print = json_encode($print); // re encode

Kindly help me to solve this problem in PHP.

  • 写回答

1条回答 默认 最新

  • douxun4924 2015-03-08 15:07
    关注

    Try to replace the first foreach by:

    foreach($print['Table'] as $val) {
        if (!isset($rows[$val['zindex']])) {
            $rows[$val['zindex']] = array();
        }
        $rows[$val['zindex']][] = $val['Row'];
    }
    

    Instead of having a 1-dimensional array with values, the array is 2-dimensional, having all Row values for each value of zindex.

    Then, $least should be an array as well:

    $least = array();
    foreach ($rows as $zindex = $row) {
        $least[$zindex] = min($row);
    }
    

    That will calculate the minimum value for each value of zindex separately.

    Finally, the last foreach should discriminate for zindex as well:

    foreach ($print['Table'] as &$val) {
        $val['Row'] -= $least[$val['zindex']];
    }
    

    Success!

    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教