在php中从数组中提取数据

时间:2017-04-11 05:50:53

标签: php arrays

如何从此数组中提取数据:

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

采用这种格式?

color = blue,red
size = L,M

1 个答案:

答案 0 :(得分:1)

使用implode()explode()将解决目的。试试这个:

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()