【问题标题】:How to move array in multidimensional array using key value?如何使用键值在多维数组中移动数组?
【发布时间】:2019-04-06 06:55:07
【问题描述】:

如何在具有键值的多维数组中移动数组? 我正在使用 array_push 将值添加到多维数组。我有一个数组

$myArray = Array(Array('code' => '1','room' => Array('name' => 'Room-A')),Array('code' =>'1','room' => Array('name' => 'Room-B'
            )), Array('code' => '2','room' => Array('name' => 'Vip-1')),Array('code' => '2','room' => Array('name' => 'Vip-2')));

我尝试使用这样的代码:

for ($i=0; $i <count($myArray) ; $i++) { 
        if ($myArray[$i]['code']=='1') {
            array_push($myArray[$i]['room'], $myArray[$i]['room']);
        }
        else{
            array_push($myArray[$i]['room'], $myArray[$i]['room']);
        }
    }

我想要这样的结果:

Array
(
    [0] => Array
        (
           [code] => 1
           [room] => Array
             (
               [0] => Array
                 (
                    [name] => Room-A 
                 )
               [1] => Array
                 (
                   [name] => Room-B
                 )
             )
        )

    [1] => Array
      (
          [code] => 2
          [room] => Array
              (
             [0] => Array
                 (
                    [name] => Vip-1  
                 )
             [1] => Array
                 (
                   [name] => Vip-2
                 )
              )
        )
  )

知道如何加入这个数组吗?

【问题讨论】:

    标签: php arrays loops


    【解决方案1】:

    您可以使用array_reduce 将数组汇总为关联数组,使用代码作为键。使用array_values将关联数组转换为简单数组。

    $myArray = ....
    
    $result = array_values(array_reduce($myArray, function($c, $v){
        if ( !isset( $c[ $v['code'] ] ) ) $c[ $v['code'] ] = array( 'code' => $v['code'], 'room' => array() );
        $c[ $v['code'] ]['room'][] = $v['room'];
        return $c;
    },array()));        
    
    echo "<pre>";
    print_r( $result );
    echo "</pre>";
    

    这将导致:

    Array
    (
        [0] => Array
            (
                [code] => 1
                [room] => Array
                    (
                        [0] => Array
                            (
                                [name] => Room-A
                            )
    
                        [1] => Array
                            (
                                [name] => Room-B
                            )
    
                    )
    
            )
    
        [1] => Array
            (
                [code] => 2
                [room] => Array
                    (
                        [0] => Array
                            (
                                [name] => Vip-1
                            )
    
                        [1] => Array
                            (
                                [name] => Vip-2
                            )
    
                    )
    
            )
    
    )
    

    【讨论】:

      【解决方案2】:

      这是 foreach() 相当于 Eddie 的答案(我发现它更容易阅读/维护)。

      代码:(Demo)

      $myArray = [
          ['code' => '1', 'room' => ['name' => 'Room-A']],
          ['code' => '1', 'room' => ['name' => 'Room-B']],
          ['code' => '2', 'room' => ['name' => 'Vip-1']],
          ['code' => '2', 'room' => ['name' => 'Vip-2']]
      ];
      foreach ($myArray as $row) {
          if (!isset($result[$row['code']])) {
              $result[$row['code']] = ['code' => $row['code'], 'room' => [$row['room']]];
              //                                                         ^------------^-- pushed deeper
          } else {
              $result[$row['code']]['room'][] = $row['room'];
              //                           ^^-- pushed deeper
          }
      }
      var_export(array_values($result));
      

      此问题与许多想要按特定列分组的预先存在的问题非常相似。因为这个问题的要求是创建一个更深的结构来包含 room 子数组,所以我无法找到一个确切的副本来结束。

      通常我会写:

      if (!isset($result[$row['code']])) {
          $result[$row['code']] = $row;
      

      要减少语法,但新的输出结构必须同时应用于ifelse

      为避免键重复/冲突,房间子数组需要被推送/索引到较低的级别。

      最后,该技术已在 StackOverflow 上多次演示。您将元素值作为分组依据,并在迭代输入数组时将该值用作临时键。检查临时密钥是否存在是确定组是新组还是以前遇到过的最快方法。完成后,调用array_values() 删除临时键(重新索引数组)。

      【讨论】:

      • 它确实令人印象深刻,基本上我不知道该怎么做,也不知道我必须搜索什么。我的知识真的很差,因为我刚刚学习了编程。感谢您用一个简单的示例来解决我的问题
      猜你喜欢
      • 1970-01-01
      • 2012-01-05
      • 1970-01-01
      • 2013-09-21
      • 2016-12-07
      • 2015-10-30
      • 2019-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多