I am new to php so, need any help for this issue
I tried to run query
localhost/ls-test/_design/app-views/_view/timestamp-measurement?descending=true&include_docs=true
and get json return something like this format:
{"total_rows":441740,"offset":0,"rows":[
{"id":"123","key":[2013,2],"value":["1","PowerConsumption","0.0"],"doc":{"_id":"5446691910660799","SourceType":"SENSOR","SourceId":"4294403072","Property":"PowerConsumption","value":"0.0","}},
{"id":"124","key":[2013,2],"value":["228224","Motion","false"],"doc":{"_id":"5446688472521031","SourceType":"SENSOR","SourceId":"228224","Property":"Motion","value":"false","timestamp":"2013-02-12 17:06:06.768"}}
...]}
so, how do I further query it in PHP that my result only displayed based on specific value e.g only list of documents with SourceId:228224
You would json_decode in PHP then work with your associative array.
http://php.net/manual/en/function.json-decode.php
Related
Good evening,
This is probably a stupid question but I've been fiddling with this for a while now. I have a string coming from an AJAX call. To inspect the actual string that gets sent to my PHP from some JavaScript I put it into the result and spit it out in the front end. The string looks like this:
count: "[{\"cartKey\":\"d9d4f495e875a2e075a1a4a6e1b9770f\",\"qty\":\"3\"},
{\"cartKey\":\"67c6a1e7ce56d3d6fa748ab6d9af3fd7\",\"qty\":\"2\"},
{\"cartKey\":\"f7177163c833dff4b38fc8d2872f1ec6\",\"qty\":\"32\"}]"
So! My problem is getting this to be an actual PHP array. If I do this:
$result['count'] = json_decode($updates, true);
inside my PHP, the result is 0.
Ignore the count name on the result. I'm just trying to turn the above string into an array of objects I can use in PHP rather than a JSON string.
Thanks in advance!
I'm a birk.
The actual value of $updates was null, I think.
I have just solved my problem by not calling JSON.stringify on the data sent from the AJAX call.
The value now comes through and is actually decoded as if by magic within the PHP script.
Thanks for taking a look.
Ash
Hi this is the code i am following in my project.
$reports = $this->curl->simple_get('url');
echo is_array($reports)?"Is Array":"Not Array";exit;
It's giving Not Array as output.
I want to convert that into associative array.
The data you are getting is probably not an array, but a string containing an array structure, e.g. output by print_r(). This kind of data will not automatically be converted back into a PHP array.
To use this you can use a similar solution as brought out here:
Create variable from print_r output
it describes the print_r_reverse function that's brought out in php.net page.
how ever - this is kind of an ugly hack. I would suggest to change the page content and use json_encode() in the "url" page, and parse the content using json_decode()
I want to return values I retrieve from the db using group_concat as an array of data. Is it possible to do this in the mysql query? Or do I need to explode the data into an array?
GROUP_CONCAT(sh.hold_id) as holds
returns this
[holds] => 3,4
I want it to return:
[holds] => array(3,4)
As I said in my comment: you need to explode the data into an array, using php code like this:
$holds = explode(',', $holds);
because mysql has no concept of array-type for data.
This is possible since MySQL 5.7.22 using the JSON_ARRAYAGG() method
Read more: https://dev.mysql.com/doc/refman/5.7/en/aggregate-functions.html#function_json-arrayagg
Example:
SELECT JSON_ARRAYAGG(category.slug) as categories From categories
If you need to do it on the MySQL level, and then you may probably parse it to an object.
You can do the following
SELECT CONCAT("[", GROUP_CONCAT(category.name), "]") AS categories
From categories
MySQL has no concept of arrays. Therefore it is not able to return an array. It is up to your processing code (here the php scripts) to convert the concatenated notation into a php array.
It is possible to return mysql JSON array like so,
json_array(GROUP_CONCAT(sh.hold_id)) as holds
Refer docs for further info.
I currently have json using json_encode from a mysql query which looks like this:
{"post_2":{"caption":"...","id":"...","accountID":"..","date":"07\/07\/2011 1:45:12 AM","title":"...","authorInfo":{"Email Address":"..."}}}, {"post_2":{"caption":"...","id":"...","accountID":"..","date":"07\/07\/2011 1:45:12 AM","title":"...","authorInfo":{"Email Address":"..."}}}
How can I have the json being an array of posts ('post_2', 'post_1') rather than it being a dictionary? The JSON will be decoded on an iPhone using SBJSON and the JSON will have to be made into an array in the backend.
Thanks in advanced.
Provide a non-associative array to json_encode(). The easiest way is usually to simply call array_values() on the (associative) array, and encode the result.
Take a look at PHP's json_decode function, specifically the 2nd parameter if you want an array.
I have a postgresql (V 8.4) function that returns SETOF a custom type. An example of the string output from this function is the following 4 rows:
(2,"CD,100"," ","2010-09-08 14:07:59",New,0,,,,,,"2010-09-06 16:51:51","2010-09-07 16:51:57",)
(5,CD101,asdf,"2010-08-08 14:12:00",Suspended-Screen,1,10000,,,,,,,)
(4,DNR100,asdf,"2010-09-08 14:10:31",Suspended-Investgate,0,,,,,,"2010-09-06 16:51:51","2010-09-07 16:51:57",)
(3,MNSCU100," ","2010-09-08 14:09:07",Active,0,,,,,,,,)
I need to work with this data in PHP and I'm trying to figure out the best way to work with it. What I would love is if there was a way for postgresql to return this like a table where columns represent each value within a record rather than as a comma-separated string.
Is this possible? If not, what is the best way to work with this comma-separated string of values in PHP?
I've see this post (Convert PostgreSQL array to PHP array) and can use the function mentioned there but I wanted to ask if anyone has other ideas or suggestions.
Thanks,
Bart
There's str_getcsv() which'll parse a string as CSV data and return an array of the individual fields
Yep, its real easy, just change the way you are calling the function.
Instead of
SELECT my_srf(parm1);
Do either:
SELECT * FROM my_srf(parm1);
SELECT (my_srf(parm1)).*;
You'll even get the column names out this way.