0

I have a one dimensional array as follows. (It may dynamically extend to any length)

Array
(
[0] => Array
    (
        [city] => Trivandrum
        [citykey] => ab5416c6
    )

[1] => Array
    (
        [city] => Kochi
        [citykey] => 85cb7d9c
    )

[2] => Array
    (
        [city] => Alappuzha
        [citykey] => 4d5f200e
    )

[3] => Array
    (
        [city] => Mumbai
        [citykey] => 47d98024
    )

)

Now I would like to split it to 3 arrays . I mean I would like to split an array in to 3 arrays as follows.

    Array
(
[0] => Array
    (
        [city] => Trivandrum
        [citykey] => ab5416c6
    )
[1] => Array
    (
        [city] => Mumbai
        [citykey] => 47d98024
    )

)

Array
(
[0] => Array
    (
        [city] => Kochi
        [citykey] => 85cb7d9c
    )

)

Array
(
[0] => Array
    (
        [city] => Alappuzha
        [citykey] => 4d5f200e
    )

)
8
  • 2
    Depending on how you want it split into 3 different arrays, array_chunk() may be helpful. Commented Apr 28, 2012 at 17:14
  • What do you want each array to contain? It's not clear from the question. Could you provide the expected output from this four element array? Commented Apr 28, 2012 at 17:15
  • I would like to get this output. Array ( [0] => Array ( [city] => Trivandrum [citykey] => ab5416c6 ) [1] => Array ( [city] => Mumbai [citykey] => 47d98024 ) ) Array ( [0] => Array ( [city] => Kochi [citykey] => 85cb7d9c ) ) Array ( [0] => Array ( [city] => Alappuzha [citykey] => 4d5f200e ) ) Commented Apr 28, 2012 at 17:19
  • Why do you want to create 3 independent array variables? Why can't they be a part of a parent array, like they are now. If you really want to create 3 different arrays, what names are you going to given them? Commented Apr 28, 2012 at 17:25
  • I just trying to split an array in to three. If the parent array contains 5 elements , then 1st,4th will go to the first array , 2nd and 5th will go to the second array and 3rd will go to the third array. Please help me Commented Apr 28, 2012 at 17:32

2 Answers 2

1
$a = range(1,4); // example input array of 4 items
$n = 3; // number of pieces you want in the output array $b

$b = array_fill(0, $n, array());
for($i=0; $i<count($a); $i++) {
    $b[$i % $n][] = $a[$i];
}

print_r($b);

// if you need to split them out
list($a1,$a2,$a3) = $b;
Sign up to request clarification or add additional context in comments.

Comments

0

Try this code:

<?
$i=0;
$array=Array(0=> Array("city" => 'Trivandrum',
        "citykey" => 'ab5416c6'
    ),
1 => Array
    (
        "city" => 'Kochi',
        "citykey" => '85cb7d9c'
    ),
2 => Array
    (
        "city" => 'Alappuzha',
        "citykey" => '4d5f200e'
    ),
3 => Array
    (
        "city" => 'Mumbai',
        "citykey" => '47d98024'
    )
);
$array_0=array();
$array_1=array();
$array_2=array();
foreach ($array as $value)
  {
  $name='array_'.$i%3;
  $temp[0]=$value;
  $$name=array_merge_recursive($$name, $temp);
  $i++;
  }
echo '<pre>';
print_r($array_0);
print_r($array_1);
print_r($array_2);
echo '</pre>';
?>

Your original array should be in variable $array

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.