Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have the following multidimensional array:

array (size=2)
      10 => 
        array (size=7)
          1 => 
            array (size=4)
              0 => string '10view1' (length=7)
              1 => string '10ins1' (length=6)
              2 => string '10edit1' (length=7)
              3 => string '10del1' (length=6)
          2 => 
            array (size=4)
              0 => string '10view2' (length=7)
              1 => string '10ins2' (length=6)
              2 => string '10edit2' (length=7)
              3 => string '10del2' (length=6)
          3 => 
            array (size=4)
              0 => string '10view3' (length=7)
              1 => string '10ins3' (length=6)
              2 => string '10edit3' (length=7)
              3 => string '10del3' (length=6)
      11 => 
        array (size=7)
          1 => 
            array (size=4)
              0 => string '11view1' (length=7)
              1 => string '11ins1' (length=6)
              2 => string '11edit1' (length=7)
              3 => string '11del1' (length=6)
          2 => 
            array (size=4)
              0 => string '11view2' (length=7)
              1 => string '11ins2' (length=6)
              2 => string '11edit2' (length=7)
              3 => string '11del2' (length=6)
          3 => 
            array (size=4)
              0 => string '11view3' (length=7)
              1 => string '11ins3' (length=6)
              2 => string '11edit3' (length=7)
              3 => string '11del3' (length=6)

I want to convert it to the array like:

1 => array(10 => array( 
              0 => string '10view1' (length=7)
              1 => string '10ins1' (length=6)
              2 => string '10edit1' (length=7)
              3 => string '10del1' (length=6)),
           11 => array(
              0 => string '11view1' (length=7)
              1 => string '11ins1' (length=6)
              2 => string '11edit1' (length=7)
              3 => string '11del1' (length=6) 
           )
        ),
2 => array(
          10 => array(
             0 => string '10view2' (length=7)
              1 => string '10ins2' (length=6)
              2 => string '10edit2' (length=7)
              3 => string '10del2' (length=6)
          ),
          11 => array(
               0 => string '11view2' (length=7)
              1 => string '11ins2' (length=6)
              2 => string '11edit2' (length=7)
              3 => string '11del2' (length=6)
          )
      ),
3 => array(
        10 => array(
             0 => string '11view3' (length=7)
              1 => string '11ins3' (length=6)
              2 => string '11edit3' (length=7)
              3 => string '11del3' (length=6)
          ),
          11 => array(
             0 => string '11view1' (length=7)
              1 => string '11ins1' (length=6)
              2 => string '11edit1' (length=7)
              3 => string '11del1' (length=6) 
          )
    )

please help me i confuse to solve this problem thanks in advance.

It's quite obvious for me how to implement this using foreach loop, but I wonder if it's possible to do this with PHP array functions like array_map or array_walk. Please use PHP 5.3 for the callback function. Thank you!

share|improve this question
    

1 Answer 1

Using array map you can achieve like this

<?php

function merge_array($n, $m)
{
   $data = array();

   $data[10] = $n;
   $data[11] = $m;

   return $data;

}

$c = array_map("merge_array", $array[10], $array[11]);
print_r($c);

Using for loop it is very simple you can achieve like this

$count_10 = count($array[10]);

$count_11 = count($array[11]);

$count = ($count_10>$count_11)?$count_10:$count_11;

$data = array();

for($i=0; $i<$count; $i++)
{

  if(isset($array[10][$i]))
  {
       $data[$i][] =  $array[10][$i];
  }

  if(isset($array[11][$i]))
  {
       $data[$i][] =  $array[11][$i];
  }

}
share|improve this answer
    
thanks you for reply but i used snipped array in this question here actually my array is dynamically so if you are any more idea please tell me. –  Jigar Prajapati Jun 19 at 8:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.