【问题标题】:PHP: Create a number of Json arrays from a Json Array based on the group on one keyPHP:基于一个键上的组从一个Json数组创建多个Json数组
【发布时间】:2018-04-19 13:32:03
【问题描述】:

我有一个 JSON 数组,如下所示:-

[{
    "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",

}]

我想创建将包含所有类似contract_no 的键值对的数组数量。对于上面的示例,我想要输出如下:-

数组-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",

}]

数组-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。

有没有办法做到这一点?

【问题讨论】:

  • Is there a way to do this? 当然有。到目前为止,我没有看到你尝试过的任何东西。那么,到目前为止,您尝试过什么?
  • 我尝试了 if else。如果合约编号与数组的先前合约编号匹配,则推入一个新数组,否则循环将继续下一个。但问题是它只创建了一个不等于匹配对数的数组。
  • 只需检查该 json 数组中有多少对象。根据您的需要..

标签: php mysql arrays json


【解决方案1】:

将 JSON 转换为 php 数组

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

然后创建一个包含所有contract_no的临时(或非临时)数组,如果contract_no相同,则添加每个数组元素的内容

$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所有 元素都是数组,并且$arrays 元素的所有 元素都具有contract_no 索引。如果不是这种情况,您可以使用isset 检查索引是否存在。

如果你不想要关联数组,你可以使用

$result = array_values($temp_array)

所以$result 数组(或$temp_array)是存储在一个数组中的示例的 Array-I 和 Array-II。我认为这更容易。如果您仍然想要单独的数组(我不推荐),您可以执行以下操作:

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

编辑

即使这是完全相同的代码,试试这个(我添加了更多错误检查):

$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 添加到一个空数组中(即使之前的代码对我有用,就像我说的几乎一样)

这个例子可以在php沙箱here看到。

【讨论】:

  • 它有帮助 :) 非常感谢。
  • 我只是在这一点上很弱。我在这得到了一些额外的东西:- {“DCI-92700028”:{“order_no”:“1”,“contract_no”:“DCI-92700028”,“dc_in_date”:“2017-11-06”,“0 ": { "order_no": "1", "contract_no": "DCI-92700028", "dc_in_date": "2017-11-06", } }, "DCI-37500029": { "order_no": "G1049" , "contract_no": "DCI-37500029", "dc_in_date": "2017-11-06", } } 还有更多我想同时更新mysql记录。
  • @shivamgour 嗯,我不知道多余的东西是从哪里来的。也许您的 $json 没有按照您发布的方式格式化? Here is a link 到一个 php 沙箱,其中总结了我编写的代码并且工作正常对于 mysql 记录,我们需要 很多 更多关于您的数据库结构以及您正在尝试做什么的信息。
  • 感谢您对此进行调查。我得到 json 数组,如:- { "order_no": "1", "contract_no": "DCI-92700028", "dc_in_date": "2017-11-06", "0": { "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”:“1”,“contract_no”:“DCI-92700028”,“dc_in_date”:“2017-11-06”,} ] 休息一切对我都有好处。
  • 你能把你的代码贴在这里吗?我真的不明白这个错误。正如我已经指出的那样,代码对我有用(查看链接)。看起来您正在第一个 if 块中设置 $temp_array[$a['contract_no']]= $a;`。我也更新了我的解决方案,也许这对你有用。
【解决方案2】:

您可以使用 array_search 和 array_column 方法。

<?php

function recursive_array_search($needle,$haystack) {
    $return = null;
    foreach($haystack as $key=>$value) {
        $current_key=$key;
        if($needle===$value || (is_array($value) && recursive_array_search($needle,$value) !== null)) {
            $return[] = $current_key;
        }
    }
    return $return;
}

$json = '[{
    "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"
}]';
$array = json_decode($json, true);
echo '<pre>';
    print_r($array);
    echo '</pre>';
$columns = array_column($array,'contract_no');
foreach($columns as $column){
    echo 'Contact No:'.$column.'<br />';
    $found_key = recursive_array_search($column, $array);
    echo '<pre>';
    print_r($found_key);
    echo '</pre>';

}
?>

【讨论】:

    【解决方案3】:

    您可以在数组(array_map 和 array_filter)上使用函数以获得结果。

    首先,您需要找到合同编号列表(搜索的关键)。

    那么对于每个合约号,你需要构造一个匹配key的记录数组。

    <?php
    
    $json = <<< END
    [{
        "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"
    }]
    END;
    
    $a = json_decode($json,true);
    
    // list of $contract_no
    $contracts = array_unique( // remove duplicates values
        array_map( // applies the callback to the elements of $a
            function ($item) { // return the contract_no of each record of $a
                return $item['contract_no'];
            }, $a)
    );
    
    $result = [];
    foreach( $contracts as $contract_no ) {
        $result[] = array_filter($a, // filters records of $a  using a callback function
            function ($item) use ($contract_no) { // anonymous function inherit of $contract_no
                if ($item['contract_no'] === $contract_no) {
                    return $item; // return record of $a if the test is true
                }
            }
        );
    }
    
    var_dump($result);
    

    【讨论】:

      猜你喜欢
      • 2013-06-11
      • 2017-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-10
      • 2013-08-21
      相关资源
      最近更新 更多