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
)
Related
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);
I'm getting a response from a service and print_r() shows an array but it seems to be different from usual arrays.
If I print array_values it's are empty.
PHP:
print_r($token);
//Result: Array ( [{"access_token":"123","token_type":"bearer"}] => )
print_r(array_values($token));
//Result: Array ( [0] => )
Why are the access_token and token_type values not listed in array_values?
The answer is not JSON, It's not because you have a JSON type array. It is because there is NO value in your array.
//Result: Array ( [{"access_token":"123","token_type":"bearer"}] => )
That array has one index with no value, hence array_values shows nothing. You have created that array incorrectlly :)
I can reproduce your outputs with this:
$token = array('{"access_token":"123","token_type":"bearer"}' => '');
That is because you have an array with the key
'{"access_token":"123","token_type":"bearer"}'
but no value.
To access the JSON string in the array key, you could do this:
$keys = array_keys($token);
print_r($keys[0]);
To access the JSON object, you can further do
print_r(json_decode($keys[0]));
Output:
(
[access_token] => 123
[token_type] => bearer
)
Demo: Fiddle
I have this string :
$string = '{"2r0ij":{"id":"2r0ij","title":"Official Reddit announcements","subscribers":7225390}}'
and I want convert to array. I tried with json_decode($string), but result is null. Same result with print_r($string).
What is wrong?
If you want to convert it to array, you need to set the second parameter of json_decode() to true:
$array = json_decode($string, true);
print_r($array);
You do need to use json_decode() with the 'true' flag -
$string = '{"2r0ij":{"id":"2r0ij","title":"Official Reddit announcements","subscribers":7225390}}';
$foo = json_decode($string, true);
print_r($foo);
echo $foo['2r0ij']['title'];
The result is:
Array ( [2r0ij] => Array ( [id] => 2r0ij [title] => Official Reddit announcements [subscribers] => 7225390 ) )
Official Reddit announcements
Here is a working example. The string, minus the opening and closing quotes, validates at JSONLint.
$resultViewed='["88"]';
$viewed=json_decode($resultViewed);
if(!in_array("9",$viewed)){
print_r($viewed);
$viewed = array_push($viewed,"9");
print_r($viewed);
}
This prints
Array ( [0] => 88 ) 2
Instead of
Array ( [0] => 88,[1]=>9 )
The array is valid, yet using array_push() to add another value isn't working as I'd expect.
remove assignment: $viewed =:
$viewed = array_push($viewed,"9");
Just:
array_push($viewed,"9");
Its already in the manual, it returns the new number of items, not the values of the array.
Or just use the simple way:
$viewed[] = "9";
I am using an API which returns data as a string in the following format:
{"domain.com":{"status":"available","classkey":"domcno"}}
I want to put this result in to a multi-dimensional PHP array, but since the input is a string, I can't think of a convenient way to convert this.
Is there a function that can automatically parse this data into an array as desired?
That's JSON, simple enough:
$array = json_decode($string, true);
Yields:
Array
(
[domain.com] => Array
(
[status] => available
[classkey] => domcno
)
)
$j = '{"domain.com":{"status":"available","classkey":"domcno"}}';
$d = json_decode($j,true);
print_r($d);