Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add an new JSON array into an existing JSON array 2

Status
Not open for further replies.

waubain

Technical User
Dec 13, 2011
200
US
I want to add a new JSON array to an existing JSON array that have the same keys. I have searched, but cannot find a solution to match.

My current array
Code:
[{"meetdate":"2019-06-05","acronym":"dm","type":"_ho_6c"},
 {"meetdate":"2019-07-22","acronym":"htn","type":"_ho_6c"}
]

The goal array
Code:
[{"meetdate":"2019-06-05","acronym":"dm","type":"_ho_6c"},
 {"meetdate":"2019-07-22","acronym":"htn","type":"_ho_6c"},
 {"meetdate":"2019-08-15","acronym":"mi","type":"_ho_3c"}
]

I have tried array_merge and array + array, but none result in what I am trying to accomplish. Here is my code:

PHP:
$ho_meetdate = '2019-08-15';
$ho_acronym = 'mi';
$ho_type = '_ho_3c';

$arr2 = array();
                $arr['meetdate'] = $ho_meetdate1;
                $arr['acronym']  = $ho_acronym1;
                $arr['type']     = $ho_type1;  
                
                //get existing array and decode  
                $fileContents = file_get_contents('../../editor/textfiles/handout_arrayTest.txt');
                $arr1 = json_decode($fileContents, true);
                
                //attempts at adding an array existing array
                $result = array_merge($arr1, $arr2);
                $result = $arr1 + $arr2;
                
                //Save the array back to a the text file.
                $ho_encoded = json_encode($result);
                file_put_contents("textfiles/handout_arrayTest.txt", $ho_encoded);
 
Hi

