【问题标题】:Converting an indexed array into separate arrays whose keys match将索引数组转换为键匹配的单独数组
【发布时间】:2014-02-09 17:44:45
【问题描述】:

我认为我的问题很容易解决,但我这辈子都想不通。

我需要转换这个多维数组:

[additionallocations] => Array
        (
            [Address] => Array
                (
                    [0] => Address1
                    [1] => Address2
                )

            [City] => Array
                (
                    [0] => City1
                    [1] => City2
                )

            [State] => Array
                (
                    [0] => AK
                    [1] => DC
                )

            [Zip] => Array
                (
                    [0] => 234423
                    [1] => 32423
                )

            [Country] => Array
                (
                    [0] => US
                    [1] => US
                )

        )

进入这个:

[additionallocations0] => Array
        (
           [Address] => Address1
           [City] => City1
           [State] => AK
           [Zip] => 234423
           [Country] => US
        )
[additionallocations1] => Array 
        (
           [Address] => Address2
           [City] => City2
           [State] => DC
           [Zip] => 32423
           [Country] => US
         )

我尝试过使用 foreach 循环,但无法获得预期的结果:

$count = 0;
        foreach($_POST['additionallocations'] as $value => $key) {
            foreach($key as $row) {
                $additional['additional'.$count] = array($value => $row);
            }
            $count++;
        }

这是一个phpfiddle 我需要将$locationsBAD 数组转换成$locationsGOOD 数组的样子

【问题讨论】:

    标签: php arrays


    【解决方案1】:

    Ofir 缺少值中的位置计数。

    这是我要解决您的问题的方法:

    <?php
    // we need to know how many locations beforehand
    $qty = count($additionallocations["Address"]);
    
    for ($l=0; $l<$qty; $l++)
    {
        foreach($additionallocations as $param => $values)
        {
            $new_locations['location'.$l][$param] = $values[$l];
        }
    }
    print_r($new_locations);
    ?>
    

    我得到:

    Array
    (
        [location0] => Array
            (
                [Address] => Address1
                [City] => City1
                [State] => AK
                [Zip] => 234423
                [Country] => US
            )
    
        [location1] => Array
            (
                [Address] => Address2
                [City] => City2
                [State] => DC
                [Zip] => 32423
                [Country] => US
            )
    
    )
    

    【讨论】:

      【解决方案2】:

      你可以试试:

      foreach($_POST['additionallocations'] as $key => $values) {
        foreach ($values as $count => $value) {
          $name = 'additionallocations' . $count;
          if (!isset($output[$name]) {
            $output[$name] = array();
          }
          $output[$name][$key] = $value;
        }
      }
      

      【讨论】:

      • 您的解决方案更好,因为它避免了计算qty。你只是错过了 if 中的一个结束括号,这不是必需的,顺便说一句。
      【解决方案3】:

      您混淆了循环嵌套的顺序。应该是这样的:

      1. 按嵌套数组中的值循环
      2. 按一级数组中的键循环。

      所以代码应该是这样的:

      $locations = array(
          'Address' => array('Address1', 'Address2'),
          'City' => array('City1', 'City2'),
          'State' => array('AK', 'DC'),
          'Zip' => array('234423', '32423'),
          'Country' => array('US', 'US'),
      );
      
      $result = array();
      for ($i = 0;; $i++)
      {
          $b_more = false;
          $arr = array();
          foreach ($locations as $key => $loc)
          {
              $arr[$key] = $i < count($loc) ? $loc[$i] : 0;
              if ($i < count($loc) - 1)
                  $b_more = true;
          }
          $result['additionallocations' . $i] = $arr;
          if (!$b_more)
              break;
      }
      print_r($result);
      

      【讨论】:

        【解决方案4】:

        看起来我迟到了,但这也有效: https://eval.in/99929

           foreach($additionallocations as $key=>$ary) {
               foreach($ary as $i=>$data) {
                   ${location.$i}[$key] = $data;
               }
           }
        

        这实际上为您提供了单独的数组 $location0、$location1 等。这是我解释为您想要的。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-08-01
          • 1970-01-01
          • 2018-03-04
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多