PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, March 8, 2022

[FIXED] remove quotation in array

 March 08, 2022     arrays, php, yii     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing