convert sql array value result to array php - php

I have a column of datatype array in sql like:
This value is received as :
$customlabel = "{new,\"tree test123\",de}"
I tried json_decode($customlabel, TRUE) to get it into array. But this just returns empty string

This is not valid JSON. You can not decode the JSON. So you can consider it as a string and my answer would be..
$customlabel = html_entity_decode("{new,\"tree test123\",de}");
$customlabel = str_replace(array('{', '}'), '', $customlabel);
$customlabel = explode(',', $customlabel);
$customlabel = array_map('strval', $customlabel);
print_r($customlabel);
Your output will be...
Array ( [0] => new [1] => "tree test123" [2] => de )

Related

Convert array string into an array Laravel

I've saved some users ids in the database like this:
column user_ids: [2,1]
When I show column's value is "[2,1]"
How can I convert this to an array!
It's a valid Json string. You can use json_decode to get the array.
json_decode("[2,1]");
You can try this code :
<?php
$string = "[2,1]";
$result = json_decode($string);
print_r($result);
?>
Output :
Array
(
[0] => 2
[1] => 1
)

PHP insert keys in string

I have the following string:
{"1":"http://localhost:8888/classecar/uploads/dropzone/ferrari1.jpg","2":"http://localhost:8888/classecar/uploads/dropzone/ferrari2.jpg","3":"http://localhost:8888/classecar/uploads/dropzone/ferrari3.jpg","4":"http://localhost:8888/classecar/uploads/dropzone/ferrari4.jpg"}
How can I get rid of the numbers preceding "http..." and transform this same numbers in array keys?
Like this:
[0] => "http...",
[1] => "http...",
[2] => "http...",
That looks like a JSON string, so you could decode it.
You could try
$array = json_decode($string, true);
You may also need to reindex the array so it is 0 based; so something like
$array = array_values(json_decode($string, true));
you are having an array data in JSON formation. you have to use a PHP function json_decode to get an result.
$php_array = json_decode($your_array[0], true);
//to see your array
print_r($php_array);
You are missing something I think.
Look at this snippet:
$a=[
0 => '{
"1":"http:\/\/localhost:8888\/classecar\/uploads\/dropzone\/ferrari1.jpg",
"2":"http:\/\/localhost:8888\/classecar\/uploads\/dropzone\/ferrari2.jpg",
"3":"http:\/\/localhost:8888\/classecar\/uploads\/dropzone\/ferrari3.jpg",
"4":"http:\/\/localhost:8888\/classecar\/uploads\/dropzone\/ferrari4.jpg"
}'
];
echo "<pre>";
print_r(json_decode($a[0],TRUE));
it returns:
Array
(
[1] => http://localhost:8888/classecar/uploads/dropzone/ferrari1.jpg
[2] => http://localhost:8888/classecar/uploads/dropzone/ferrari2.jpg
[3] => http://localhost:8888/classecar/uploads/dropzone/ferrari3.jpg
[4] => http://localhost:8888/classecar/uploads/dropzone/ferrari4.jpg
)
This will work considering that the array value is a "string" containing a json object.
A var_dump on your array will give you an exact idea on what type the array value is.
Hope this helps:
<?php
//you might want to convert the JSON string to an array:
$json = '{"1":"http://localhost:8888/classecar/uploads/dropzone/ferrari1.jpg","2":"http://localhost:8888/classecar/uploads/dropzone/ferrari2.jpg","3":"http://localhost:8888/classecar/uploads/dropzone/ferrari3.jpg","4":"http://localhost:8888/classecar/uploads/dropzone/ferrari4.jpg"}';
$array = json_decode($json);
var_dump($array);
// here you already have the json converted to an php array
$arrayWithoutStrangeIndexes = [];
foreach($array as $index => $content){
$arrayWithoutStrangeIndexes[]= $content;
}
// here is just your array with plain data
var_dump($arrayWithoutStrangeIndexes);

Decode serialized cookie and loop options

I store form elements as serialized data in a cookie.
On another page I want to collect this cookie but the cookie contains this string:
form_key=kcE3W2vzParNhPN5&options%5B1508%5D=2025&options%5B1509%5D=1234&options%5B1510%5D=5678&options%5B1511%5D=&options%5B1512%5D=&options%5B1513%5D=&productId=59891
%5B and %5D are brackets I figured, but how can I look through all these options in the string and get their ID + value into an array with PHP.
So from above string I would like to create an array with:
arr = array (
[1508] = '2025';
[1509] = '1234';
[1510] = '5678';
[1511] = '';
[1512] = '';
);
I think what you want is parse_str():
$str = "form_key=kcE3W2vzParNhPN5&options%5B1508%5D=2025&options%5B1509%5D=1234&options%5B1510%5D=5678&options%5B1511%5D=&options%5B1512%5D=&options%5B1513%5D=&productId=59891";
$output = array();
parse_str($str, $output);
print_r($output); // $output['options'] will contain your array you're looking for.
See execution here:
Array
(
[form_key] => kcE3W2vzParNhPN5
[options] => Array
(
[1508] => 2025
[1509] => 1234
[1510] => 5678
[1511] =>
[1512] =>
[1513] =>
)
[productId] => 59891
)

