将索引数组转换为键匹配的单独数组

时间:2014-02-09 17:44:45

标签: php arrays

我认为我的问题很容易解决,但对于我的生活,我无法弄清楚。

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

[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++;
        }

我需要将$locationsBAD数组转换为类似于$locationsGOOD数组

phpfiddle

4 个答案:

答案 0 :(得分:3)

您可以尝试:

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

答案 1 :(得分:3)

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 :(得分:3)

你混淆了循环嵌套的顺序。它应该如下:

  1. 循环嵌套数组中的值
  2. 在第1级数组中按键循环。
  3. 所以代码应如下所示:

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

答案 3 :(得分:2)

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

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

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