将json字符串转换为php对象并访问它的值

时间:2017-10-03 03:16:45

标签: php json

我有如下图所示的json数据,如何获得突出显示的值并更改它们?

Json Data

EDITED

我试图获得" furniture_id"使用下面的代码......但是失败了...我不知道我还能做些什么来获得价值

$test = '{"data":[{"owned_id":1,"furniture_id":1,"owned_name":"desk_123"},{"owned_id":2,"furniture_id":2,"owned_name":"chair_123"},{"owned_id":3,"furniture_id":4,"owned_name":"sofa_123"}]}';

    foreach ($test as $key => $value) {
        dd($furniture_id['id']);
    }

3 个答案:

答案 0 :(得分:0)

试试此代码

$test = '{"data":[{"owned_id":1,"furniture_id":1,"owned_name":"desk_123"},{"owned_id":2,"furniture_id":2,"owned_name":"chair_123"},{"owned_id":3,"furniture_id":4,"owned_name":"sofa_123"}]}';

$test = json_decode($test, true);

//if you want to change value of furniture_id in index 0 
$test['data'][0]['furniture_id'] = 111;
print_r($test); exit;

答案 1 :(得分:0)

$test = '{"data":[{"owned_id":1,"furniture_id":1,"owned_name":"desk_123"},{"owned_id":2,"furniture_id":2,"owned_name":"chair_123"},{"owned_id":3,"furniture_id":4,"owned_name":"sofa_123"}]}';
    $response=json_decode($test);

        foreach ($response->data as $key => $value) {
           echo $value->furniture_id;
echo "<br>";
        }

首先你必须使用json_encode方法解码json数据。然后,如果你打印json解码响应,那么你将获得标准对象的结果

stdClass Object
(
    [data] => Array
        (
            [0] => stdClass Object
                (
                    [owned_id] => 1
                    [furniture_id] => 1
                    [owned_name] => desk_123
                )

            [1] => stdClass Object
                (
                    [owned_id] => 2
                    [furniture_id] => 2
                    [owned_name] => chair_123
                )

            [2] => stdClass Object
                (
                    [owned_id] => 3
                    [furniture_id] => 4
                    [owned_name] => sofa_123
                )

        )

)

更新

$test = '{"data":[{"owned_id":1,"furniture_id":1,"owned_name":"desk_123"},{"owned_id":2,"furniture_id":2,"owned_name":"chair_123"},{"owned_id":3,"furniture_id":4,"owned_name":"sofa_123"}]}';
$response=json_decode($test);
echo "<pre>";
print_r($response);
$newArray=[];
    foreach ($response->data as $key => $value) {
    $row=[];
       $value->furniture_id= $value->furniture_id+1;
    }
    echo "<pre>";
    print_r($response->data);

现在输出

Array
(
    [0] => stdClass Object
        (
            [owned_id] => 1
            [furniture_id] => 2
            [owned_name] => desk_123
        )

    [1] => stdClass Object
        (
            [owned_id] => 2
            [furniture_id] => 3
            [owned_name] => chair_123
        )

    [2] => stdClass Object
        (
            [owned_id] => 3
            [furniture_id] => 5
            [owned_name] => sofa_123
        )

)

答案 2 :(得分:0)

$test = '{"data":[{"owned_id":1,"furniture_id":1,"owned_name":"desk_123"},{"owned_id":2,"furniture_id":2,"owned_name":"chair_123"},{"owned_id":3,"furniture_id":4,"owned_name":"sofa_123"}]}';

这个$test字符串本身是json string。由于json类似于javascript对象,因此我们无法在javascript中存储json。这就是我们放置的原因json在php中引用并将它们表示为字符串。

你可以做的是使用json_decode()将json字符串转换为php对象并在foreach循环中访问它们

foreach (json_decode($test)->data as $key) {

    echo $key->furniture_id;
}