dqpu4988 2014-01-26 14:40
浏览 29
已采纳

php从数组中提取特殊字符

I have the following array format. I need to extract the array to get all the attributes

Consider this is my sample result

Array
(
    [0] => Array
        (
            [bean_id] => 7628
            [product_id] => 7389
            [sku] => 950981-1
            [item_value] => Color:Green;Size:M
        )

    [1] => Array
        (
            [bean_id] => 7629
            [product_id] => 7389
            [sku] => 950981-2
            [item_value] => Size:L;Color:Green
        )

    [2] => Array
        (
            [bean_id] => 7630
            [product_id] => 7389
            [sku] => 950981-3
            [item_value] => Size:XL;Color:Green
        )

)

I have used the following loop to extract all the item_value;

foreach($attributes as $attribute) {
    list($key, $value) = explode(':', $attribute['item_value']);
    $product_attribute[$key][$attribute['sku']] .= $value.',';
} 
print_r($product_attribute); exit;

The above statement produces the output as

Array
 (
   [Color] => Array
     (
       [950981-1] => Green;Size, #Here i need to get only Green not **;size,**
     )    
   [Size] => Array
     (
       [950981-2] => L;Color,
       [950981-3] => XL;Color,
     )
 )
  • 写回答

2条回答 默认 最新

  • dongyanfeng0563 2014-01-26 15:14
    关注

    Try this:

    $product_attribute = array();
    foreach ($products as $product) {
        $sku = $product['sku'];
        foreach (explode(';', $product['item_value']) as $attr) {
            list($name, $val) = explode(':', $attr);
            $product_attribute[$name][$sku] = $val;
        }
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 Java-Oj-桌布的计算
  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路