简体   繁体   中英

How do I replace the value for key “program_id” in JSON Array?

I have read through a few answers and the solution seems simple enough but it's just not working.

The below produces "null" at the end of the NEW Array:

//Original Array from db
$outArray[] = '[{"id":"1","user_id":"1","program_id":"replace_1,replace_2,replace_3,replace_4,replace_5"}]';

//replacement key with value from string delimited by comma
$program_id= 'replace_1,replace_2,replace_3,replace_4,replace_5';
$string = $program_id; 
$str_arr = explode (",", $string);
$data['program_id'] = $str_arr[0];
$replacement_key_with_value = json_decode($data); //{"program_id":"replace_1"}

//replace 'program_id' value in Original Array
$arrayData = json_decode($outArray, true);
$newArrayData = array_replace($arrayData, $replacement_key_value);
$outArray[] = json_encode($newArrayData);

echo $outArray; //[{"id":"1","user_id":"1","program_id":"replace_1,replace_2,replace_3,replace_4,replace_5},"null"]

there are some errors at your code sample, but if I get you right, this code will check for the key program_id inside your JSON string with new one:

<?php
    //Original json string from db
    $jsonString= '[{"id":"1","user_id":"1","program_id":"replace_1,replace_2,replace_3,replace_4,replace_5"}]';
    $myNewProgramID = 'New Program ID Value';
    //replace 'program_id' value in Original Array
    $arrayData = json_decode($jsonString, true);
    if (count($arrayData)) {
        $newArr = [];
        foreach ($arrayData as $key => $item) {
            $newArr[$key] = $item;
            if (isset($newArr[$key]['program_id'])) {
                $newArr[$key]['program_id'] = $myNewProgramID;
            }
        }
        $arrayData = $newArr;
    }
    $outArray = json_encode($arrayData);
    echo $outArray; //[{"id":"1","user_id":"1","program_id":"New Program ID Value"}]
    exit;
?>

You should convert json to array and then modify the element that you want:

//Original Array from db
$outArray = '[{"id":"1","user_id":"1","program_id":"replace_1,replace_2,replace_3,replace_4,replace_5"}]';
$arrayData = json_decode($outArray, true);
var_dump($arrayData);
echo "<BR><BR>";

$program_id= 'replace_1,replace_2,replace_3,replace_4,replace_5';
$string = $program_id;
$str_arr = explode (",", $string);
$outArray = $arrayData;
$outArray[0]['program_id'] = $str_arr[0];
var_dump($outArray);

Running Your code leads to bunch of errors:

json_decode() expects parameter 1 to be string, array given

Undefined variable: replacement_key_value

array_replace(): Expected parameter 1 to be an array, null given

Array to string conversion

Assuming You have JSON string, the first thing You must do is to decode JSON. Then You can replace anything. eg:

<?php

$raw = '{"program_id": "p1,p2,p3,p4"}';

$decoded = json_decode($raw);

$decoded->program_id = str_replace("p2", "p5", $decoded->program_id);

$encoded = json_encode($decoded);

echo $encoded;

In example above we are searching for p2 and replacing it with p5.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM