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
-
show how should look the whole final arrayRomanPerekhrest– RomanPerekhrest2017年04月11日 05:55:03 +00:00Commented Apr 11, 2017 at 5:55
1 Answer 1
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()
Rahul
18.6k7 gold badges43 silver badges64 bronze badges
answered Apr 11, 2017 at 5:59
Indrasis Datta
8,6182 gold badges18 silver badges32 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-php