如何使用PHP按键值修改数组列表的数据结构

时间:2018-05-03 05:18:03

标签: php

我需要使用PHP根据某些键值修改json数组列表的数据结构。我在下面解释我的代码。

<?php
$output=array(
    array(
        "first_name"=>"robin",
        "last_name"=>"sahoo",
        "reg_no"=>12,
        "paper_code"=>"BA001"
    ),array(
        "first_name"=>"robin",
        "last_name"=>"sahoo",
        "reg_no"=>12,
        "paper_code"=>"BA002"
    ),array(
        "first_name"=>"Rama",
        "last_name"=>"Nayidu",
        "reg_no"=>13,
        "paper_code"=>"BA001"
    )
);
//echo json_encode($output);
$result=array();
foreach ($output as $key => $value) {
    if (count($result)==0) {
        $result[]=array(
            "name"=>$value["first_name"].' '.$value['last_name'],
            "reg_no"=>$value['reg_no'],
            "paper1"=>$value['paper_code'],
            "paper2"=>"",
            "paper3"=>"",
            "paper4"=>""
        );
    }
}

输入数组的输出如下所示。

// Output:
[
    {
        "first_name":"robin",
        "last_name":"sahoo",
        "reg_no":12,
        "paper_code":"BA001"
    },
    {
        "first_name":"robin",
        "last_name":"sahoo",
        "reg_no":12,
        "paper_code":"BA002"
    },
    {
        "first_name":"Rama",
        "last_name":"Nayidu",
        "reg_no":13,
        "paper_code":"BA001"
    }
];

以上是我的数组列表。在这里,我需要通过reg_no修改所有行值,这意味着如果有多行包含相同的reg_no,那么这些行将合并加入这两个名称,我的预期输出应如下所示。

预期产出:

[
    {
        'name':"robin sahoo",
        "reg_no":12,
        "paper1":"BA001",
        "paper2":"BA002",
        "paper3":"",
        "paper4":""
    },
    {
        'name':"Rama Nayidu",
        "reg_no":13,
        "paper1":"BA001",
        "paper2":"",
        "paper3":"",
        "paper4":""
    }
]

此处paper1,paper2,paper3,paper4将被串行选择,意味着假设同一reg_no=12第一行paper_code= BA001然后它将是paper1=BA001,第二行paper_code = BA002则为{{} 1}}等等。这里我使用PHP来映射这个数组。

3 个答案:

答案 0 :(得分:0)

本准则可以帮助您

<?php
$output=array(array("first_name"=>"robin","last_name"=>"sahoo","reg_no"=>12,"paper_code"=>"BA001"),array("first_name"=>"robin","last_name"=>"sahoo","reg_no"=>12,"paper_code"=>"BA002"),array("first_name"=>"Rama","last_name"=>"Nayidu","reg_no"=>13,"paper_code"=>"BA001"));
//echo json_encode($output);

$result=array();
foreach ($output as $key => $value) {
    if (count($result)==0) {
        $output[$key]=array("name"=>$value["first_name"].' '.$value['last_name'],"reg_no"=>$value['reg_no'],"paper1"=>$value['paper_code'],"paper2"=>"","paper3"=>"","paper4"=>"");
    }
}echo "<pre>";print_r($output);
?>

答案 1 :(得分:0)

你可以试试这个

$result = []; // Initialize result array
foreach ($output as $key => $value) {
    $name = $value['first_name'] . ' ' . $value['last_name'];

    // check if same name already has entry, create one if not
    if (!array_key_exists($name, $result)) {
        $result[$name] = array(
            'name' => $name,
            'reg_no' => $value['reg_no'],
            'paper1' => '',
            'paper2' => '',
            'paper3' => '',
            'paper4' => ''
            );
    }

    // count array elements with value, then set paper number and value
    $paper = 'paper' . (count(array_filter($result[$name])) - 1);
    $result[$name][$paper] = $value['paper_code'];
}

$result = array_values($result); // reindex result array
$result = json_encode($result);  // Encode to json format
print_r($result); // print result

这假设每个first_name

last_namereg_no始终相同

答案 2 :(得分:0)

请尝试以下方式,让我知道..

$output=array(array("first_name"=>"robin","last_name"=>"sahoo","reg_no"=>12,"paper_code"=>"BA001"),array("first_name"=>"robin","last_name"=>"sahoo","reg_no"=>12,"paper_code"=>"BA002"),array("first_name"=>"Rama","last_name"=>"Nayidu","reg_no"=>13,"paper_code"=>"BA001"));
//echo json_encode($output);
$result=array();
$temp=array();
if(!empty($output)){
    foreach ($output as $key => $value) {
        if(isset($temp[$value['reg_no']])){
            if(empty($temp[$value['reg_no']]["paper1"]) || $temp[$value['reg_no']]["paper1"] == ""){
                $temp[$value['reg_no']]["paper1"] = $value['paper_code'];
            }else if(empty($temp[$value['reg_no']]["paper2"]) || $temp[$value['reg_no']]["paper2"] == ""){
                $temp[$value['reg_no']]["paper2"] = $value['paper_code'];
            }else if(empty($temp[$value['reg_no']]["paper3"]) || $temp[$value['reg_no']]["paper3"] == ""){
                $temp[$value['reg_no']]["paper3"] = $value['paper_code'];
            }else if(empty($temp[$value['reg_no']]["paper4"]) || $temp[$value['reg_no']]["paper4"] == ""){
                $temp[$value['reg_no']]["paper4"] = $value['paper_code'];
            }
        }else{
            $temp[$value['reg_no']] = array("name"=>$value["first_name"].' '.$value['last_name'],"reg_no"=>$value['reg_no'],"paper1"=>$value['paper_code'],"paper2"=>"","paper3"=>"","paper4"=>"");
        }
    } 
}              
if(!empty($temp)){
    foreach ($temp as $key => $value) {
        $result[] = $value;
    } 
}