在数组中解码json,在json中编辑数组和编码 - PHP

时间:2022-10-17 09:19:51

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

我是php的新手,并试图在数组中获取json,并想在下面的json中更改密钥是我的代码:

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

问题是print_r($ row);正在更新,但print_r($ json);不会打印更新的字符串。

4 个解决方案

#1


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';
}

#2


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..

您可以多次使用$ key和$ row变量。因此,$ key的值每次都会更改,因此父循环不起作用..

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

你可以使用@Ja͢ck的递归函数。

#3


-1  

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

这是因为你不得在foreach中保存=>之后定义的变量。您必须以格式存储:

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

在编写foreach之前,必须添加一个新的数组,如$ result = array(),然后将其存储在$ result中。

#4


-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.

使用json_decode()解码JSON字符串,编辑生成的数组,然后使用json_encode();将数组返回到JSON编码的字符串。

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

此外,使用array_key_exists()而不是比较数组键字符串。

$array = json_decode($json);

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

$json = json_encode($array);

#1


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';
}

#2


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..

您可以多次使用$ key和$ row变量。因此,$ key的值每次都会更改,因此父循环不起作用..

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

你可以使用@Ja͢ck的递归函数。

#3


-1  

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

这是因为你不得在foreach中保存=>之后定义的变量。您必须以格式存储:

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

在编写foreach之前,必须添加一个新的数组,如$ result = array(),然后将其存储在$ result中。

#4


-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.

使用json_decode()解码JSON字符串,编辑生成的数组,然后使用json_encode();将数组返回到JSON编码的字符串。

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

此外,使用array_key_exists()而不是比较数组键字符串。

$array = json_decode($json);

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

$json = json_encode($array);