简体   繁体   中英

array_merge before json encode in PHP

$myArray = array();

for($i=0;i<2;$i++){
..
.. //get the content logic here

assign it to array
$myArray["item"]["content"] = $item_content;
}
echo json_encode($myArray);

The above code produced this result:

在此处输入图片说明

Which has an error because I didn't merge them.

I tried to merge like this:

$finalJson = array_merge($finalJson, $myArray); 

but the output of echo $finalJson is one object instead of 3.

Update:

Your real problem is down to your use of array_merge on an associative array. The behaviour of array_merge is to reassign duplicate keys if they are associative (cf the docs ):

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

And because $myArray is clearly using strings as keys, the values in $finalJson are being reassigned. What you need to do is either create unique keys on each iteration, or simply push the values of $myArray onto a numerically indexed $finalJson array (like I showed in my original answer below)


The problem is simply this:

$myArray["item"]["content"] = $item_content;

it's inside the loop, each time reassigning the value of $myArray["item"]["content"] , and not adding it to the array. I suspect that what you wanted to do is add this at the bottom of your loop (for each new value of $myArray ):

$finalJson[] = $myArray;

Then, on the next iteration, $myArray will be reassigned, and its new value will be appended to the $finalJson variable.

i have a tricky problem.

What i do. I generate from the System Tables of Databases (DEV ,Test Prod setup) information for Tables, Vies trigger … with PHP and compare the results ti see teh differences with JavaScript.

Also I have a Documentation DB for additional business information which was installed only once on TEST DB.

Therfore I need to connect all four environments to get the data.

I use if ($flag === 'i')

 for information DB and

elseif ($flag === 's')  

for system db's.

Result is $row_array and $info_array which must be combined and sendet back to Javascript.

$json_response =   array_merge($rinfo, $rsql );    

echo json_encode($json_response, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);    

I try this merge in a different positions in the program.

First time in
elseif ($flag === 'i') {

result json:

    [
    {
        "0": "ACT",……….
    } ][ ]        

second time after   }  //ifelse return also 2 arrays

    [{"0":"ACT","1":"Tabelle Akten","2":"hh","3":null,"4":null,"5":"UCC","6":"Y","7":"Reload Data in Test","8":"y","9":"delete all older tha","10":"n","11":" ","12":"y","13":" ","14":"o","15":"y","16":"o","17":"y","18":"y","19":"Diese tabelle speichert die Acten Verweise","20":"Gert Dorn","21":1570254359,"TDESCRIPTION":"n","TTYPE":" ","TREC_ESTIM":"y","TREC_GROWTH":" ","TDOMAIN":"o","TREL_TYPE":"y","TREL_RULES":"o","THOUSEKEEPING":"y","THOUSE_RULES":"y","TCID":"Diese tabelle speichert die Acten Verweise","TCID_RULES":"Gert Dorn","TUSE_UCC":1570254359,"TUSE_DWH":"","TUSE_ODS":"","TUSE_CWF":"","TUSE_IWF":"","TUSE_OWF":"","TUSE_DEP_MANAGER":"","TENTITY_DESCRIPTION":"","TOWNER":""

,"TTIMESTAMP":""**}][{**"0":"ACT","1":"DB2INST1"

,"2":"USERSPACE1","3":null,"4":"2018-11-21 16:43:20.066567","5":"2018-12-07 10:12:10.255759","6":null,"7":"2020-03-26","8":"2018-11-21 16:43:20.343258","9":3,"NAME":"ACT","CREATOR":"DB2INST1","TBSPACE":"USERSPACE1","REMARKS":"","CTIME":"2018-11-21 16:43:20.066567","STATS_TIME":"2018-12-07 10:12:10.255759","STATISTICS_PROFILE":"","LASTUSED":"2020-03-26","ALTER_TIME":"2018-11-21 16:43:20.343258","COLCOUNT":3}]    

The program code and result you can download at

http://dmdg.io/dmdg.zip

Hope you can help Kind regards gert

Did you consider using array_push?

array_push is always preferred than myArray[] = $value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM