PHP:基于一个键上的组从一个Json数组创建许多Json数组 [英] PHP: Create a number of Json arrays from a Json Array based on the group on one key

查看:123
本文介绍了PHP:基于一个键上的组从一个Json数组创建许多Json数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下的JSON数组:-

I have an JSON Array like following:-

[{
    "order_no": "1",
    "contract_no": "DCI-92700028",
    "dc_in_date": "2017-11-06",

}, {
    "order_no": "1",
    "contract_no": "DCI-92700028",
    "dc_in_date": "2017-11-06",

}, {
    "order_no": "G1049",
    "contract_no": "DCI-37500029",
    "dc_in_date": "2017-11-06",

}]

我要创建Number of arrays,它将由相似的contract_no的所有键值对组成.对于上面的示例,我想要输出Like:-

I want to create Number of arrays which will consist all key-value pair of similar contract_no. For above Example, I want output Like:-

数组-I

[{
    "order_no": "1",
    "contract_no": "DCI-92700028",
    "dc_in_date": "2017-11-06",

}, {
    "order_no": "1",
    "contract_no": "DCI-92700028",
    "dc_in_date": "2017-11-06",

}]

Array- II

Array- II

[ {
    "order_no": "G1049",
    "contract_no": "DCI-37500029",
    "dc_in_date": "2017-11-06",

}]

基本思想是我有Mysql表,该表有两列.一个是"contract_no",另一个是"order_details".我想将order_details的值更新为合同编号匹配的Array-I和Array-II.

Basic Idea is I have Mysql table which has two column. One is "contract_no" other one is "order_details". I want to update value of order_details as Array-I and Array-II where contract number match.

有没有办法做到这一点?

Is there a way to do this?

推荐答案

将JSON转换为php数组

Convert the JSON to a php array

/* use json_decode(..., true) for creating an array, not an object of StdClass */
$array = json_decode($json, true);

然后创建一个包含所有contract_no的临时(或非临时)数组,并在contract_no相同的情况下添加每个数组元素的内容

Then create a temproary (or not temprary) array which contains all the contract_no and add the contents of each array element if the contract_no is the same

$temp_array = array();
foreach($array as $a){
    if(!isset($temp_array[$a['contract_no']])){
        $temp_array[$a['contract_no']] = array($a);
    }
    else{
        array_push($temp_array[$a['contract_no']], $a);
    }
}

此代码假定$array all 元素是数组,并且$array s元素的 all 元素具有contract_no索引.如果不是这种情况,则可以使用isset检查索引是否存在.

This code assumes that all elements of the $array are arrays and all elements of the $arrays elements have the contract_no index. If this is not the case you can just use isset to check if the indices exist.

如果您不希望使用关联数组,则可以使用

If you do not want the associative array you can just use

$result = array_values($temp_array)

因此,$result数组(或$temp_array)是示例中的Array-I和Array-II,存储在一个数组中.我认为这比较容易.如果您仍然希望拥有单独的数组(我不建议这样做),则可以执行以下操作:

So the $result array (or the $temp_array) are the Array-I and Array-II of your example stored in one array. I think this is easier. If you still want to have separate arrays (which i would not recommend) you can do something like this:

$array_counter = 1;
foreach($result as $r){
    $array_name = "array".$array_counter;
    $$array_name = $r;
    $array_counter++;
}

修改

即使这是完全相同的代码,也请尝试执行此操作(我添加了更多错误检查功能):

Even though this is the exact same code try this (I added some more error checking):

$temp_array = array();
foreach($array as $a){
    if(isset($a['contract_no']) && !empty($a['contract_no'])){
        if(!isset($temp_array[$a['contract_no']]) || !is_array($temp_array[$a['contract_no']])){
            $temp_array[$a['contract_no']] = array();
        }

        array_push($temp_array[$a['contract_no']], $a);
    }
}

似乎您正在设置$temp_array[$a['contract_no']] = $a,这将导致您发布的格式错误.更改后的代码行应确保将$a添加到空数组中(即使之前的代码对我有用,就像我实际上说的一样)

It seems like you are setting the $temp_array[$a['contract_no']] = $a, this would cause the wrong format you are posting. The changed lines of code should make sure that the $a is added to an empty array (even though the code before worked for me and is like I said practicly the same)

可以在php沙箱此处中看到该示例.

This example can be seen in php sandbox here.

这篇关于PHP:基于一个键上的组从一个Json数组创建许多Json数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