JSON数据到PHP中适当要求阵列格式 [英] json data into proper require array format in php

查看:139
本文介绍了JSON数据到PHP中适当要求阵列格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中我收到JSON数据后回声< pre>中; 和print_r的像这样 - (这是小数据,我变得非常大数据)

 阵列

    [0] =>排列
        (
            [ID] => 0
            [日期] => 2015年5月30日
            [总] => 8
        )    [1] =>排列
        (
            [ID] => 0
            [日期] => 2015年5月31日
            [总] => 4
        )    [2] =>排列
        (
            [ID] => 4863
            [日期] => 2015年5月30日
            [总] => 183
        )    [3] =>排列
        (
            [ID] => 4863
            [日期] => 2015年5月31日
            [总] => 140
        )    [4] =>排列
        (
            [ID] => 5126
            [日期] => 2015年5月30日
            [总] => 131292
        )

要求 -
我想在这个表格的形式来显示数据THID - >

  ID 2015年5月30日2015年5月31日 - 差异LT; BR />
0 8 4 -4所组成; BR />
4863 183 140 -43 LT; BR />
5126 131292< BR />


解决方案

由于这个数据看起来是保存在数据库中,我希望你可以肯定的是,该数据始终是从两个日期,通过这些命令(如在你的榜样阵列)。

如果是这样的话,我可能会做这样的:

  $阵列=阵列(阵列('ID'=大于0,'日期'=>2015年5月30日','总'=→8)
               阵列('ID'=大于0,'日期'=>2015年5月31日','总'=> 4)
               阵列('ID'=> 4863,日期= GT;'二○一五年五月三十〇日','总'=> 183)
               阵列('ID'=> 4863,日期= GT;2015年5月31日','总'=> 140),
               阵列('ID'=> 5126,日期= GT;'二○一五年五月三十〇日','总'=> 131292));$ newArray =阵列();
$日期=阵列();
的foreach($数组$输入){
  (!使用isset($ newArray [$入门['身份证']))如果$ newArray [$入门['身份证'] =阵列();
  如果(in_array($入门['日期'],$日期)!)array_push($日期,$入门['日期']);
  array_push($ newArray [$入门['身份证'],数组($入门['总'],$入门['日期']));
}回声ID&安培; NBSP; 。 $日期[0]。与& NBSP; 。 $日期[1]。与& NBSP; 。 差异化&安培; NBSP;< BR />中;
的foreach($ newArray为的$ id => $输入){
  回声$ ID。与& NBSP; 。 (($入门[0] [1] == $日期[0])$入门[0] [0]与& NBSP;$项[1] [0]:。&放大器; NBSP; 。$入门[0] [0])。与& NBSP; 。 ((计数($条目)== 2)$条目[1] [0] - $条目[0] [0]:)。与& NBSP;&所述峰; br>中;
}

In php I am getting json data and after echo "<pre>"; and print_r like this -(this is small data I am getting very big data)

Array
(
    [0] => Array
        (
            [id] => 0
            [date] => 2015-05-30
            [total] => 8
        )

    [1] => Array
        (
            [id] => 0
            [date] => 2015-05-31
            [total] => 4
        )

    [2] => Array
        (
            [id] => 4863
            [date] => 2015-05-30
            [total] => 183
        )

    [3] => Array
        (
            [id] => 4863
            [date] => 2015-05-31
            [total] => 140
        )

    [4] => Array
        (
            [id] => 5126
            [date] => 2015-05-30
            [total] => 131292
        )
)

Requirement - I want to display thid data in this table format ->

id  2015-05-30  2015-05-31  difference <br/> 
0       8           4           -4     <br/> 
4863    183        140          -43    <br/> 
5126    131292                         <br/>

解决方案

Since this data looks like it is saved in a database, I expect you can make sure, that the data is always from two dates, ordered by those (as in your example array).

If this is the case, I would probably do it like this:

$array = array(array('id' => 0, 'date' => '2015-05-30', 'total' => 8),
               array('id' => 0, 'date' => '2015-05-31', 'total' => 4),
               array('id' => 4863, 'date' => '2015-05-30', 'total' => 183),
               array('id' => 4863, 'date' => '2015-05-31', 'total' => 140),
               array('id' => 5126, 'date' => '2015-05-30', 'total' => 131292));

$newArray = array();
$dates = array();


foreach($array as $entry) {
  if(!isset($newArray[$entry['id']])) $newArray[$entry['id']] = array();
  if(!in_array($entry['date'], $dates)) array_push($dates, $entry['date']);
  array_push($newArray[$entry['id']], array($entry['total'], $entry['date']));
}

echo "id&nbsp;" . $dates[0] . "&nbsp;" . $dates[1] . "&nbsp;" . "difference&nbsp;<br/>";
foreach($newArray as $id => $entry) {
  echo $id . "&nbsp;" . (($entry[0][1]==$dates[0]) ? $entry[0][0] . "&nbsp;" . $entry[1][0] : "&nbsp;" . $entry[0][0]) .  "&nbsp;" . ((count($entry) == 2) ? $entry[1][0]-$entry[0][0] : "") ."&nbsp;<br>";
}

这篇关于JSON数据到PHP中适当要求阵列格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