如何从格式化文本制作多维数组? PHP

时间:2016-01-20 06:42:35

标签: php arrays multidimensional-array

我想创建一个多维数组,如;

$cars = array("brand"=>"toyota","type"=>"suv","color"=>"white");

我有一个类似的字符串;

$text = "brand => toyota,type => suv, color => white";

如何从此文本创建上一个数组?

9 个答案:

答案 0 :(得分:4)

最好创建一个小函数来执行此操作,以便将来可以使用它。这是我的功能:

function customArray($text) {
   $returnArray = array();
   $explode1 = explode(',',$text);
   foreach ($explode1 as $key => $value) {
     $tempArray = explode('=>',$value);
     $returnArray[trim($tempArray['0'])] = trim($tempArray['1']);
   }
   return $returnArray;
 }

答案 1 :(得分:2)

此代码将满足您的要求:

http://url?#calltoaction

输出

$text = "brand => toyota,type => suv, color => white"; $cars = array(); $str_explode = explode(",",$text); $count = count($str_explode); for($i=0;$i<$count;$i++){ $expl = explode("=>",$str_explode[$i]); $cars[$expl[0]] = $expl[1]; } print_r($cars);

答案 2 :(得分:2)

这只需要你加倍爆炸:

trim()

注意:在$key&amp;上使用$value {{1}}删除空格。

答案 3 :(得分:1)

您可以这样使用:

// your string
$string = "brand => toyota,type => suv, color => white";

// explode with ","
$explodedArr = explode(",",$string);

$newArr = array();
foreach ($explodedArr as $value) {
    // explode value with "=>"
    $explodedInner = explode("=>",$value);
    // also remove the white spaces
    $newArr[trim($explodedInner[0])] = $explodedInner[1]; // using trim() for removing white spaces
}

echo "<pre>";
print_r($newArr);

<强>结果:

Array
(
    [brand] =>  toyota
    [type] =>  suv
    [color] =>  white
)

答案 4 :(得分:1)

使用array_walkAnonymous function

的另一种解决方案
$text = "brand => toyota,type => suv, color => white";
$cars = array();
$kv = explode(',',$text);
array_walk($kv, function ($item) use (&$cars){
        $kv = explode('=>', $item); 
        $cars[trim($kv[0])] = trim($kv[1]);
        });
print_r($cars);

输出:

Array (
    [brand] => toyota
    [type] => suv
    [color] => white
)

答案 5 :(得分:0)

使用此代码:

$exploded_text = explode(",", $text);
$new_array = array();
for($loop=0; $loop<count($exploded_text); $loop++){
$explode = explode("=>", $exploded_text[$loop]);
$new_array[trim($explode[0])] = trim($explode[1]); 
}

答案 6 :(得分:0)

UPDATE dSource set [Flags] = [Flags] | 2, [Link] = 'PutLatestLink'
FROM   DataSource    AS dSource
       JOIN CATALOG  AS cLog ON  cLog.ItemID = dSource.ItemID
WHERE  dSource.flags = dSource.flags AND dSource.Link IS NULL AND dSource.ConnectionString IS NULL 
        AND dSource.NAME = 'NameofDatasource' AND cLog.path LIKE '%foldername%'

答案 7 :(得分:0)

你可以这样做吗

<?php
    $text = "brand => toyota,type => suv, color => white";
    $cars  = array();
    while  (strpos($text,'=>')!== false) {
        $p=strpos($text,'=>');
        if ($p!=false) {
                $i=trim(substr($text,0,$p));
                $text=substr($text,$p+2);
                $p=strpos($text,',');
                if ($p==false) {$p=strlen($text);}
                $v=trim(substr($text,0,$p));
                $text=substr($text,$p+1);
                $cars [$i]=$v;
                }
        }
?>

答案 8 :(得分:0)

假设代码后面指定的格式应该有效。

<?php
$text = "brand => toyota,type => suv, color => white";
$text = explode(',', $text);
$arr = array();
foreach (array_chunk($text, 1) as $chk) {
  $temp = explode('=>', $chk[0]);
  $arr[trim($temp[0])] = trim($temp[1]);

}
echo "<pre>";print_r($arr);echo "</pre>";

<强>输出:

Array
(
    [brand] => toyota
    [type] => suv
    [color] => white
)