【问题标题】:PHP separate values by commaPHP用逗号分隔值
【发布时间】:2021-03-30 14:28:40
【问题描述】:

我正在尝试分离我的 JSON 数组:

for ($i=0; $i < count($data); ++$i) {
    $mark=explode(',', $data[$i]);
    foreach ($mark as $out) {
        echo $out;
    }
}

在哪里

    $data = [
        {
            "2":"XXX_or_XX",
            "Left_headlamp":"XXX_or_XX",
            "6":"X, XXX_or_XX, G",
            "Front_glass":"X, XXX_or_XX, G",
            "17":"S2",
            "Right_front_tire":"S2",
            "25":"E3",
            "Left_front_door":"E3",
            "29":"FLS, RRS",
            "Engine_room":"FLS, RRS",
            "30":"6",
            "Stock_id":"6"
        }
    ]

我需要的是,如果像 "Front_glass":"X, XXX_or_XX, G" 这样的一列应该有三个值

"Front_glass":"X".
"Front_glass":"XXX_or_XX"  
"Front_glass":" XXX_or_XX"

或任何可能的事情谢谢。

【问题讨论】:

  • $data 字符串数组吗?
  • @SlavaRozhnev 是的!数组(1) { [0]=> 数组(12) { [2]=> 字符串(9) "XXX_or_XX" ["Left_headlamp"]=> 字符串(9) "XXX_or_XX" [6]=> 字符串(15) "X, XXX_or_XX, G" ["Front_glass"]=> 字符串(15) "X, XXX_or_XX, G" [17]=> 字符串(2) "S2" ["Right_front_tire"]=> 字符串(2) "S2 " [25]=> string(2) "E3" ["Left_front_door"]=> string(2) "E3" [29]=> string(8) "FLS, RRS" ["Engine_room"]=> string( 8) "FLS, RRS" [30]=> 字符串(1) "6" ["Stock_id"]=> 字符串(1) "6" } }

标签: php arrays json explode


【解决方案1】:

您可以在您的情况下使用 array_map 函数。看这里live PHP sandbox

foreach($data as $key=>$row) {
    $data[$key] = array_map (
        function($el){
            return explode(',', $el);
        },
        $row
    );
}

【讨论】:

  • 很高兴听到。欢迎您采纳答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-03
  • 1970-01-01
  • 2014-04-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多