0

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);
11
  • how the merge should be done? what's the logic behind it? Commented Mar 13, 2013 at 6:29
  • 1
    How did you end up with these arrays? Commented Mar 13, 2013 at 6:30
  • try this $newarr[$arr1][] = $compnames; Commented Mar 13, 2013 at 6:30
  • @Dasun Logic is the- Product array iterates over the category array and fill category till next product name meet..if it is crate a key and iterates angain Commented Mar 13, 2013 at 6:32
  • 1
    Is this data pulled from a db? This is a crazy situation to be in. I just want to live. Commented Mar 13, 2013 at 6:35

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

4 Comments

Thx bro for another solution +1 :)
This solution will also handle 'NO Cat'. :)
Should be $newarr[$currentProduct][] = $catData[0][$key];
Also note isset('') will return true. If you are importing empty fields from excel they may be empty strings not null or false.
1

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>";

2 Comments

yes first will always contain a name..thx a lot +1 and green tick :)
No problem! I just realised I didn't handle "No Cat".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.