4

$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.

shamittomar
  • 46,210
  • 12
  • 74
  • 78
Nithish
  • 253
  • 2
  • 4
  • 16

2 Answers2

4
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 PHP - how to flip the rows and columns of a 2D array

Community
  • 1
  • 1
Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
2

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]);
user229044
  • 232,980
  • 40
  • 330
  • 338