0

How can portions of multidimensional arrays be added to an existing two-dimensional (associative) array?

For example, for an existing associative array with the following elements:

$builder = array();
$builder['builder_id'] =        $source['id'];
$builder['builder_name'] =      $source['name'];
$builder['builder_address'] =   $source['address'];

how can portions of the following multidimensional array:

$selection[$category['category_name']]['item_name'] = $category_general['item_name'];
$selection[$category['category_name']]['item_source'] = $category_general['item_source'];
$selection[$category['category_name']]['item_image'] = $category_general['item_image'];

be appended to create the following structure:

$builder['builder_id']
$builder['builder_name']
$builder['builder_address']
$builder['category_name']
$builder['category_name']['item_name']
$builder['category_name']['item_source']
$builder['category_name']['item_image']

Assignments like this didn't work:

$builder['category_name'] = $selection[$category['category_name']];
$builder['category_name'] = $selection[$category['category_name']][];

Any suggestions?

Thanks!

edit:
@symcbean you are correct. assignment failed due to null element in array being assigned.
@Snowsickle thanks for tip which identified source of problem noted.

3
  • PHP arrays are not multi-dimensional - they are nested. Commented Aug 15, 2012 at 21:25
  • can you clarify the difference between nested and multidimensional arrays? Commented Aug 17, 2012 at 20:57
  • Is google down again? Try stackoverflow.com/questions/11177615/… for starters Commented Aug 20, 2012 at 8:37

2 Answers 2

2

The first assignment should work.

$builder['category_name'] = $selection[$category['category_name']];

will assign all values contained in the array $selection[$category['category_name']] to the array $builder['category_name'].

DEMO

Sign up to request clarification or add additional context in comments.

3 Comments

PHP Error received when using this assignment method: A PHP Error was encountered Severity: Notice Message: Undefined index: Residential & Commercial Filename: controllers/cron.php Line Number: 238
Snowsickle is correct. If you get that error then the assignment you've shown in the second block of code in your question will fail too - and it the reason it fails is due to the contents of the $category array - not the method for combining the arrays.
@pseudo, please so a var_dump() of both the $selection and $builder arrays and edit your question with the result.
0

The assignment should work.. but if you need an alternative method:

foreach($selection[$category['category_name']] as $key => $value)
{
    $builder['category_name'][$key] = $value;
}

But once again, assignment with arrays has always worked for me.

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.