PHP 数组转换

孟玄德
  • 23

1数组合并
请问下怎么实现呢?谢谢了!

array(3) {
  [0] => array(7) {
    ["game_name"] => string(18) "趣看每日一笑"
    ["game_icon"] => string(32) "game_icon_wx0913c2de2ce8376a.png"
    ["id"] => int(1)
    ["gift_name"] => string(15) "进阶大礼包"
    ["gift_details"] => string(23) "金币*5000  砖石*100"
    ["need_score"] => int(30)
    ["gift_type"] => int(2)
  }
  [1] => array(7) {
    ["game_name"] => string(18) "趣看每日一笑"
    ["game_icon"] => string(32) "game_icon_wx0913c2de2ce8376a.png"
    ["id"] => int(2)
    ["gift_name"] => string(15) "高阶大礼包"
    ["gift_details"] => string(25) "金币*50000  砖石*1000"
    ["need_score"] => int(120)
    ["gift_type"] => int(2)
  }
  [2] => array(7) {
    ["game_name"] => string(18) "趣看热点事件"
    ["game_icon"] => string(32) "game_icon_wx945daf05d01dad39.png"
    ["id"] => int(3)
    ["gift_name"] => string(12) "初级礼包"
    ["gift_details"] => string(8) "红包*1"
    ["need_score"] => int(50)
    ["gift_type"] => int(1)
  }
}

修改为 :

array(3) {
  [0] => array(7) {
    ["game_name"] => string(18) "趣看每日一笑"
    ["game_icon"] => string(32) "game_icon_wx0913c2de2ce8376a.png"
    ['gift']=>array(2){
      [0]=>array(5){
        ["id"] => int(1)
        ["gift_name"] => string(15) "进阶大礼包"
        ["gift_details"] => string(23) "金币*5000  砖石*100"
        ["need_score"] => int(30)
        ["gift_type"] => int(2)
      }
      [1]=>array(5){
        ["id"] => int(2)
        ["gift_name"] => string(15) "高阶大礼包"
        ["gift_details"] => string(25) "金币*50000  砖石*1000"
        ["need_score"] => int(120)
        ["gift_type"] => int(2)
      }
    }
  }
  [2] => array(7) {
    ["game_name"] => string(18) "趣看热点事件"
    ["game_icon"] => string(32) "game_icon_wx945daf05d01dad39.png"
    ['gift']=>array(2){
      [0]=>array(){
        ["id"] => int(3)
        ["gift_name"] => string(12) "初级礼包"
        ["gift_details"] => string(8) "红包*1"
        ["need_score"] => int(50)
        ["gift_type"] => int(1)
      }
    }
  }
}

数组代码,我也懒得写了。给个JSON 数组吧!方便装换测试

[
{
"game_name": "趣看每日一笑",
"game_icon": "game_icon_wx0913c2de2ce8376a.png",
"id": 1,
"gift_name": "进阶大礼包",
"gift_details": "金币*5000 砖石*100",
"need_score": 30,
"gift_type": 2
},
{
"game_name": "趣看每日一笑",
"game_icon": "game_icon_wx0913c2de2ce8376a.png",
"id": 2,
"gift_name": "高阶大礼包",
"gift_details": "金币*50000 砖石*1000",
"need_score": 120,
"gift_type": 2
},
{
"game_name": "趣看热点事件",
"game_icon": "game_icon_wx945daf05d01dad39.png",
"id": 3,
"gift_name": "初级礼包",
"gift_details": "红包*1",
"need_score": 50,
"gift_type": 1
}
]
回复
阅读 984
2 个回答
✓ 已被采纳

第一种假设"game_name"和"game_icon"值都能分别各自相等

$data = json_decode(file_get_contents('array.json'), true);

$arr1 = array();
$arr2 = array();

foreach ($data as $item) {
    $arr2[]['gift'][0] = array_splice($item, 2);
    $arr1[] = $item;
}

//去重,按照通常方法比较(不修改类型)
$arr1_u = array_unique($arr1, SORT_REGULAR);

$data = array();
foreach ($arr1_u as $value) {
    /**
     * $value是这样一个数组:
     * array(
     * 'game_name' => '趣看每日一笑',
     * 'game_icon' => 'game_icon_wx0913c2de2ce8376a.png',
     * )
     */
    //找出'game_name'和'game_icon'值分别各自相等的数组的键
    $keys = array_keys($arr1, $value);

    $item = array_merge($arr1[$keys[0]], $arr2[$keys[0]]);
    $length = count($keys);
    if ($length > 1) {
        for ($i = 1; $i < $length; $i++) {
            $item['gift'][] = $arr2[$keys[$i]]['gift'];
        }
    }
    $data[] = $item;
}
var_dump($data[0], $data[1]);

第二种,只是'game_name'值相等

$data = json_decode(file_get_contents('array.json'), true);
$arr1 = array();
$arr2 = array();

foreach ($data as $item) {
    $arr2[]['gift'][0] = array_splice($item, 2);
    $arr1[] = $item;
}

//取出$arr2中'game_name'列作为数组单列,并去重
$arr_game_name = array_column($arr1, 'game_name');
$arr_game_name_u = array_unique($arr_game_name);

$data = array();

foreach ($arr_game_name_u as $value) {
    //找出'game_name'值相等的数组的键
    $keys = array_keys($arr_game_name, $value);

    $item = array_merge($arr1[$keys[0]], $arr2[$keys[0]]);
    $length = count($keys);
    if ($length > 1) {
        for ($i = 1; $i < $length; $i++) {
            $item['gift'][] = $arr2[$keys[$i]]['gift'][0];
        }
    }
    $data[] = $item;
}

数组代码,我也懒得写了。给个JSON 数组吧!方便装换测试

[
{
"game_name": "趣看每日一笑",
"game_icon": "game_icon_wx0913c2de2ce8376a.png",
"id": 1,
"gift_name": "进阶大礼包",
"gift_details": "金币*5000 砖石*100",
"need_score": 30,
"gift_type": 2
},
{
"game_name": "趣看每日一笑",
"game_icon": "game_icon_wx0913c2de2ce8376a.png",
"id": 2,
"gift_name": "高阶大礼包",
"gift_details": "金币*50000 砖石*1000",
"need_score": 120,
"gift_type": 2
},
{
"game_name": "趣看热点事件",
"game_icon": "game_icon_wx945daf05d01dad39.png",
"id": 3,
"gift_name": "初级礼包",
"gift_details": "红包*1",
"need_score": 50,
"gift_type": 1
}
]
你知道吗?

宣传栏