【问题标题】:Arrays merging and Json encoding数组合并和 Json 编码
【发布时间】:2017-06-14 09:06:12
【问题描述】:

我生成未知数量的数组,如下所示:

for ($t=0;$t<2;$t++) {
  for ($xx=0;$xx<$totf[$t];$xx++) {
    $otdata[$t][$xx] = array("".$otname[$t][$xx]."" =>
        ['games'=> $otg[$t][$xx], 
        'mint'=> $otqmint[$t][$xx], 
        'pct'=>$otpct[$t][$xx]]);
  }
}

在将结果编码为.jsonfile 之前,我需要合并这些数组。

我是这样做的:

$newot= array_merge_recursive($otfdata);
$myJsondata = json_encode($newot);
file_put_contents('json/myJson.json', $myJsondata);

生成的myJson.jsonfile 是这样的:

[[{"John":{"games":"1","mint":"3.65","pct":"0"}},
{"Mary":{"games":"1","mint":"0.625","pct":"12"}},
...
{"Whatever":{"games":"1","mint":"0.325","pct":"4"}}]]

而且我不知道如何管理这个文件。我的意思是,我想做这样的事情:

1)在php中,我想达到“Mary”=>“0.625”的“mint”数据点。

$myfile=file_get_contents("json/myJson.json");
$mydata = json_decode($myfile,true);
$myResult=$mydata["Mary"]["mint"];

显然,这不起作用。

2) 在 jQuery 中,我想要的基本相同:到达“Mary”的“mint”数据点。

  var gdatafile = "json/myJson.json";
  $.getJSON(gdatafile, function (gjson) {
    var myResult=gjson["Mary"].mint;
    $('#resultContainer').text(myResult);
  });

我的问题:

A) 我认为我遇到的第一个问题是在合并不同数组的过程中。我这样做的方式在开头和结尾生成[[,我认为这是不正确的。

B) 除此之外,我在 jQuery 中的做法是否正确?

我需要什么:

1) 以如下方式合并和编码数组:

{"John":{"games":"1","mint":"3.65","pct":"0"},
"Mary":{"games":"1","mint":"0.625","pct":"12"},
...
"Whatever":{"games":"1","mint":"0.325","pct":"4"}}

我认为这可以解决问题 A。

2) 确认我在 jQuery 中的做法是否正确。

谢谢

【问题讨论】:

  • array_merge_recursive — 递归合并两个或多个数组
  • array_merge_recursive 是我正在使用的。
  • 您在数组键中有不同的名称,这就是为什么您得到 nested arrays 即数组数组 php.net/manual/en/function.array-merge-recursive.php 检查示例
  • Sundar 我不明白你的意思。您将如何改变我构建数组或合并它们的方式?

标签: javascript php jquery arrays json


【解决方案1】:

您可以按如下所示的预期格式生成数组

<?php

$data = array();

for ($t=0;$t<2;$t++) {
  for ($xx=0;$xx<$totf[$t];$xx++) {

    //create the temporary array
    $temp = array(
        'games'=> $otg[$t][$xx], 
        'mint'=> $otqmint[$t][$xx], 
        'pct'=>$otpct[$t][$xx]]
    );

    //identify the array key
    $key = $otname[$t][$xx];

    //check the key is exist or not
    if(isset($data[$key])) {

        //take the existing value as a backup
        $exone = $data[$key]; 
        //overwrite the array
        $data[$key] = array();

        //check the exsiting array is multidimension or not
        if(isset($exone[0])) {
            foreach ($exone as $one) {
                //push the array to the respective key
                array_push($data[$key], $one);
            }
        }
        else {
            //if single dimension then make it multi dimension
            array_push($data[$key], $exone);            
        }

        //push the latest array to the same key
        array_push($data[$key], $temp);

    }//create the new array for the key
    else {          
        $data[$key] = $temp;
    }

    /*$otdata[$t][$xx] = array("".$otname[$t][$xx]."" =>
        ['games'=> $otg[$t][$xx], 
        'mint'=> $otqmint[$t][$xx], 
        'pct'=>$otpct[$t][$xx]]);
    }
*/}

//print the json data
echo json_encode($data);

【讨论】:

  • 太棒了!欣赏它。
猜你喜欢
  • 1970-01-01
  • 2018-04-14
  • 2016-02-09
  • 1970-01-01
  • 2019-08-30
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多