×
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

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

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

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

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

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.

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

2
Hi

$row holds a copy of current array item. Modifying it does not affect $fileContents. So better explicitly change the array :

CODE --> PHP ( fragment )

foreach ($fileContents as $nr => $row) {
    if (($row['meetdate']) == $meetdate) {
        $fileContents[$nr]['handout'] = $acronym; 
    }
} 

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 --> PHP ( fragment )

foreach ($fileContents as &$row) {
    if (($row['meetdate']) == $meetdate) {
        $row['handout'] = $acronym; 
    }
} 
Of course, Unsetting References helps to avoid future surprises.

Feherke.
feherke.github.io

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

(OP)
Thanks for your help.

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