I want to create an array from this string, stored in database:
$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;
Manually defining the array works as expected:
$id_values = array("2148" => "50","2050" => "2","2403" => "1");
$id
isn't going to be the number from the original string - it will create a new one.
You could try this:
$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.'';
}
That's untested but it should be fine.
In the future, use json_encode
and json_decode
to store and retrieve arrays.
Note: You probably want to get rid of the quotes too - just add
$b = str_replace('"', '', $b);
on the line before $bobs = explode(" => ", $b);
please try below code
$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]).'';
}
It might be a better idea to save it in a better/more usable format, maybe using serialize:
The serialize function returns a string. You can then save that string in your database, at the same place you save your other string.
say this is your array:
$a = [2258=>"here",2259=>"then"];
$s = serialize($a);
// save the content of $s to your database
then, to get an array from the produced string:
// $s is the string from your database
$a = unserialize($s);
I think the below code will help you to decode the given string array correctly. Here you need to explore first by ',' and then followed by '=>'
$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;
Here str_replace function can be used to remove the quotes
I would just use eval instead. Basically make make it into a PHP expression and then use eval. Try this.
$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;
Please try the Below code Snipet It will work.
<?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];
}
}
Output:
array(3) {
[2148]=>
string(2) "50"
[2050]=>
string(1) "2"
[2403]=>
string(1) "1"
}