How do I loop php arrays -


this product array -

array (     [0] => array         (             [0] => 'product1'             [1] =>              [2] =>              [3] =>              [4] => 'product2'             [5] =>              [6] =>              [7] => 'product3'             [8] => 'product4'             [9] => 'product5'         ) ) 

and category array product -

array (     [0] => array         (             [0] => 'cat1'             [1] => 'cat2'             [2] => 'cat3'             [3] => 'cat4'             [4] => 'cat5'             [5] => 'cat6'             [6] => 'cat7'             [7] =>              [8] =>              [9] =>          ) ) 

final output require in format -

array ( 'product1' => array(            [0] => 'cat1',            [1] => 'cat2',            [2] => 'cat1',            [3] => 'cat4'            ), 'product2' => array(            [0] => 'cat5',            [1] => 'cat6',            [2] => 'cat7'            ), 'product3' => array(            [0] => 'no cat'            ), 'product4' => array(            [0] => 'no cat'            ), 'product5' => array(            [0] => 'no cat',            ) ) 

i tried not forming correct output --

$newarr = array(); foreach( $productdata[0] $arr1 ) {      if( $arr1 != '' )     {         foreach( $catdata[0] $catnames ){         $newarr[$arr1] = array($compnames);         }     }  } echo "<pre>";print_r($newarr); 

if count($productdata[0]) == count($catdata[0]) can use loop. if $productdata[0][0] going contain product name should work.

<?php // array return $a = array(); // loop thru array for($i = 0; $i < count($productdata[0]); $i++) {   // if product name exists time start new array entry   if($productdata[0][$i] != '') {     // product name need might need next iteration     $current = $productdata[0][$i];     // create new array     $a[$current] = array();      if($catdata[0][$i] != '') {       // , save category @ iteration new array       $a[$current][] = $catdata[0][$i];     } else {       $a[$current][] = 'no cat';     }   } else {     // product name blank, , $current should set, append category.     $a[$current][] = $catdata[0][$i];   } }  echo "<pre>"; print_r($a); echo "</pre>"; 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -