其他分享
首页 > 其他分享> > 创建一个二维数组将该数组处理成以下格式

创建一个二维数组将该数组处理成以下格式

作者:互联网

  1. 创建一个二维数组将该数组处理成以下格式(5分)    

      

(原图)

 

 (效果图)

$subject = [
    ['name' => '语文', 'label' => '2'],
    ['name' => '数学', 'label' => '0'],
    ['name' => '化学', 'label' => '0'],
    ['name' => '英语', 'label' => '0'],
    ['name' => '政治', 'label' => '2'],
    ['name' => '绘画', 'label' => '1'],
    ['name' => '摄影', 'label' => '1'],
];
$subjectDate = [
    '0' => '理科',
    '1' => '艺术',
    '2' => '文科'
];
$result=[];
foreach ($subject as $k=>$v){
    $label=$v['label'];
  $name=$subjectDate[$label];
$result[$name][]=$v['name'];

}
//print_r($result);

 

 (2):

如图json字符串转化成数组且处理成以下格式(5分)

{"type_name":"电脑","spec":[{"name":"颜色","value":["红色","黑色","蓝色"]},{"name":"内存","value":["64G","256G"]}],"attr":[{"name":"毛重","value":"10g"},{"name":"产地","value":"北京"}]}

 

 

 

(原图)

 

 

(效果图)

$json='{"type_name":"电脑","spec":[{"name":"颜色","value":["红色","黑色","蓝色"]},{"name":"内存","value":["64G","256G"]}],"attr":[{"name":"毛重","value":"10g"},{"name":"产地","value":"北京"}]}';
 $new_json=json_decode($json,true);
$arr=[];
foreach ($new_json['spec'][0]['value'] as $k=>$v){
    $arr[$k]['type_name']=$new_json['type_name'];
    $arr[$k]['color']=$v;
}
print_r($arr);

 

标签:arr,name,一个二维,value,label,json,数组,格式,type
来源: https://www.cnblogs.com/xiaoyantongxue/p/15205561.html