Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.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数组并将其分成3个不同的数组_Php_Arrays_Json_Multidimensional Array - Fatal编程技术网

Php 解码多维Json数组并将其分成3个不同的数组

Php 解码多维Json数组并将其分成3个不同的数组,php,arrays,json,multidimensional-array,Php,Arrays,Json,Multidimensional Array,我正在从一个Web服务接收下面的json负载,在解码whlist并保留相同的数组键后,我尝试使用循环将其分成3个不同的数组: { "id": 5705376, "title": "Satellite Installation", "created_at": "2017-09-07T14:02:19.000Z", "updated_at": "2017-09-07T14:02:19.000Z", "customer_id": 3126803, "way_points": [{ "id":

我正在从一个Web服务接收下面的json负载,在解码whlist并保留相同的数组键后,我尝试使用循环将其分成3个不同的数组:

{
"id": 5705376,
"title": "Satellite Installation",
"created_at": "2017-09-07T14:02:19.000Z",
"updated_at": "2017-09-07T14:02:19.000Z",
"customer_id": 3126803,
"way_points": [{
    "id": 7405587,
    "lat": -26.0578251,
    "lng": 28.02189520000002,
    "task_id": 5705376,
    "done": false,
    "allow_editing_inventory": true,
    "customer_contact_ids": [3126803],
    "customer": {
        "original_phone_number": null,
        "last_open_at": null,
        "last_order_at": null,
        "uploaded_profile_image": {
            "url": "/images/avatar.png"
        },
        "original_lat": null,
        "original_lng": null,
        "district": null,
        "house_number": null,
        "street": null
    },
    "full_address": "8 Anslow Ln, Bryanston, Sandton, 2191, South Africa"
}],
"customer": {
    "id": 3126803,
    "name": "Lance",
    "address": "8 Anslow Ln, Bryanston, Sandton, 2191, South Africa",
    "lat": -26.0578251,
    "lng": 28.0218952,
    "created_at": "2017-08-29T10:00:32.360Z",
    "updated_at": "2017-09-07T14:02:19.860Z",
    "phone": null,
    "merchant_id": 11221,
    "allow_login": false,
    "stripe_id": null,
    "original_phone_number": null,
    "last_open_at": null,
    "last_order_at": null,
    "uploaded_profile_image": {
        "url": "/images/avatar.png"
    },
    "original_lat": null,
    "original_lng": null,
    "house_number": null,
    "street": null
},
"late": false,
"external_id": "5705376",
"uuid": "28095e33-b30c-4e35-98d2-aae6ad428f66",
"dispatcher_id": null,
"team_ids": [12422],
"ready_to_execute": true,
"tip_driver_enabled": false,
"automatically_assigned": false,
"task_inventories": null,
"task_notes": null
}
对于“way_points”键中的值,我想在解码后生成一个名为waypoint的数组,同时忽略其中的customers数组。在此过程中,我还想将“id”键名称更改为“waypoint_id”

对于“customers”键(在主数组中)中的值,我想在分配“url”值的同时,在解码后,生成一个名为“waypoint”的数组 “上载的配置文件图像”键。在此过程中,我还想将“id”密钥名称更改为“customer\u id”

上面提到的数组之外的任何元素(即主数组中未分配给数组的元素),我希望它们形成一个名为“orders”的数组


我该怎么做呢。我是循环新手

首先,您需要将json编码的字符串解码为可用格式,可以是
数组
或对象。您可以找到
json\u decode
的所有可能选项

现在开始循环。为了保持简单,您可以直接将需要的三个数组初始化为空数组。然后,在循环中,只需检查键值并选择要将数据附加到的数组

$waypoints = array();
$customers = array();
$orders = array();

$count = 0;
foreach($inputArray as $key => $element) {
    /* using if statements */
    if($key === 'way_points') {
        $waypoints[] = $element;
    }
    elseif($key === 'customer') {
        $customers[] = $element;
    }
    else {
        $orders[$count][$key] = $element;
    }

    /* using a switch/case */
    switch($key) {
        case 'way_points':
            $waypoints[] = $element;
            break;
        case 'customer':
            $customers[] = $element;
            break;
        default:
            $orders[$count][$key] = $element;
            break;
    }

    $count++;
}
此时,您应该拥有所需的所有数据,但我们仍然没有更改密钥。您可以保持原样,毕竟,
$waypoints
中的
id
键代表
航路点id
是不言自明的。但是,如果您确实需要更改密钥,有几种方法可以做到这一点。您可以在新形成的
$waypoints
数组上循环,并修改此新循环中的关键点

$waypoints = change_keys($waypoints);

function change_keys($arr) {
    return array_map(function($waypoint) {
        return array(
            'waypoint_id' => $waypoint['id'],
            'lat' => $element['lat'],
            /* remaining fields */
        );
    }, $arr);
}
或者,您可以减少步数,并在初始
foreach
循环中执行此操作

$waypoints = array();
$customers = array();
$orders = array();

$count = 0;
foreach($inputArray as $key => $element) {
    if($key === 'way_points') {
        $waypoints[] = array_map(function($element) {
            return array(
                'waypoint_id' => $element['id'],
                'lat' => $element['lat'],
                /* remaining fields */
            );
        }, $arr);
    }

    /* ... */
}
您可以在
数组\u map
上找到更多信息



另一方面,正如@jeroen在评论中提到的,您只需使用
json\u decode
,就可以得到一个可用的关联数组。例如,要在航路点上循环,您只需编写
foreach($key=>myArray[waypoints]as$waypoint)
,首先您需要将json编码的字符串解码为可用格式,可以是
array
或对象。您可以找到
json\u decode
的所有可能选项

现在开始循环。为了保持简单,您可以直接将需要的三个数组初始化为空数组。然后,在循环中,只需检查键值并选择要将数据附加到的数组

$waypoints = array();
$customers = array();
$orders = array();

$count = 0;
foreach($inputArray as $key => $element) {
    /* using if statements */
    if($key === 'way_points') {
        $waypoints[] = $element;
    }
    elseif($key === 'customer') {
        $customers[] = $element;
    }
    else {
        $orders[$count][$key] = $element;
    }

    /* using a switch/case */
    switch($key) {
        case 'way_points':
            $waypoints[] = $element;
            break;
        case 'customer':
            $customers[] = $element;
            break;
        default:
            $orders[$count][$key] = $element;
            break;
    }

    $count++;
}
此时,您应该拥有所需的所有数据,但我们仍然没有更改密钥。您可以保持原样,毕竟,
$waypoints
中的
id
键代表
航路点id
是不言自明的。但是,如果您确实需要更改密钥,有几种方法可以做到这一点。您可以在新形成的
$waypoints
数组上循环,并修改此新循环中的关键点

$waypoints = change_keys($waypoints);

function change_keys($arr) {
    return array_map(function($waypoint) {
        return array(
            'waypoint_id' => $waypoint['id'],
            'lat' => $element['lat'],
            /* remaining fields */
        );
    }, $arr);
}
或者,您可以减少步数,并在初始
foreach
循环中执行此操作

$waypoints = array();
$customers = array();
$orders = array();

$count = 0;
foreach($inputArray as $key => $element) {
    if($key === 'way_points') {
        $waypoints[] = array_map(function($element) {
            return array(
                'waypoint_id' => $element['id'],
                'lat' => $element['lat'],
                /* remaining fields */
            );
        }, $arr);
    }

    /* ... */
}
您可以在
数组\u map
上找到更多信息



另一方面,正如@jeroen在评论中提到的,您只需使用
json\u decode
,就可以得到一个可用的关联数组。例如,要在航路点上循环,只需编写
foreach($key=>myArray[waypoints]as$waypoint)

为什么需要不同的变量?只需使用
json\u decode()
解码json,您就拥有了所需的一切。您共享的不是有效的json,您确定这是正确的json吗?很抱歉json无效,我编辑了我的回答为什么需要不同的变量?只需使用
json\u decode()
解码json,您就拥有了所需的一切。您共享的不是有效的json,您确定这是正确的json吗?很抱歉json无效,我编辑了我的答案