This question already has answers here:
How to convert JSON string to array
(17 answers)
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
{
"receiver_uid":[
"58a43a3e3fbbf3.61108490",
"58a43be07a3bc3.90311110",
"58da53ab5ce8d6.84754819"
]
}
This is the value I would like to convert to array. I know it's quite a simple question, but please can anyone help me?
Use json_decode
json_decode($your_jsonString,true)
You should use json_decode.
Try this:
$data = json_decode($your_json_string, TRUE);
The second parameter will make decoded json string into an associative arrays.
Example :-
$data = '{"receiver_uid":["58a43a3e3fbbf3.61108490","58a43be07a3bc3.90311110","58da53ab5ce8d6.84754819"]}';
$array = json_decode($data, true);
echo "<pre>";
print_r($array);
Output would be like
Array
(
[receiver_uid] => Array
(
[0] => 58a43a3e3fbbf3.61108490
[1] => 58a43be07a3bc3.90311110
[2] => 58da53ab5ce8d6.84754819
)
)
Related
This question already has answers here:
How to extract and access data from JSON with PHP?
(1 answer)
PHP JSON Specific Key To Array [duplicate]
(3 answers)
Closed 2 years ago.
I have an API that sends me the data in the format
[{"part_no":"AAA"},{"part_no":"BBB"},{"part_no":"CCC"},......{"part_no":"ZZZ"}]
I want to create an array like ["AAA", "BBB", ...., "ZZZ"] from the above array.
I know it's possible by iterating the array item by item, and appending it to a new array, but thought that there might be a better (and hopefully faster) approach.
Like C# hasLinq that does this all in a one-liner, JS has map, it is possible to do a similar thing in PHP ?
<?php
$json = '[{"part_no":"AAA"},{"part_no":"BBB"},{"part_no":"CCC"},{"part_no":"ZZZ"}]';
$data = json_decode($json, true);
$result = array_column($data, 'part_no');
var_export($result);
Output:
array (
0 => 'AAA',
1 => 'BBB',
2 => 'CCC',
3 => 'ZZZ',
)
This question already has answers here:
php object attribute with dot in name
(5 answers)
How can I access a property with an invalid name?
(1 answer)
How to access object properties with names like integers or invalid property names?
(7 answers)
Closed 3 years ago.
I need to access the value of the json that contains a point in its name.
I would like to access the "proy_sim.name" field but I do not know how
{
"prsp_sol": [
{
"proy_sim.name": "Vehículos",
"prsp_def.name": "TRACTOR"
}
]
}
After decoding with json_decode() you'll realize that there is an additional array you're not accounting for:
$json = '{
"prsp_sol": [
{
"proy_sim.name": "Vehículos",
"prsp_def.name": "TRACTOR"
}
]
}';
$decoded = json_decode($json, true); // true makes it an array
print_r($decoded);
echo $decoded['prsp_sol'][0]['proy_sim.name'];
//-----------------------^ additional nested array
The output:
Array
(
[prsp_sol] => Array
(
[0] => Array
(
[proy_sim.name] => Vehículos
[prsp_def.name] => TRACTOR
)
)
)
Vehículos
Here is an example
The point in the name is irrelevant.
This question already has answers here:
Converting MySQL result array to JSON [duplicate]
(2 answers)
Closed 6 years ago.
I want to convert my array value:
Array ( [page_1] => fifth [page_2] => first [page_3] => fourth [page_4] => third )
Into JSON format is given below
{s:6:"page_1";s:5:"third";s:6:"page_2";s:5:"first";s:6:"page_3";s:6:"fourth";s:6:"page_4";s:5:"fifth";}
Can anyone please help me
You want to serialize you array.
You need to use serialize()
<?php
$a = array (
'page_1' => 'fifth',
'page_2' => 'first',
'page_3' => 'fourth',
'page_4' => 'third');
echo serialize($a);
// Outputs: a:4:{s:6:"page_1";s:5:"fifth";s:6:"page_2";s:5:"first";s:6:"page_3";s:6:"fourth";s:6:"page_4";s:5:"third";}
?>
$json = json_encode($array);
and otherwise
$array = json_decode($json, true);
When I insert the value in table it is inserting like
s:107:"a:4:{s:6:"page_1";s:5:"third";s:6:"page_2";s:5:"first";s:6:"page_3";s:6:"fourth";s:6:"page_4";s:5:"fifth";}";
don't know why cause when i display it that is right but in table it is inserting something like above
This question already has answers here:
Get the first element of an array
(39 answers)
multidimensional array
(3 answers)
Closed 9 years ago.
I have this:
Array
(
[28] => Array
(
[name] => HTC Touch HD
)
)
There's only one array inside the main array and I only the value of name. Problem is that I don't know the index (28).
You could use array_values just in general to get rid of any weird keys:
$normal = array_values($arr);
$normal[0]['name']
Or in this particular case, end, which is only a little bit hacky:
end($normal)['name']
http://codepad.viper-7.com/cApBjK
(Yep, reset and first and such work too.)
You could also just use
$array = array_pop($array);
And then to get the name element:
$array['name']
You can try something like this:
reset($outerArray);
$innerArray = current($outerArray);
Now you should have access to the value you want.
Pretty self-explanatory :)
<?php
$array = array(
28 => array(
'name' => 'HTC Touch HD'
)
);
$key = current(array_keys($array));
echo '<pre>';
print_r($array[$key]);
echo '</pre>';
?>
If you don't know the structure of an array, you can use foreach construct.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert a string into list of arrays
I have a string code=AA&price=10&user_id=5.initially i exploded first with & and then by = but i getting.
array(
[0] =>code
[0] =>AA
[0] =>price
[0] =>10
[0] =>user_id
[0] =>5
)
My aim is to show
array(
[code] => AA
[price] => 10
[user_id] => 5
)
parse_str($str, $values);
var_dump($values);
You are parsing a URL encoded format, use the already existing parse_str to parse it.
$string = 'code=AA&price=10&user_id=5';
$params = array();
parse_str($string, $params);
print_r($params);
http://php.net/manual/en/function.parse-str.php
parse_str — Parses the string into variables
parse_str('code=AA&price=10&user_id=5', $outputArray);