Would coloring the variable names help ?
Code:
[highlight #f66][navy]$arr2[/navy][/highlight] [teal]=[/teal] [b]array[/b][teal]();[/teal]
[highlight #6f6][navy]$arr[/navy][/highlight][teal][[/teal][i][green]'meetdate'[/green][/i][teal]] =[/teal] [navy]$ho_meetdate1[/navy][teal];[/teal]
[highlight #6f6][navy]$arr[/navy][/highlight][teal][[/teal][i][green]'acronym'[/green][/i][teal]]  =[/teal] [navy]$ho_acronym1[/navy][teal];[/teal]
[highlight #6f6][navy]$arr[/navy][/highlight][teal][[/teal][i][green]'type'[/green][/i][teal]]     =[/teal] [navy]$ho_type1[/navy][teal];[/teal]  

[gray]//get existing array and decode  [/gray]
[navy]$fileContents[/navy] [teal]=[/teal] [COLOR=orange]file_get_contents[/color][teal]([/teal][i][green]'../../editor/textfiles/handout_arrayTest.txt'[/green][/i][teal]);[/teal]
[highlight #66f][navy]$arr1[/navy][/highlight] [teal]=[/teal] [COLOR=orange]json_decode[/color][teal]([/teal][navy]$fileContents[/navy][teal],[/teal] [b]true[/b][teal]);[/teal]

[gray]//attempts at adding an array existing array[/gray]
[navy]$result[/navy] [teal]=[/teal] [COLOR=orange]array_merge[/color][teal]([/teal][highlight #66f][navy]$arr1[/navy][/highlight][teal],[/teal] [highlight #f66][navy]$arr2[/navy][/highlight][teal]);[/teal]
[navy]$result[/navy] [teal]=[/teal] [highlight #66f][navy]$arr1[/navy][/highlight] [teal]+[/teal] [highlight #f66][navy]$arr2[/navy][/highlight][teal];[/teal]

Regarding the actual array operation, you probably want this one :
Code:
[navy]$result[/navy] [teal]=[/teal] [COLOR=orange]array_merge[/color][teal]([/teal][navy]$arr1[/navy][teal],[/teal] [highlight][b]array[/b][teal]([/teal][/highlight][navy]$arr2[/navy][highlight][teal])[/teal][/highlight][teal]);[/teal]

Feherke.
feherke.github.io
 
The typo's on the array elements were my bad, too much cutting and pasting. When I tried you array operation ($result = array_merge($arr1, array($arr2));) , I did not quite get the results I wanted.

Code:
{"meetdate":"2019-08-15","acronym":"mi","type":"_ho_3c",[highlight #73D216]"0":[/highlight]{"meetdate":"2019-08-15","acronym":"mi","type":"_ho_3c"}}

I finally found an example that I could massage to work. I probably did not explain the problem appropriately.

PHP:
$ho_meetdate1 = "2019-08-15";
$ho_acronym1 = "mi";
$ho_type1 = "_ho_3c";


$ho_meetdate2 = "2019-09-15";
$ho_acronym2 = "htn";
$ho_type2 = "_ho_6c";

//create original json array
$arr1 = array();
                $arr1["meetdate"] = $ho_meetdate1;
                $arr1["acronym"]  = $ho_acronym1;
                $arr1["type"]     = $ho_type1;  

                file_put_contents("../../editor/textfiles/handout_arrayPush.txt", json_encode($arr1)); 

$arr2 = array();
                $arr2["meetdate"] = $ho_meetdate2;
                $arr2["acronym"]  = $ho_acronym2;
                $arr2["type"]     = $ho_type2;   
//get existing array  
                $arr1 = file_get_contents('../../editor/textfiles/handout_arrayPush.txt');
//merge 2 arrays
                  $result = array();                  
                  $result[] = json_decode($arr1, true);
                  $result[] = json_decode(json_encode($arr2), true);
                  
//Save the array back to a the text file.
                  file_put_contents("../../editor/textfiles/handout_arrayPush.txt", json_encode($result));
 
Hi

waubain said:
When I tried you array operation ($result = array_merge($arr1, array($arr2));) , I did not quite get the results I wanted.
Now this is weird. Here is what I got :
Code:
[blue]master #[/blue] cat handout_arrayPush.txt 
[{"meetdate":"2019-06-05","acronym":"dm","type":"_ho_6c"},
 {"meetdate":"2019-07-22","acronym":"htn","type":"_ho_6c"}
]

[blue]master #[/blue] cat waubain.php 
<?php

$arr2 = array();
$arr2['meetdate'] = '2019-08-15';
$arr2['acronym']  = 'mi';
$arr2['type']     = '_ho_3c';

$fileContents = file_get_contents('handout_arrayPush.txt');
$arr1 = json_decode($fileContents, true);

$result = array_merge($arr1, array($arr2));

$ho_encoded = json_encode($result);
file_put_contents('handout_arrayPush.txt', $ho_encoded);

[blue]master #[/blue] php waubain.php 

[blue]master #[/blue] cat handout_arrayPush.txt 
[{"meetdate":"2019-06-05","acronym":"dm","type":"_ho_6c"},{"meetdate":"2019-07-22","acronym":"htn","type":"_ho_6c"},{"meetdate":"2019-08-15","acronym":"mi","type":"_ho_3c"}]

[blue]master #[/blue] php -v
PHP 7.2.17-1+ubuntu18.04.1+deb.sury.org+3 (cli) (built: Apr 10 2019 10:50:57) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.17-1+ubuntu18.04.1+deb.sury.org+3, Copyright (c) 1999-2018, by Zend Technologies
    with Xdebug v2.7.1, Copyright (c) 2002-2019, by Derick Rethans

Anyway, glad you solved it.


Feherke.
feherke.github.io
 
FYI, as a follow-up.

The example that I found and used worked when merging 2 arrays. When I added a third and every array after that, the code added an additional [ bracket at the beginning of the array and a ] bracket at the end of the newly added array.

I retried feherke's example and it worked perfectly. I believe, in my original attempt, I may have had a extra closing curly bracket which affected how the array was added.

Again thanks.
 
Are you still using the line $result = $arr1 + $arr2? arry_merge already merges two arrays into one $result array. And you should be able to repeat that as many times as you like. But then don't also add the arrays together, I'd say that's causing what you don't want. Without even testing.

Besides, you can always extend an array by assigning to a non named element, which means "append at the end as a new element":

Code:
$arr1[]=$arr2

Since you want $arr2 as a new row in $arr1 and rows in $arr1 consist of an array each, that's wanting to add $arr2 as new element and this code does exactly that. Untested, but in this case you neither need + nor array_merge() to add a new array as a new element of the old. Notice, this does not overwrite the whole array $arr1 and replaces it with $arr2, notice the brackets at the end, this tells PHP, add a new element at the end of the array.

Bye, Olaf.

Olaf Doschke Software Engineering
 
Olaf,
I am not using the line $result = $arr1 + $arr2, since I could not get it to work. I switched to what feherke recommended: array_merge().

I tried your solution and it worked. Thanks for the bracket explanation, many examples do not explain what each part does.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top