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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert a key:value pair into an array based on another key:value pair 2

Status
Not open for further replies.

waubain

Technical User
Dec 13, 2011
200
US
I am trying to insert a key:value pair into an array based on another key:value pair. The json array is in a text file. I am not getting any errors, but nothing is inserted. After insertion it should look like the first array. The key and new value comes from a form.

Here is the array meetinfo_arrayTest.txt

Code:
[{"meetdate":"2019-03-23","topic":"Hypertension", "handout":"htn"},
 {"meetdate":"2019-04-18","topic":"TBA"},
 {"meetdate":"2019-05-02","topic":"TBA"}]

Here is my code.
PHP:
        //retrieve values from form
	$meetdate = $form->getValue('meetdate');                
        $acronym = $form->getValue('ho_acronym');

        //Retrieve the data from our text file.
        $fileContents = file_get_contents('../editor/textfiles/meetinfo_arrayTest.txt');

        //Convert the JSON string back into an array.
        $infodecoded = json_decode($fileContents, true);
         
        foreach($fileContents as $row) {
        	if (($row['meetdate']) == $meetdate) {
                	$row['handout'] = $acronym; 
                }}
                    
        //Save the JSON string to a text file.
        $infoencoded = json_encode($infodecoded);
        file_put_contents("../editor/textfiles/meetinfo_arrayTest.txt", $infoencoded);
Any help would be appreciated.
 
Hi

$row holds a copy of current array item. Modifying it does not affect $fileContents. So better explicitly change the array :
Code:
[b]foreach[/b] [teal]([/teal][navy]$fileContents[/navy] [b]as[/b] [highlight][navy]$nr[/navy] [teal]=>[/teal][/highlight] [navy]$row[/navy][teal]) {[/teal]
    [b]if[/b] [teal](([/teal][navy]$row[/navy][teal][[/teal][i][green]'meetdate'[/green][/i][teal]]) ==[/teal] [navy]$meetdate[/navy][teal]) {[/teal]
        [highlight][navy]$fileContents[/navy][teal][[/teal][navy]$nr[/navy][teal]][/teal][/highlight][teal][[/teal][i][green]'handout'[/green][/i][teal]] =[/teal] [navy]$acronym[/navy][teal];[/teal] 
    [teal]}[/teal]
[teal]}[/teal]

Alternatively you can change $row into reference, but that is not recommended practice as later reuse of $row after the loop will still alter the last referred item :
Code:
[b]foreach[/b] [teal]([/teal][navy]$fileContents[/navy] [b]as[/b] [highlight pink][teal]&[/teal][/highlight][navy]$row[/navy][teal]) {[/teal]
    [b]if[/b] [teal](([/teal][navy]$row[/navy][teal][[/teal][i][green]'meetdate'[/green][/i][teal]]) ==[/teal] [navy]$meetdate[/navy][teal]) {[/teal]
        [navy]$row[/navy][teal][[/teal][i][green]'handout'[/green][/i][teal]] =[/teal] [navy]$acronym[/navy][teal];[/teal] 
    [teal]}[/teal]
[teal]}[/teal]
Of course, Unsetting References helps to avoid future surprises.

Feherke.
feherke.github.io
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top