如何从字符串中分解数组?

How to explode an array from a string?

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

$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");

$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);

前一行

请尝试以下代码

$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]).'';

}

最好将其保存为 better/more 可用的格式,也许使用 serialize:

序列化函数returns一个字符串。然后,您可以将该字符串保存在数据库中,与保存其他字符串的位置相同。

假设这是你的数组:

$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);

我认为下面的代码将帮助您正确解码给定的字符串数组。在这里你需要先探索 ',' 然后再探索 '=>'

$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函数可以用来去掉引号

我会改用 eval。基本上把它变成一个 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;

请尝试下面的代码片段它会起作用。

<?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;
?>
$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"
}