Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 删除数组中的引号_Php_Arrays_Yii - Fatal编程技术网

Php 删除数组中的引号

Php 删除数组中的引号,php,arrays,yii,Php,Arrays,Yii,我有这样一个字符串:- $a = " [abc,hjhd],[ccdc,cdc],[csc,vdfv]"; 我想将此字符串插入数组中 $marker_tower_line = array( 'type' => 'Feature', 'properties' => array( 'marker-color' => '#f00', 'marker-size' => 'small' ), 'g

我有这样一个字符串:-

$a = " [abc,hjhd],[ccdc,cdc],[csc,vdfv]";
我想将此字符串插入数组中

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

["[abc,hjhd],[ccdc,cdc],[csc,vdfv]"];
但我需要-

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

我想你在找这个

$somearray=explode(",",$a);
然后使用$somearray作为坐标。唯一需要注意的是,您必须在逻辑中使用这个想法来实现。例如,如果$a是您正在生成的字符串,那么就这样做

$a = "[abc,hjhd].,[ccdc,cdc].,[csc,vdfv]";
然后使用explode作为

$somearray=explode(".,",$a);
希望这有帮助。

最简单的答案(一行简单的php函数):-


输出:-

您可以使用此代码。函数
make_my_array()
适用于以给定格式编码的任何字符串

make_my_array()函数将字符串作为参数,并遍历每个字符以生成输出数组。它通过“[”字符确定集合的起始,通过“,”字符确定单独的集合元素,“]”字符确定集合的结束

function make_my_array($sa) {
    $s = "";
    $ans = array();

    for($i=0; $i<strlen($sa); $i++) {
        $t = array();
        if($sa[$i] == '[') {
            for($j=$i+1; $j<strlen($sa); $j++) {
                if($sa[$j] == ',') {
                    $t[] = $s;
                    $s = "";
                }
                else if($sa[$j] == ']') {
                    $t[] = $s;
                    $s = "";
                    $i = $j + 1;
                    $ans[] = $t;
                    break;
                }
                else {
                    $s .= $sa[$j];
                }
            }
        }
    }

    return $ans;
}


$a = " [abc,hjhd],[ccdc,cdc],[csc,vdfv]";
$marker_tower_line = array(
    'type' => 'Feature',
    'properties' => array(          
        'marker-color' => '#f00',
        'marker-size' => 'small'
    ),
    'geometry' => array(
        'type' => 'LineString',
        'coordinates' => make_my_array($a)
    )
);
函数make_my_数组($sa){
$s=“”;
$ans=数组();
对于($i=0;$i数组(
“标记颜色”=>“#f00”,
“标记大小”=>“小”
),
“几何体”=>数组(
'type'=>'LineString',
“坐标”=>使_成为我的_数组($a)
)
);

但正如您所说,这是一个刺痛,所以会被引用?您需要对数据做什么?您需要解析字符串并转换为所需的数组。这也是一个非常冗长的代码,需要做和理解。请添加一个解释,说明您需要做什么did@prabha很高兴帮助你:):)
<?php
$a = " [abc,hjhd],[ccdc,cdc],[csc,vdfv]";

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

print_r($b);
function make_my_array($sa) {
    $s = "";
    $ans = array();

    for($i=0; $i<strlen($sa); $i++) {
        $t = array();
        if($sa[$i] == '[') {
            for($j=$i+1; $j<strlen($sa); $j++) {
                if($sa[$j] == ',') {
                    $t[] = $s;
                    $s = "";
                }
                else if($sa[$j] == ']') {
                    $t[] = $s;
                    $s = "";
                    $i = $j + 1;
                    $ans[] = $t;
                    break;
                }
                else {
                    $s .= $sa[$j];
                }
            }
        }
    }

    return $ans;
}


$a = " [abc,hjhd],[ccdc,cdc],[csc,vdfv]";
$marker_tower_line = array(
    'type' => 'Feature',
    'properties' => array(          
        'marker-color' => '#f00',
        'marker-size' => 'small'
    ),
    'geometry' => array(
        'type' => 'LineString',
        'coordinates' => make_my_array($a)
    )
);