PHP Question

Make new array in PHP

A have some little problem with array in PHP.

I have this array:

I have this array.

There are many ['variants'] below.

I need to create a new array like this:

new array like this

from all ['variants'].

I have a function with this

$variants = array();
$features = $features['18']; foreach ($features as $feature) {
if (!empty($feature['variants'])) {
$variants = array_merge($variants, $feature['variants']);
return $variants;

But it have error:
[function.array-merge]: Argument #2 is not an array.

How can I fix this?

Answer Source

If you code correct - rewrite line 3:

if (!empty($feature['variants'])) {


if (!empty($feature['variants']) && is_array($feature['variants'])) {

And I would give the call to array_values. For example:

foreach($features as $feature)
    $result = [];
    if(isset($feature['variants']) && is_array($feature['variants']))
        $result = array_merge($result, array_values($feature['variants']));