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));
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 an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
I have this below JsonArray send from API, I want to save it to the database, but I can't get the data of the Array, always is a blank.
[{
"user": 7,
"states": true
}, {
"user": 13,
"states": true
}, {
"user": 10,
"states": false
}]
This is what I tried to get the JsonArray
if($_SERVER['REQUEST_METHOD']=='POST'){
$jsondata = json_decode(file_get_contents("php://input"),true);
$user= $jsondata['user'];
$status = $jsondata['states'];
$Query = "insert table(user,states,)
values ('$user','$status ')";
}
Thank You
$jsondata consist on multi dimensional array, your array looks like:
Array
(
[0] => Array
(
[user] => 7
[states] => 1
)
[1] => Array
(
[user] => 13
[states] => 1
)
[2] => Array
(
[user] => 10
[states] =>
)
)
And you are trying to insert without using any specific index or not using any loop.
Solution is that, you need to use loop here to store multiple rows in your database.
Second solution is that, you can store this data as same as it is, means, save data in json format in one column rather then to store in multiple rows and multiple column, whenever you need to fetch record or need to show your data, you can just use json_decode().
Side note: i hope (user,states,) last comma is just a typo here.
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
how to read "test" in "dir"?
$_USERS = array (
10 => array (
`name` => `xyz`,
`role` => `user`,
`dir` => `["test","test1"]`,),
);
Assuming you are creating this array statically in your script, I would advise against using the ` quotation. Instead use a single or double quotation.
The below example works the way I believe you are requesting:
$_USERS = array (
10 => array (
"name" => 'xyz',
"role" => 'user',
"dir" => '["test","test1"]',),
);
echo json_decode($_USERS[10]["dir"])[0];
Because the array in "dir" is not a real array, we can translate it into a PHP array by using json_decode.
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:
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