Convert an indexed array to separate arrays whose keys match

I think that my problem is easy to solve, but for life I can not understand.

I need to convert this multidimensional array:

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

        )

In it:

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

I tried using foreach loops, but I cannot get the expected results:

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

Here is phpfiddle I need to convert an array $locationsBADas an array$locationsGOOD

+3
source share
4 answers

Ofir was absent, the number of points of delivery is in the values.

Here is what I should solve your problem:

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

And I get:

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
        )

)
+3
source

You can try:

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;
  }
}
+3
source

. :

  • 1- .

, :

$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

, , : https://eval.in/99929

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

This actually gives you separate arrays of $ location0, $ location1, etc. This is what I interpreted as what you wanted.

+2
source

All Articles