Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用php将json值存储到数组中?_Php_Arrays_Json - Fatal编程技术网

如何使用php将json值存储到数组中?

如何使用php将json值存储到数组中?,php,arrays,json,Php,Arrays,Json,我需要将所有来自json url的“total_value”和“car_id”的值放入一个数组 Json: 我的Php: <?php $getJson = file_get_contents("http://url.com/control.json",false); $j = json_decode($getJson); 类似这样的东西: $result = []; foreach ($j as $item) { array_push($result, [ '

我需要将所有来自json url的“total_value”和“car_id”的值放入一个数组

Json:

我的Php:

<?php

$getJson = file_get_contents("http://url.com/control.json",false);

$j = json_decode($getJson);
类似这样的东西:

$result = [];
foreach ($j as $item) {
    array_push($result, [
        'total_value' => $item['total_value'],
        'car_id' => $item['car_id']
    ]);
}
大概是这样的:

$result = [];
foreach ($j as $item) {
    array_push($result, [
        'total_value' => $item['total_value'],
        'car_id' => $item['car_id']
    ]);
}

您的json格式不正确,您缺少一个
}

您可以循环您的
$j
,并获得类似
$item->control->total_value
的值。 然后将要查找的2个值添加到数组中,并将该数组添加到
$result

$j = json_decode($data);
$result = [];
foreach ($j as $item) {
    array_push($result,[
        'total_value' => $item->control->total_value,
        'car_id' => $item->control->car_id
    ]);
}

您的json格式不正确,您缺少一个
}

您可以循环您的
$j
,并获得类似
$item->control->total_value
的值。 然后将要查找的2个值添加到数组中,并将该数组添加到
$result

$j = json_decode($data);
$result = [];
foreach ($j as $item) {
    array_push($result,[
        'total_value' => $item->control->total_value,
        'car_id' => $item->control->car_id
    ]);
}

您也可以通过使用和删除不需要的项来实现这一点,这是一个较短的代码量,并且不需要您创建临时变量,也不需要从对象开始,然后以数组结束

array_walk($obj, function(&$v, $k) {
    unset($v->control->color);
});
例如(修复损坏的json):

<?php
$json = '[{
        "control": {
            "color": "blue",
            "total_value": 21.5,
            "car_id": 421118
        }
    },
    {
        "control": {
            "color": "green",
            "total_value": 25,
            "car_id": 421119
        }
    },
    {
        "control": {
            "color": "red",
            "total_value": 18,
            "car_id": 421519
        }
    }
]';

$obj = json_decode($json);

array_walk($obj, function(&$v, $k) {
    unset($v->control->color);
});

print_r($obj);
Array
(
    [0] => stdClass Object
        (
            [control] => stdClass Object
                (
                    [total_value] => 21.5
                    [car_id] => 421118
                )

        )

    [1] => stdClass Object
        (
            [control] => stdClass Object
                (
                    [total_value] => 25
                    [car_id] => 421119
                )

        )

    [2] => stdClass Object
        (
            [control] => stdClass Object
                (
                    [total_value] => 18
                    [car_id] => 421519
                )

        )

)

您也可以通过使用和删除不需要的项来实现这一点,这是一个较短的代码量,并且不需要您创建临时变量,也不需要从对象开始,最后以数组结束

array_walk($obj, function(&$v, $k) {
    unset($v->control->color);
});
例如(修复损坏的json):

<?php
$json = '[{
        "control": {
            "color": "blue",
            "total_value": 21.5,
            "car_id": 421118
        }
    },
    {
        "control": {
            "color": "green",
            "total_value": 25,
            "car_id": 421119
        }
    },
    {
        "control": {
            "color": "red",
            "total_value": 18,
            "car_id": 421519
        }
    }
]';

$obj = json_decode($json);

array_walk($obj, function(&$v, $k) {
    unset($v->control->color);
});

print_r($obj);
Array
(
    [0] => stdClass Object
        (
            [control] => stdClass Object
                (
                    [total_value] => 21.5
                    [car_id] => 421118
                )

        )

    [1] => stdClass Object
        (
            [control] => stdClass Object
                (
                    [total_value] => 25
                    [car_id] => 421119
                )

        )

    [2] => stdClass Object
        (
            [control] => stdClass Object
                (
                    [total_value] => 18
                    [car_id] => 421519
                )

        )

)

是否解码($getJson,true)(将true添加为第二个值)帮助?可能,但是?如何将值存储在不同的数组中?您期望的输出是什么?一个具有“total_value”的数组和另一个具有“car_id”的数组执行
$j=json_解码($getJson,true)(将true添加为第二个值)帮助?可能,但是?如何将值存储在不同的数组中?您期望的输出是什么?一个具有“total_value”的数组和另一个具有“car_id”的数组