3

I think my problem is simple to solve but for the life of me I can't figure it out.

I need to convert this multi dimensional 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
                )

        )

Into this:

[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 have tried using foreach loops but I can not 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 a phpfiddle I need to convert the $locationsBAD array into looking like the $locationsGOOD array

4 Answers 4

3

Ofir was missing the locations count is in the values.

Here is what I got to solve your issue:

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

)
Sign up to request clarification or add additional context in comments.

Comments

3

You can try with:

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 Comment

Your solution is nicer because it avoids computing qty. You just miss a closing parens in the if, which is not required, btw.
3

You have confused order of loops nesting. It should be the following:

  1. Loop by values in nested arrays
  2. Loop by keys in 1st level array.

So the code should look like this:

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

Comments

2

Looks like I'm late to the party but this works too: https://eval.in/99929

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

This actually gives you separate arrays $location0, $location1 etc. Which is what I interpreted to be what you wanted.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.