0

I am newbee in php and trying to get json in array and wanna change key in that json below is my code :

   $json = json_decode(file_get_contents('all_json_files/jobs.json'), true); 
    foreach ($json as $key=>$row){
      foreach ( $row as $key=>$row){
         foreach ( $row as $key=>$row){
            foreach ($row as $key=>$row){
               if(strcmp($key,"security_block")==0)
                {
                       foreach ($row as $k=>$r){
                       if(strcmp($k,"job_payload_hash")==0)
                       {
                         $row[$k]['job_payload_hash']=$base64String;
                         print_r($row);
                       }
                }
              }
            }
         }
       }
    }
    print_r($json);

Issue is print_r($row); is updating properly but print_r($json); does not print the updated string .

3
  • You know you can use json_decode() right? Commented Jan 9, 2015 at 11:32
  • json_encode and json_decode are your friends, those loops are crazy. Commented Jan 9, 2015 at 11:33
  • yes i know that things is i just want to chnage value of 'job_payload_hash' in my json @Varedis i-CONICA Commented Jan 9, 2015 at 11:43

4 Answers 4

3

If the key could appear anywhere, the answer is pretty simple:

function update_hash(&$item, $key, $base64String)
{
    if ($key == "job_payload_hash") {
        $item = $base64String;
    }
}

array_walk_recursive($json, 'update_hash', 'something');

Update

The structure is something different that previously assumed; while the above will work, probably the below is a more direct approach:

foreach (array_keys($json['jobs']) as $jobId) {
    $json['jobs'][$jobId]['job']['security_block']['job_payload_hash'] = 'something';
}
Sign up to request clarification or add additional context in comments.

Comments

0

You use $key & $row variable in multiple time. for this reason, value of $key is changed each time, so parent loop does not work..

You can use recursive function lik answer of @Ja͢ck .

Comments

-1

this is because you have to save not in variables you defined after => in a foreach. You have to store this in format:

$json[0][0] ... = $base64String;

OR

You have to add a new array like $result = array() before you write the foreach and then store it in $result.

2 Comments

Not sure why this was down voted, it's an answer in the right direction.
Because it tries to fix awful code. The OP is clearly going in the wrong direction. So a good answer would suggest a better way of coding the above, not help fix someone's crap code.
-1

Decode the JSON string using json_decode(), edit your resulting array, then use json_encode(); to return the array to a JSON encoded string.

Also, use array_key_exists() rather than comparing array key strings.

$array = json_decode($json);

if(array_key_exists("job_payload_hash", $array){
  $array["job_payload_hash"] = base64encode($var);
}

$json = json_encode($array);

2 Comments

This only observes a single dimension; the data that needs to be changed seems to be five levels deep.
You're right, array_walk_recursive is better for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.