将包含类似于 PHP 数组语法的键值对的分隔字符串解析为关联数组

问题描述 投票:0回答:7

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

$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");
php arrays text-parsing
7个回答
5
投票

以更好/更可用的格式保存它可能是一个更好的主意,也许使用 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);

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_encode
json_decode
来存储和检索数组。

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

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

上线前

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


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

}

0
投票

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

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


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"
}

-1
投票

我只会使用 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;

-1
投票

请尝试以下代码 Snipet 它会起作用。 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;
?>
© www.soinside.com 2019 - 2024. All rights reserved.