Skip to content
Advertisement

PHP separate values by comma

I am trying to separate my JSON Array:

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

WHERE

    $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"
        }
    ]

What i need is if one column like “Front_glass”:”X, XXX_or_XX, G” has three values it should be

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

or any thing that is possible Thanks.

Advertisement

Answer

You can use array_map function in your case. Look here live PHP sandbox

foreach($data as $key=>$row) {
    $data[$key] = array_map (
        function($el){
            return explode(',', $el);
        },
        $row
    );
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement