Trialcoder
Trialcoder

Reputation: 6004

How do I loop php arrays

This is my product array -

Array
(
    [0] => Array
        (
            [0] => 'Product1'
            [1] => 
            [2] => 
            [3] => 
            [4] => 'Product2'
            [5] => 
            [6] => 
            [7] => 'Product3'
            [8] => 'Product4'
            [9] => 'Product5'
        )
)

And this is the category array for the product -

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

Final output to be require in this 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 this but this is not forming the correct output --

$newarr = array();
foreach( $productData[0] as $arr1 )
{

    if( $arr1 != '' )
    {
        foreach( $catData[0] as $catnames ){
        $newarr[$arr1] = array($compnames);
        }
    }

}
echo "<pre>";print_r($newarr);

Upvotes: 0

Views: 82

Answers (2)

Ashwini Agarwal
Ashwini Agarwal

Reputation: 4858

Try this one..

$newarr = array();
foreach( $productData[0] as $key => $product )
{

    if( $product != '' )
    {
        $newarr[$product] = array();
        $currentProduct = $product;

    }

    if( isset($catData[0][$key]) && ($catData[0][$key] != '') )
    {
        $newarr[$currentProduct][] = $catData[0][$key];
    }
    else
    {
         $newarr[$currentProduct][] = 'No Cat';
    }

}
echo "<pre>";print_r($newarr);

Upvotes: 2

shapeshifter
shapeshifter

Reputation: 2967

If count($productData[0]) == count($catData[0]) you can use a for loop. If $productData[0][0] is always going to contain a product name then this should work.

<?php
// array to return
$a = array();
// loop thru array
for($i = 0; $i < count($productData[0]); $i++) {
  // if the product name exists its time to start a new array entry
  if($productData[0][$i] != '') {
    // get the product name as we need might need this for the next iteration
    $current = $productData[0][$i];
    // create the new array
    $a[$current] = array();

    if($catData[0][$i] != '') {
      // and save the category at this iteration into the new array
      $a[$current][] = $catData[0][$i];
    } else {
      $a[$current][] = 'No Cat';
    }
  } else {
    // product name was blank, and $current should be set, append the category.
    $a[$current][] = $catData[0][$i];
  }
}

echo "<pre>";
print_r($a);
echo "</pre>";

Upvotes: 1

Related Questions