I want to get ItemCategory->id's for each Item->id
How can I do it in the best way?
Here is a part of JSON data item
"6": {
"Item": {
"id": "6",
"name": "test",
"description": "description",
},
"ItemThumbnail": null,
"ItemCategory": {
"3": {
"id": "3",
"name": "name",
"status": "active",
"date_created": "2015-07-07 11:23:52",
"date_updated": "0000-00-00 00:00:00",
},
"4": {
"id": "4",
"name": "name",
"status": "active",
"date_created": "2015-07-07 11:23:52",
"date_updated": "0000-00-00 00:00:00",
}
},
"ItemGroup": []
},
You can convert your json string to a PHP array using $array = json_decode($str, true);, then loop your array and extract the information you need.
See http://php.net/manual/en/function.json-decode.php for more detail on json_decode
After json decode use this.
foreach($Item as $Items)
{
$ItemCategory = $Items->ItemCategory;
foreach($ItemCategory as $ItemCategorys)
{
echo $ItemCategorys->id;
}
}
Related
I'm pretty much self teaching myself to code and having a little play with creating a scraper app. I have an array I'm using in an json api but I need to flatten the structure so I can easily consume it elsewhere.
I'm really struggling to fathom out what I need to do here. I've read a load of other questions which are similar requests, but the array formats never match my format so I don't actually know how to tackle this.
This is the current array structure
{
"result": [
{
"Boot buddy": {
"id": "2",
"groupno": "1",
"urlsource": "https://www.amazon.co.uk/Boot-Buddy-fastest-simplest-footwear/dp/B014UPAHO4?pd_rd_wg=lVVK6&pd_rd_r=bf1ba871-fb59-4c66-a146-e94dde7c8e6d&pd_rd_w=gWC2F&ref_=pd_gw_ri&pf_rd_r=W68MX1TXFDDJ8Q8Z08CP&pf_rd_p=cecd4520-32f6-5499-ae19-cd4e83816acd",
"name": "Boot buddy",
"date": "2019-04-14 16:00:29.595,2019-04-14 21:50:31.362,2019-04-14 21:54:11.184",
"price": "£14.99,£14.99,£14.99"
},
"Amazon echo": {
"id": "1",
"groupno": "1",
"urlsource": "https://www.amazon.co.uk/dp/B07CH6JKW3/ref=gw_uk_desk_h1_aucc_cp_mp?pf_rd_p=e4e5a2e6-ddbd-473a-a5fb-e8cc09a11f88&pf_rd_r=1MN25BRXY8YDQ4TBK4X6",
"name": "Amazon echo",
"date": "2019-04-14 16:00:29.595,2019-04-14 21:50:31.362,2019-04-14 21:54:11.184",
"price": "£14.99,£14.99,£14.99"
}
}
]
}
I want to achieve this
{
"result": [
{
"id": "2",
"groupno": "1",
"urlsource": "https://www.amazon.co.uk/Boot-Buddy-fastest-simplest-footwear/dp/B014UPAHO4?pd_rd_wg=lVVK6&pd_rd_r=bf1ba871-fb59-4c66-a146-e94dde7c8e6d&pd_rd_w=gWC2F&ref_=pd_gw_ri&pf_rd_r=W68MX1TXFDDJ8Q8Z08CP&pf_rd_p=cecd4520-32f6-5499-ae19-cd4e83816acd",
"name": "Boot buddy",
"date": "2019-04-14 16:00:29.595,2019-04-14 21:50:31.362,2019-04-14 21:54:11.184",
"price": "£14.99,£14.99,£14.99"
},
{
"id": "1",
"groupno": "1",
"urlsource": "https://www.amazon.co.uk/dp/B07CH6JKW3/ref=gw_uk_desk_h1_aucc_cp_mp?pf_rd_p=e4e5a2e6-ddbd-473a-a5fb-e8cc09a11f88&pf_rd_r=1MN25BRXY8YDQ4TBK4X6",
"name": "Amazon echo",
"date": "2019-04-14 16:00:29.595,2019-04-14 21:50:31.362,2019-04-14 21:54:11.184",
"price": "£14.99,£14.99,£14.99"
}
]
}
This is the code I'm using to generate the array which then gets converted into json for use in the api. I'm iterating over a previous call to the database to merge duplicate records together but keep the unique date and price data. So maybe there's a way to change this code to get the output I'm after?
$records=array();
$records[result]=array();
foreach ($products_arr[records] as $key => $value) {
$hash = $value['name'];
if(isset($result[$hash])){
$result[$hash]['date'] .= ",{$value['date']}";
$result[$hash]['price'] .= ",{$value['price']}";
}else{
$result[$hash] = $value;
}
}
array_push($records[result], $result);
Any help appreciated!!
Like that:
$json=<<<'EOD'
{
"result": [
{
"Boot buddy": {
"id": "2",
"groupno": "1",
"urlsource": "https://www.amazon.co.uk/Boot-Buddy-fastest-simplest-footwear/dp/B014UPAHO4?pd_rd_wg=lVVK6&pd_rd_r=bf1ba871-fb59-4c66-a146-e94dde7c8e6d&pd_rd_w=gWC2F&ref_=pd_gw_ri&pf_rd_r=W68MX1TXFDDJ8Q8Z08CP&pf_rd_p=cecd4520-32f6-5499-ae19-cd4e83816acd",
"name": "Boot buddy",
"date": "2019-04-14 16:00:29.595,2019-04-14 21:50:31.362,2019-04-14 21:54:11.184",
"price": "£14.99,£14.99,£14.99"
},
"Amazon echo": {
"id": "1",
"groupno": "1",
"urlsource": "https://www.amazon.co.uk/dp/B07CH6JKW3/ref=gw_uk_desk_h1_aucc_cp_mp?pf_rd_p=e4e5a2e6-ddbd-473a-a5fb-e8cc09a11f88&pf_rd_r=1MN25BRXY8YDQ4TBK4X6",
"name": "Amazon echo",
"date": "2019-04-14 16:00:29.595,2019-04-14 21:50:31.362,2019-04-14 21:54:11.184",
"price": "£14.99,£14.99,£14.99"
}
}
]
}
EOD;
$arr = json_decode($json, true);
$arr['result'] = array_values($arr['result'][0]);
$result = json_encode($arr);
In the orginal JSON string, the first level of the result key is an array with a single indexed item that gives once decoded to a multidimensionnal array the index 0. To turn all keys contained at this level to indexes, you only need to use the array_values PHP function.
This should work
$json = '{
"result": [
{
"Boot buddy": {
"id": "2",
"groupno": "1",
"urlsource": "https://www.amazon.co.uk/Boot-Buddy-fastest-simplest-footwear/dp/B014UPAHO4?pd_rd_wg=lVVK6&pd_rd_r=bf1ba871-fb59-4c66-a146-e94dde7c8e6d&pd_rd_w=gWC2F&ref_=pd_gw_ri&pf_rd_r=W68MX1TXFDDJ8Q8Z08CP&pf_rd_p=cecd4520-32f6-5499-ae19-cd4e83816acd",
"name": "Boot buddy",
"date": "2019-04-14 16:00:29.595,2019-04-14 21:50:31.362,2019-04-14 21:54:11.184",
"price": "£14.99,£14.99,£14.99"
},
"Amazon echo": {
"id": "1",
"groupno": "1",
"urlsource": "https://www.amazon.co.uk/dp/B07CH6JKW3/ref=gw_uk_desk_h1_aucc_cp_mp?pf_rd_p=e4e5a2e6-ddbd-473a-a5fb-e8cc09a11f88&pf_rd_r=1MN25BRXY8YDQ4TBK4X6",
"name": "Amazon echo",
"date": "2019-04-14 16:00:29.595,2019-04-14 21:50:31.362,2019-04-14 21:54:11.184",
"price": "£14.99,£14.99,£14.99"
}
}
]
}';
$arr = (array) json_decode($json)->result[0];
foreach($arr as $data){
$newArr['result'][] = $data;
}
echo json_encode($newArr);
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I've got basic array from my api:
{ "title": "offer title", "body": "offer body",
"specialities": { "lang": "en", "id": "1", "icon": "0",
"name": "speciality name 1" }, "region": "region1" }
I want to get id value from request for my php variable lets say: $idVariable. How can I do it?
I tried something like:
$idVariable = $request->specialities[0]->id
but it seems not working. What is the right way?
Then how should I work with the arrays of object in this case:
{ "title": "offer title", "body": "offer body",
"specialities": [
{ "lang": "en", "id": "1", "icon": "0", "name": "speciality name 1" },
{ "lang": "en", "id": "2", "icon": "0", "name": "speciality name 2" },
{ "lang": "en", "id": "2", "icon": "0", "name": "speciality name 3" },
etc...], "region": "region1" }
To get id's of every object in specialities array? I know that it could be a duplicate question, but I ask for just a basic example.
I tried to use json decode like below:
json_decode($request->get('specialties'))->id
edit:
The almost-right way to do it is to decode json file first:
$data = json_decode($request);
and then get the right property from the array:
$id = $data['specialities'][0]['id'];
the problem now is that id is a string not an integer and by simply using:
$int_id = intval($id)
I've got $int_id = 0 instead of 1,2,3 etc
You are getting a response from API in JSON you should use json_decode() and then use the data. Try this code.
$json = '{ "title": "offer title", "body": "offer body",
"specialities": { "lang": "en", "id": "1", "icon": "0",
"name": "speciality name 1" }, "region": "region1" }';
$data = json_decode($json);
echo $data->specialities->id;
My JSON output is like:
{
"0": "1",
"1": "araer",
"2": "aeraer",
"3": "aeraer",
"4": "News/Magzine Website",
"5": "2016-01-22 13:15:56",
"6": "2016-01-22 13:15:56",
"id": "1",
"name": "araer",
"email": "aeraer",
"url": "aeraer",
"web": "News/Magzine Website",
"created_at": "2016-01-22 13:15:56",
"updated_at": "2016-01-22 13:15:56"
}, {
"0": "2",
"1": "asd",
"2": "asd",
"3": "sfd",
"4": "sdf",
"5": "2016-02-10 13:06:28",
"6": "0000-00-00 00:00:00",
"id": "2",
"name": "asd",
"email": "asd",
"url": "sfd",
"web": "sdf",
"created_at": "2016-02-10 13:06:28",
"updated_at": "-0001-11-30 00:00:00"
}
The code I am using in Model:
public function getBlog() {
try {
$result = $this - > get();
return $result;
} catch (Exception $ex) {
return array();
}
return array();
}
Check PDO fetch style in your config/databse.php array. If it is not present there then it will use PDO::FETCH_BOTH (by default) which returns both associative and numeric values. To get only associative value you need to set 'fetch' => PDO::FETCH_CLASS, or 'fetch' => PDO::FETCH_ASSOC
You function will retutn all records from your blog table.
And if you want to get single record then try something like
$this->get()->first();
I have the following response (I cut the extra short):
{
"meta": {
"current_page": "1",
"last_page": "1",
"per_page": "15",
"total": "1",
"from": "1",
"to": "1"
},
"Products": [
{
"archived": "0",
"committed_stock": "0",
"created_at": "2015-05-10T17:39:53+00:00",
"deleted": "0",
"description": "desc",
"id": "43061710",
"links": {
"Users": [
{
"id": "107534",
"type": "created_by"
}
],
"Attributes": [
{
"id": "31538870"
}
]
}
}
]
}
Everytime I get this response, there will only be one item in "Attributes." What is the easiest way of grabbing this value? So far I have this:
$json = json_decode($json_data);
$json = json_decode($json_data, true);
echo $json["Products"][0]["links"]["Attributes"][0]["id"];
try this:
var_dump( $json->Products[0]->links->Attributes);
the object field could be ether also an object, or an array:
refer to field: $object->object
refer to array's i cell: $object->array[i]
P.S.
please edit the json, it's missing it's end...
You may also want to try some JSON Path libs for PHP: https://github.com/Peekmo/JsonPath
So, in the very beginning, before the send_sms.php is loaded, I have this Json stored in a database:
{
"chats": {
"chat": [{
"id": "1",
"name": "Ethan Wilberforce",
"messages": {
"message": [{
"id": "1",
"name": "Ethan Wilberforce",
"text": "Hello how are you doing",
"time": "4:41"
}, {
"id": "2",
"name": "Qasim Iqbal",
"text": "Not bad. How about you?",
"time": "4:42"
}, {
"id": "3",
"name": "Ethan Wilberforce",
"text": "I'm not too bad myself.",
"time": "4:43"
}]
}
}, {
"id": "2",
"name": "Geoff Vahaaho",
"messages": {
"message": [{
"id": "1",
"name": "Geoff Vahaaho",
"text": "Hello how are you doing",
"time": "4:41"
}, {
"id": "2",
"name": "Qasim Iqbal",
"text": "Not bad. How about you?",
"time": "4:42"
}, {
"id": "3",
"name": "Geoff Vahaaho",
"text": "I'm not too bad myself.",
"time": "4:43"
}, {
"id": "4",
"name": "Qasim Iqbal",
"text": "Nice.",
"time": "4:43"
}]
}
}]
}
}
The Json is completely valid, no errors. It is storing two chats, with messages in them.
Now, here is the PHP code that alters the Json:
$data = $user->data;
$parsed_data = json_decode($data);
...
for($i = 0, $size = sizeof($parsed_data->chats->chat); $i < $size; ++$i) {
if($parsed_data->chats->chat[$i]->name == $to) {
$found = true;
$parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)] = new stdClass;
$parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)]->id = sizeof($parsed_data->chats->chat[$i]->messages->message);
$parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)]->name = $user->name;
$parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)]->text = $message;
$parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)]->time = $time;
echo "done. ";
break;
}
}
What I intend this to do is, to add another stdClass object in the "message" array of the chat. So I basically do just that, hoping it will work.
Now, it works, kind of, but here is the new Json after we json_encode it:
{
"chats": {
"chat": [{
"id": "1",
"name": "Ethan Wilberforce",
"messages": {
"message": [{
"id": "1",
"name": "Ethan Wilberforce",
"text": "Hello how are you doing",
"time": "4:41"
}, {
"id": "2",
"name": "Qasim Iqbal",
"text": "Not bad. How about you?",
"time": "4:42"
}, {
"id": "3",
"name": "Ethan Wilberforce",
"text": "I'm not too bad myself.",
"time": "4:43"
}, {}, {
"id": 4
}, {
"name": "Qasim Iqbal"
}, {
"text": "Hello i am testing"
}, {
"time": 1326066200
}]
}
}, {
"id": "2",
"name": "Geoff Vahaaho",
"messages": {
"message": [{
"id": "1",
"name": "Geoff Vahaaho",
"text": "Hello how are you doing",
"time": "4:41"
}, {
"id": "2",
"name": "Qasim Iqbal",
"text": "Not bad. How about you?",
"time": "4:42"
}, {
"id": "3",
"name": "Geoff Vahaaho",
"text": "I'm not too bad myself.",
"time": "4:43"
}, {
"id": "4",
"name": "Qasim Iqbal",
"text": "Nice.",
"time": "4:43"
}]
}
}]
}
}
You will notice it was indeed added in the "Ethan Wilberforce" chat, but each string in the stdClass was converted to its own array item in the "message" array. How could I fix this problem? Many thanks.
Your problem is this:
$parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)] = new stdClass;
$parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)]->id = sizeof($parsed_data->chats->chat[$i]->messages->message);
It basically amounts to:
$array[$last] = new stdClass();
$array[$last+1] = "id";
$array[$last+2] = "name";
You keep appending new arrays/objects, because you use sizeof(...) which always becomes one larger than the previous line. It's not the last index, but the size. Which is $lastindex+1.
What you should be doing anyway, is not using an object, but just appending an array, and with all its attributes at once:
$parsed_data->chats->chat[$i]->messages->message[] = array(
"id" => ...,
"name" => ...,
"time" => ...,
);
When you encode that associative array back into JSON it will also become a normal {...} JSON object group.