dongzhang7382 2018-05-03 05:18
浏览 30
已采纳

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

I need to modify the data structure of json array list as per some key value using PHP. I am explaining my code below.

<?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"=>""
        );
    }
}

The output of the input array is given below.

// 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"
    }
];

The above is my array list. Here I need to modify the all row value by reg_no means if there are multiple rows including same reg_no then those will merge with joining the both name and my expected output should like below.

expected output:

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

Here paper1,paper2,paper3,paper4 will be selected serially means suppose same reg_no=12 has first row paper_code= BA001 then it will be paper1=BA001 and second row paper_code=BA002 then it will be paper2=BA002 and so on. Here I am using PHP to map this array.

  • 写回答

3条回答 默认 最新

  • douhong6187 2018-05-03 05:42
    关注

    Try the following, Let me know..

    $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;
        } 
    }     
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?