0

Here are the two Multi-dimension arrays explained.

// Categories Array
$categories = array(
  array('cat_id'=>'1', 'cat_name' => 'Category One', 'cat_data' => 'Some Data'), 
  array('cat_id'=>'2', 'cat_name' => 'Category Two', 'cat_data' => 'Some Data'),
  array('cat_id'=>'3', 'cat_name' => 'Category Tree', 'cat_data' => 'Some Data')
);
// Products Array (One $products array is to be placed inside every new category.
$products = array(
  array('p_id'=>'1', 'p_name'=>'Product One'), 
  array('p_id'=>'2', 'p_name'=>'Product Two'), 
  array('p_id'=>'3', 'p_name'=>'Product Three')
);

Here The $products needs to be placed inside every element of $category array, with a key of some random key take for eg 'product_list'.

Here is the result like

$category = array(
  array('cat_id'=>'1', 'cat_name' => 'Category One', 'cat_data' => 'Some Data', 'product_list'=>array()),
  array('cat_id'=>'2', 'cat_name' => 'Category Two', 'cat_data' => 'Some Data', 'product_list'=>array())
);

Please scroll Right for the above code to see the last element added to these elements.

Please tell how to add that multi-dimension array with a key to each n every element of the $category array. Thanks

2
  • Ok, was so much easier ;) Commented Sep 24, 2013 at 12:02
  • that relation is not required here, that is to be used in some different business logic, here I just wasn't able to add that multidimensional array with a key, but I figured it out quite easy one. thanks for interest bro. Commented Sep 24, 2013 at 12:04

3 Answers 3

1

What is the problem?

foreach ($categories as &$category) {
    $category['product_list'] = $products;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try to use this code.

foreach($categories as $key=>$value)
{
     $categories[$key]['product_list'] = $products;
}

Comments

0

After trying several attemps, I solved this problem by myself by just a simple code. here it is.

$categories['product_list'] = $products;

Hope, users finding this type of problem this useful. Thanks

Comments

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.