×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Add an new JSON array into an existing JSON array
2

Add an new JSON array into an existing JSON array

Add an new JSON array into an existing JSON array

(OP)
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:

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

RE: Add an new JSON array into an existing JSON array

Hi

Would coloring the variable names help ?

CODE --> PHP ( fragment )

$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; 

Regarding the actual array operation, you probably want this one :

CODE --> PHP ( fragment )

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

Feherke.
feherke.github.io

RE: Add an new JSON array into an existing JSON array

(OP)
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 --> Result

{"meetdate":"2019-08-15","acronym":"mi","type":"_ho_3c","0":{"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.

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

RE: Add an new JSON array into an existing JSON array

Hi

Quote (waubain)

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 --> command line

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

master # 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);

master # php waubain.php 

master # 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"}]

master # 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

RE: Add an new JSON array into an existing JSON array

(OP)
Thank you for your help.

RE: Add an new JSON array into an existing JSON array

(OP)
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.

RE: Add an new JSON array into an existing JSON array

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
https://www.doschke.name

RE: Add an new JSON array into an existing JSON array

(OP)
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close