0

I have an array from a post operation

$country = $_POST['country']; //The number of countries differ according to the user selection
$c = count($country);

Output: `

Array ( [0] => England,69,93 [1] => Australia,79,84 [2] => Greece,89,73 [3] => Germany,59,73 )`

I will have to break it into a multidimensional array like:

> Array ( [0] => Array ( [0] => England [1] => 69 [2] => 93)
>         [1] => Array ( [0] => Australia [1] => 79 [2] => 84)                      
>         [2] => Array ( [0] => Greece [1] => 89 [2] => 73)
>         [3] => Array ( [0] => Germany [1] => 59 [2] => 73))

How do I do this in PHP

I tried

$r = array();

foreach($country as &$r){
  $r = explode(",", $r);
  //for($i = 0; $i < count($country); $i++){
    //for($j = 0; $j < count($r); $j++){
        //$array[$i][$j] = $r;
    //}
  //}
}
echo '<br>';
print_r($r);

The for-loop also did not work and hence commented it out but left it as a an option if needed.

The print function now only prints 1 of the array. Not exactly sure what have I done wrong. Any help is appreciated. Thank you

1
  • try print_r($country); after your loop instead Commented Mar 23, 2013 at 11:51

3 Answers 3

1

You were nearly there:

$r = array();

foreach($country as $country_item){
  $r[] = explode(",", $country_item);
}
echo '<br>';
print_r($r);

The above should work.

What might be even better for you (if your country is unique in each array):

$r = array();

foreach($country as $country_item){
  $temp_array = explode(",", $country_item);
  $r[$temp_array[0]] = array($temp_array[1], $temp_array[2]); 
}
echo '<br>';
print_r($r);

This will give you an output like follows:

> Array ( [England] => Array ( [0] => 69 [1] => 93)
>         [Australia] => Array ( [0] => 79 [1] => 84)                      
>         [Greece] => Array ( [0] => 89 [1] => 73)
>         [Germany] => Array ( [0] => 59 [1] => 73))

This therefore means you can access a countries figures as follows:

$r[$country_name];
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much...I still don't understand though... As I did use this line... $r[] = explode(",", $country_item); and for that I get an error: Fatal error: [] operator not supported for strings in C:\wamp\www\clar\test5.php on line 21... yet it worked when I typed your code... Thank you again
0

try this

for($i=0;$i<count($country);$i++)
 {
      $country1[$i] = explode(",", $country[$i]);
 }

Comments

0

You are overriding your $r main array with the $r from the loop - this is the solution - always divide your vars:

$output = array();
foreach($country as $c){
  $parts = explode(',',$c);
  $output[] = $parts; 
}

print_r($output);

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.