Elamparithi.P Elamparithi.P - 3 months ago 10
JSON Question

How to add to json array in .json file with php?

I have this json array in json file

[["a","b","c","d"],["3","2","25","25"],["4","48","20","20"],["3","1","22","22"],["5","4","31","31"],["5","3","33","33"],["5","6","43","43"],["5","8","45","45"],["5","5","42","42"],["5","11","37","37"],["5","7","40","40"],["5","10","36","36"],["1","35","40","40"],["2","22","38","38"],["2","23","35","35"],["1","31","34","34"],["2","19","43","43"],["2","17","35","35"],["3","14","20","20"],["3","15","17","17"],["3","13","16","16"],["2","20","36","36"],["1","39","28","28"],["3","26","20","20"],["4","46","20","20"],["1","36","32","32"],["1","37","21","21"],["3","9","5","5"],["3","12","23","23"],["3","18","21","21"],["3","16","22","22"],["3","21","15","15"],["4","34","6","6"],["1","33","29","29"],["1","32","24","24"],["1","30","30","30"],["3","24","27","27"],["3","27","23","23"],["3","25","23","23"],["3","28","6","6"],["3","29","18","18"],["4","47","19","19"],["4","51","6","6"],["4","50","4","4"],["1","49","26","26"],["1","40","41","41"],["1","41","43","43"],["4","43","2","2"],["4","44","1","1"],["1","38","44","44"],["1","45","37","37"],["1","42","39","39"],["4","61","3","3"],["4","60","7","7"],["4","57","10","10"],["4","58","14","14"],["4","56","8","8"],["4","54","12","12"],["4","59","9","9"],["4","55","10","10"],["4","53","11","11"],["4","52","13","13"]]


I need to add another array to the above json array,

that is

[["e"],["45"
],["37"],["40"],["57"],["61"],["85"],["96"],["79"],["71"],["77"],["68"],["77"],["73"],["65"],["64"],
["85"],["65"],["37"],["31"],["30"],["68"],["49"],["37"],["37"],["60"],["39"],["21"],["42"],["39"],["40"
],["28"],["22"],["51"],["43"],["55"],["48"],["42"],["42"],["22"],["34"],["35"],["22"],["18"],["47"],
["78"],["85"],["104"],["100"],["92"],["71"],["74"],["140"],["222"],["248"],["276"],["229"],["259"],["233"
],["248"],["250"],["268"]]


I need the output like this in Array

[["a,b,c,d,e"],["3","2","25","25","45"]]


I can't able to do it in array_push,array_merge ,Can any help me ?

Thanks in advance ...

Answer

You should have get the json string in a php variable. then:

$array1 = json_decode($json_string1, true);// not needed if already you have an array
$array2 = json_decode($json_string2, true);
$merged = array();

foreach( $array1 as $key => $value ){
    if(array_key_exists($key, $array2)){
         $merged[]  =  array_merge($value, $array2[$key]);
    }else{
         $merged[]  =  $value ;
    }
}