Chew Halo
Chew Halo

Reputation: 147

Extract data from array in php

How can I extract data from this array:

Array
(
    [0] => Array
        (
            [name] => color / size 
            [value] => Array
                (
                    [0] => blue / l 
                    [1] => blue / m 
                    [2] => red / l 
                    [3] => red / m 
                )
}

in this format?

color = blue,red
size = L,M

Upvotes: 0

Views: 155

Answers (1)

Indrasis Datta
Indrasis Datta

Reputation: 8606

Using implode() and explode() will solve the purpose. Try this:

foreach ($array[0]['value'] as $k => $val) {
    $values  = explode(' / ', $val); // Break up string into array
    $color[] = $values[0]; // Store colors in this array
    $size[]  = $values[1];  // Store size in this array
}

echo 'Color = ' . implode(',', array_unique($color)); // First get unique values using array_unique, then convert this array to string using implode()
echo '<br/>';
echo 'Size = '. implode(',', array_unique($size)); // First get unique values using array_unique, then convert this array to string using implode()

Upvotes: 1

Related Questions