1. 程式人生 > 其它 >建立一個二維陣列將該陣列處理成以下格式

建立一個二維陣列將該陣列處理成以下格式

  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);