IOS Dictionary to php array

I have IOS dictionary like
[{\"my_id_one\":16403,\"my_id_two\":7239}]
When I decode using json_decode, it returns null.
What is best way to decode it to convert it to a PHP array?
Take a look at stripcslashes():
$str = '[{\"my_id_one\":16403,\"my_id_two\":7239}]';
$str_unescaped = stripcslashes($str);
$array = json_decode($str_unescaped, true);
print_r($array);
This outputs:
Array ( [0] => Array ( [my_id_one] => 16403 [my_id_two] => 7239 ) )

PHP string to nested / multidimensional array

I have this example php string:
$string = "#[item_1][door] #[mozart][grass] = yes #[mozart][green] = no #[mozart][human] #[blue][movie]=yes #[item_1][beat] = yes #[item_1][music] = no
";
now $string idented just to easy view:
#[item_1][door]
#[mozart][grass] = yes
#[mozart][green] = no
#[mozart][human]
#[blue][movie]=yes
#[item_1][beat] = yes
#[item_1][music] = no
I want to know how can i get this string ( or other string following this style ) and transform in an array that looks like:
Array
(
[item_1] => Array
(
[door] => Array
(
[mozart] => Array
(
[grass] => yes
[green] => no
[human] => Array
(
[blue] => Array
(
[movie] => yes
)
)
)
)
[beat] => yes
[music] => no
)
)
What i tried
I tried to use and recursive function to create an nested array but i can't have access to the array pointer ( in deep levels ) in recursive functions.. don't know why.. maybe is the wrong patch to the answer.
thank you,
OK, I hope you still need this, because I wasted more time than I'd like to admin getting this right :)
Basically, my approach was to first manipulate the string into the format [set][of][keys]=value, and then loop through the string of keys and comparing them with the last set of keys to create the correct key hierarchy. I used eval because it's easier, but you can write a replacement function if you can't stomach seeing that function in your code:
//FIRST WE GET THE STRING INTO EASIER TO WORK WITH CHUNKS
$original_string = "#[item_1][door] #[mozart][grass] = yes #[mozart][green] = no #[mozart][human] #[blue][movie]=yes #[item_1][beat] = yes #[item_1][music] = no ";
$cleaned_string = str_replace('] #[','][',$original_string);
/* This results in clusters of keys that equal a value:
#[item_1][door][mozart][grass] = yes #[mozart][green] = no #[mozart][human][blue][movie]=yes #[item_1][beat] = yes #[item_1][music] = no
OR (with line breaks for clarity):
#[item_1][door][mozart][grass] = yes
#[mozart][green] = no
#[mozart][human][blue][movie]=yes
#[item_1][beat] = yes
#[item_1][music] = no */
//break it up into an array:
$elements = explode('#',$cleaned_string);
//create a variable to compare the last string to
$last_keys = "";
//and another that will serve as our final array
$array_of_arrays = array();
//now loop through each [item_1][door][mozart][grass] = yes,[mozart][green] = no, etc
foreach($elements as $element){
if ($element==""){continue;} //skip the first empty item
//break the string into [0] = group of keys and [1] the value that terminates the string
//so [item_1][door][mozart][grass] = yes BECOMES [item_1][door][mozart][grass], AND yes
$pieces = explode('=',str_replace(array('[',']'),array("['","']"),trim($element)));
//now compare this set of keys to the last set of keys, and if they overlap merge them into a single key string
$clean_keys = combine_key_strings($pieces[0],$last_keys);
//set the new key string the value for the next comparison
$last_keys = $clean_keys;
//and (ugly, I know) we use an eval to convert "[item_1][door][mozart][grass]='yes'" into a properly keyed array
eval("\$array_of_arrays".$clean_keys." = '".trim($pieces[1])."';");
}
//now dump the contents
print_r($array_of_arrays);
//THIS FUNCTION COMPA
function combine_key_strings($new,$old){
//get the key that starts the newer string
$new_keys = explode('][',$new);
$first_key = $new_keys[0].']';
//see if it appears in the last string
$last_occurance = strrpos ($old,$first_key);
//if so, merge the two strings to create the full array keystring
if (is_int($last_occurance)){
return substr($old,0,$last_occurance).$new;
}
return $new;
}
This should spit out your correctly nested array:
Array
(
[item_1] => Array
(
[door] => Array
(
[mozart] => Array
(
[grass] => yes
[green] => no
[human] => Array
(
[blue] => Array
(
[movie] => yes
)
)
)
)
[beat] => yes
[music] => no
)
)
Good night!

Categories