Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 数组合并和Json编码_Javascript_Php_Jquery_Arrays_Json - Fatal编程技术网

Javascript 数组合并和Json编码

Javascript 数组合并和Json编码,javascript,php,jquery,arrays,json,Javascript,Php,Jquery,Arrays,Json,我生成的数组数量未知,如下所示: 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

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

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]]);
  }
}
$newot= array_merge_recursive($otfdata);
$myJsondata = json_encode($newot);
file_put_contents('json/myJson.json', $myJsondata);
[[{"John":{"games":"1","mint":"3.65","pct":"0"}},
{"Mary":{"games":"1","mint":"0.625","pct":"12"}},
...
{"Whatever":{"games":"1","mint":"0.325","pct":"4"}}]]
{"John":{"games":"1","mint":"3.65","pct":"0"},
"Mary":{"games":"1","mint":"0.625","pct":"12"},
...
"Whatever":{"games":"1","mint":"0.325","pct":"4"}}
生成的
myJson.json
文件如下所示:

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]]);
  }
}
$newot= array_merge_recursive($otfdata);
$myJsondata = json_encode($newot);
file_put_contents('json/myJson.json', $myJsondata);
[[{"John":{"games":"1","mint":"3.65","pct":"0"}},
{"Mary":{"games":"1","mint":"0.625","pct":"12"}},
...
{"Whatever":{"games":"1","mint":"0.325","pct":"4"}}]]
{"John":{"games":"1","mint":"3.65","pct":"0"},
"Mary":{"games":"1","mint":"0.625","pct":"12"},
...
"Whatever":{"games":"1","mint":"0.325","pct":"4"}}
我不知道如何管理这个文件。我是说,我想这样做:

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]]);
  }
}
$newot= array_merge_recursive($otfdata);
$myJsondata = json_encode($newot);
file_put_contents('json/myJson.json', $myJsondata);
[[{"John":{"games":"1","mint":"3.65","pct":"0"}},
{"Mary":{"games":"1","mint":"0.625","pct":"12"}},
...
{"Whatever":{"games":"1","mint":"0.325","pct":"4"}}]]
{"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中,我希望达到“mint”数据点“Mary”=>“0.625”

显然,这是行不通的

2) 在jQuery中,我希望基本相同:达到“mint”数据点“Mary”

我的问题:

A) 我认为我遇到的第一个问题是在合并不同阵列的过程中。我这样做会在开始和结束时生成
[[
,我认为这是不正确的

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

我需要的是:

1) 要以如下方式合并和编码阵列:

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]]);
  }
}
$newot= array_merge_recursive($otfdata);
$myJsondata = json_encode($newot);
file_put_contents('json/myJson.json', $myJsondata);
[[{"John":{"games":"1","mint":"3.65","pct":"0"}},
{"Mary":{"games":"1","mint":"0.625","pct":"12"}},
...
{"Whatever":{"games":"1","mint":"0.325","pct":"4"}}]]
{"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中的操作方式是否正确


谢谢

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

<?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);

array\u merge\u recursive-递归合并两个或多个数组array\u merge\u recursive是我正在使用的。数组键中有不同的名称,这就是为什么要得到
嵌套数组的原因,即数组检查示例。我不明白你的意思。你如何改变我构建数组的方式r我合并它们的方式?太好了!非常感谢。