dou72260 2017-04-10 21:50
浏览 20
已采纳

在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
  • 写回答

1条回答 默认 最新

  • douxi3233 2017-04-10 21:59
    关注

    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()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部