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
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:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 3 years ago.
Am using chart.js to display charts on my site. The working data attribute for chart.js is in this format
"data" => ['1', '2', '3']
I have my data currently in a PHP array
Array ( [0] => 1 [1] => 2 [2] => 3 )
However this is not working for my needs. How can I convert the PHP array into the same format as above?
I have tried
json_encode($myArray)
and
implode($myArray)
without success.
Any suggestions?
You could create an array with key "data" and another array as the value;
$myArray = ["data" => [1, 2, 3]];
echo json_encode($myArray);
Output
{"data":[1,2,3]}
Or put the values between quotes
$myArray = ["data" => ["1", "2", "3"]];
Output
{"data":["1","2","3"]}
Not sure completely understood the problem. But if you want to convert values of php array to a json you can do something like that:
json_encode(array_values($myArray));
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
)
)
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:
unserialize problem
I have a string in the form of:
a:16:{i:0;s:3:"696";i:1;s:3:"698";i:2;s:3:"690";}"
I am looking at turning this back into array, so that it will be along the lines of:
array(16) {
0 => 696,
1 => 698,
2 => 690
}
Any ideas how to do this?
Thanks
It looks like a serialized PHP string, try
$array = unserialize($value);
Manual: http://php.net/manual/en/function.unserialize.php
Update
The string contains a flaw, as it expects a array of 16 elements, but only 3 given.
Consider:
$a = array (
0 => '696',
1 => '698',
2 => '690'
);
$s = serialize($a);
will result in:
"a:3:{i:0;s:3:"696";i:1;s:3:"698";i:2;s:3:"690";}"
Use the unserialize() function.
$array = unserialize($serialized_string);