views:

53

answers:

2

Hi,

$cnt[0]=>Array( [0] => 0 [1] => 0 [2] => 0 ),
$cnt[1] => Array ( [0] => 1 [1] => 0 [2] => 0 ) 

i want convert this array to below result,

       $cnt[0]=(0,0);
       $cnt[1]=(0,0);
       $cnt[2]=(0,1);

any php function there to convert like this format,

Thanks,
Nithish.

+4  A: 
function flip($arr)
{
    $out = array();

    foreach ($arr as $key => $subarr)
    {
            foreach ($subarr as $subkey => $subvalue)
            {
                 $out[$subkey][$key] = $subvalue;
            }
    }

    return $out;
}

see more example in http://stackoverflow.com/questions/2221476/php-how-to-flip-the-rows-and-columns-of-a-2d-array

Haim Evgi
+2  A: 

I'm interpreting your expected output to be something like a list of pairs:

$pairs = array(
  array(1,0),
  array(0,0),
  array(0,0)
);

You'd simply check that the sub-arrays are the same length, and then use a for loop:

assert('count($cnt[0]) == count($cnt[1])');

$pairs = array();
for ($i = 0; $i < count($cnt[0]); ++$i)
  $pairs[] = array($cnt[0][$i], $cnt[1][$i]);
meagar