Split array

This is my code:

foreach ($this->data as $var) {
if (isset($var[‘vendor’])) {
var_dump($var);
}
}

produces this:
array(2) { [“id”]=> string(1) “1” [“vendor”]=> string(10) “Aldi South” }

array(2) { [“id”]=> string(2) “45” [“vendor”]=> string(6) “Costco” }

array(2) { [“id”]=> string(2) “47” [“vendor”]=> string(17) “Red Clover Market” }

How do I combine these arrays into one array?

this might be a better way to ask my question?

new code:

$a1 = current($this->data);
$a2 = next($this->data);
$vendors = array_merge($a1,$a2);
var_dump($a1);
var_dump($a2);
var_dump($vendors);

produces this result:
array(2) { [“id”]=> string(1) “1” [“vendor”]=> string(10) “Aldi South” }

array(2) { [“id”]=> string(2) “45” [“vendor”]=> string(6) “Costco” }

array(2) { [“id”]=> string(2) “45” [“vendor”]=> string(6) “Costco” }

why doesn’t array_merge() work?

I think the easiest would be something like

$only_vendors = []; 
foreach ...
if ...
$only_vendors[] = $var;

But it does.
http://php.net/manual/en/function.array-merge.php

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.

2 Likes

Voila!

Thank-you so very much!

niche

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.