Tuesday, March 8, 2022

[FIXED] remove quotation in array

Issue

I have a string like this:-

$a = " [abc,hjhd],[ccdc,cdc],[csc,vdfv]";

I want to insert this string into an array.

$marker_tower_line = array(
    'type' => 'Feature',
    'properties' => array(          
        'marker-color' => '#f00',
        'marker-size' => 'small'
    ),
    'geometry' => array(
        'type' => 'LineString',
        'coordinates' => array (
             $a 
        )
    )
);

The output coming is-

["[abc,hjhd],[ccdc,cdc],[csc,vdfv]"];

But I need-

[[abc,hjhd],[ccdc,cdc],[csc,vdfv]];

Solution

The most Simplest answer (one-liner with simple php functions):-

<?php
$a = " [abc,hjhd],[ccdc,cdc],[csc,vdfv]";

$b = array_chunk(explode(",",str_replace(array("[","]"),array("",""),trim($a))),2);

print_r($b);

Output:- https://eval.in/833862

Or a bit more shorten (without trim()):-

<?php
$a = " [abc,hjhd],[ccdc,cdc],[csc,vdfv]";

$b = array_chunk(explode(",",str_replace(array("[","]"," "),array("","",""),$a)),2);

print_r($b);

Output:- https://eval.in/833882



Answered By - Anant Kumar Singh

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.