如何从字符串中爆炸数组?

时间:2015-04-20 12:59:22

标签: php arrays explode

我想从这个字符串创建一个数组,存储在数据库中:

$string = '"2148" => "50","2050" => "2","2403" => "1"';
$id_values = explode('=> "', $string);
foreach($id_values as $id => $value)
{
    $value_to_print .= '<img src="images/item_values/'.$id.'.gif"> - '.$value.'';
}

echo $value_to_print;

手动定义数组按预期工作:

$id_values = array("2148" => "50","2050" => "2","2403" => "1");

7 个答案:

答案 0 :(得分:5)

以更好/更有用的格式保存它可能是个更好的主意,可能使用serialize

serialize函数返回一个字符串。然后,您可以将该字符串保存在数据库中,保存其他字符串的位置。

说这是你的阵列:

$a = [2258=>"here",2259=>"then"];
$s = serialize($a);
// save the content of $s to your database

然后,从生成的字符串中获取数组:

// $s is the string from your database
$a = unserialize($s);

答案 1 :(得分:2)

$id不会是原始字符串中的数字 - 它会创建一个新字符串。

你可以试试这个:

$string = '"2148" => "50","2050" => "2","2403" => "1"';
$id_values = array();
$bits = explode(",", $string);
foreach ($bits as $b) {
    $bobs = explode(" => ", $b);
    $id_values[$bobs[0]] = $bobs[1];
}
foreach($id_values as $id => $value){
    $value_to_print .= '<img src="images/item_values/'.$id.'.gif"> - '.$value.'';
}

这是未经测试但应该没问题。

将来,请使用json_encodejson_decode来存储和检索数组。

注意:你可能也想删除引号 - 只需添加

$ b = str_replace('“','',$ b);

$bobs = explode(" => ", $b);

之前的行上

答案 2 :(得分:0)

请尝试以下代码

$value_to_print = '';
$string = '"2148" => "50","2050" => "2","2403" => "1"';

$array = explode(',',$string);

foreach($array as $data){
       $indata = explode('=>',$data);           
       $value_to_print .= '<img src="images/item_values/'.trim($indata[0]).'.gif"> - '.trim($indata[1]).'';

}

答案 3 :(得分:0)

我认为以下代码将帮助您正确解码给定的字符串数组。在这里,您需要首先通过','进行探索,然后再进行'=&gt;'

$string = '"2148" => "50","2050" => "2","2403" => "1"';

$value_to_print = "";

 $items = explode(",",$string);
foreach($items as $item)
{
  $pair = explode("=>",str_replace('"','',$item));
   $value_to_print .= '<img src="images/item_values/'.$pair[0].'.gif"> - '.$pair[1].'';
}

echo $value_to_print;

此处 str_replace 函数可用于删除引号

答案 4 :(得分:0)

$string = '"2148" => "50","2050" => "2","2403" => "1"';   
$arr = array();

// split into key-val pairs
foreach(explode(',', $string) as $pair) {
    // we use a regular expression in case the input does not 
    // have spaces surrounding the "hash-rocket"
    // we also use str_replace to remove quotes
    $key_val = preg_split("/[\s*]=>[\s*]/", str_replace('"', "", $pair));
    // ensure pair is well formed
    if (count($key_val) === 2) {
        $arr[$key_val[0]] = $key_val[1];
    }
}

输出:

array(3) {
  [2148]=>
  string(2) "50"
  [2050]=>
  string(1) "2"
  [2403]=>
  string(1) "1"
}

答案 5 :(得分:-1)

我只会使用eval代替。基本上make make成PHP表达式然后使用eval。试试这个。

$string = '"2148" => "50","2050" => "2","2403" => "1"';
$string = 'return array(' . $string . ');';
$array = eval($string);

$value_to_print = '';

foreach($array as $id => $value){
    $value_to_print .= '<img src="images/item_values/'.$id.'.gif"> - '.$value.'';
}

echo $value_to_print;

答案 6 :(得分:-1)

请尝试使用以下代码片段它会起作用。 enter image description here

<?php
    $string = '"2148" => "50","2050" => "2","2403" => "1"';
    $string = str_replace('"','',$string);

    $arrayList = explode(",",$string);
    $imageData = array();

    foreach($arrayList as $id=>$values){
        $arrayElemenets = explode(" => ",$values);
        $imageData[$arrayElemenets[0]] = $arrayElemenets[1];
    }

    $value_to_print = "";
    foreach($imageData as $id => $value){
        $value_to_print .= '<img src="images/item_values/'.$id.'.gif"> - '.$value.'';
    }

    echo $value_to_print;
?>