2

I generate an unknown number of arrays like this:

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]]);
  }
}

I need this arrays to be merged before encoding the result to a .jsonfile.

I do it like this:

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

The myJson.jsonfile that results is something like this:

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

And I don't know how to manage this file. I mean, I would like to do something like this:

1) In php, I want to reach the "mint" data point of "Mary" => "0.625".

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

Obviously, this does not work.

2) In jQuery, I want basically the same: to reach the "mint" data point of "Mary".

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

My problems:

A) I think the first problem I have is during the merging of the different arrays. The way I do it generates [[ at the beginning and at the end, and I think this is incorrect.

B) Apart from that, is it correct the way I do it in jQuery?

What I need:

1) To merge and encode the arrays in a way it looks like this:

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

I think this would solve problem A.

2) To confirm wether the way I do it in jQuery is correct or not.

Thanks

4
  • array_merge_recursive — Merge two or more arrays recursively Commented Jun 14, 2017 at 9:13
  • array_merge_recursive is what I am using. Commented Jun 14, 2017 at 9:14
  • you are having the different names in the array keys that's why you are getting the nested arrays i.e, array of arrays php.net/manual/en/function.array-merge-recursive.php check the example Commented Jun 14, 2017 at 9:26
  • Sundar I don't understand what you mean. How would you change the way I build the arrays or the way I merge them? Commented Jun 14, 2017 at 9:35

1 Answer 1

1

You can generate the array on a expected format like below

<?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);
Sign up to request clarification or add additional context in comments.

1 Comment

Great! Appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.