在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
如何从此数组中提取数据: p>
Array
(
[0] =>数组
(
[名称] =>颜色/尺寸
[值] =>数组
(
[0] =>蓝色/ l \ n [1] => blue / m
[2] => red / l
[3] => red / m
)
}
code> pre>
采用这种格式? p>
color = blue,red
size = L,M
code> pre>
div>
答
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()