Related
Hi I have this array code and I need to get: user_id": 4
I just need to get the value 4 from user_id
I have this code but it doesn't work
$myvar = $request['order'][0]['user_id'];
{
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL3NpdGUudGVzdC9wdWJsaWMvYXBpL2xvZ2luIiwiaWF0IjoxNjIzODExMTE1LCJuYmYiOjE2MjM4MTExMTUsImp0aSI6Ind2bExlTlJHdkdpU2xmVUYiLCJzdWIiOjIsInBydiI6Ijg3ZTBhZjFlZjlmZDE1ODEyZmRlYzk3MTUzYTE0ZTBiMDQ3NTQ2YWEifQ.-Q56kEsHZgVEtaMzSS9Ub11imYjWjFbJMAug3Wx7WTg",
"user": {
"success": true,
"data": {
"id": 2,
"auth_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL3NpdGUudGVzdC9wdWJsaWMvYXBpL2xvZ2luIiwiaWF0IjoxNjIzODExMTE1LCJuYmYiOjE2MjM4MTExMTUsImp0aSI6Ind2bExlTlJHdkdpU2xmVUYiLCJzdWIiOjIsInBydiI6Ijg3ZTBhZjFlZjlmZDE1ODEyZmRlYzk3MTUzYTE0ZTBiMDQ3NTQ2YWEifQ.-Q56kEsHZgVEtaMzSS9Ub11imYjWjFbJMAug3Wx7WTg",
"name": "Leo",
"email": "ventas#nikostore.net",
"phone": "+51943363373",
"default_address_id": 3,
"default_address": {
"address": "mi dirección",
"house": null,
"latitude": "-1.327080900000001",
"longitude": "-10.6348053",
"tag": null
},
"delivery_pin": "51972",
"wallet_balance": 0,
"avatar": null,
"tax_number": null
},
"running_order": null,
"delivery_details": null
},
"order": [{
"selectedaddons": [{
"addon_category_name": "Comida Marina Ingredientes",
"addon_id": "2",
"addon_name": "Cangrejo Adicional",
"price": "32.00"
}],
"id": 5,
"restaurant_id": 12,
"item_category_id": 2,
"name": "Producto 1 Loquillo",
"price": "423.00",
"old_price": "0.00",
"image": "https://localhost/phpmyadmin/themes/pmahomme/img/logo_left.png",
"is_recommended": 1,
"is_popular": 1,
"is_new": 1,
"desc": "<p>321312</p>",
"placeholder_image": null,
"is_active": 1,
"is_veg": null,
"order_column": null,
"addon_categories": [{
"id": 2,
"name": "Comida Marina Ingredientes",
"type": "SINGLE",
"user_id": 4,
"created_at": "2021-06-10 20:18:37",
"updated_at": "2021-06-10 20:39:18",
"description": null,
"pivot": {
"item_id": 5,
"addon_category_id": 2
},
"addons": [{
"id": 2,
"name": "Cangrejo Adicional",
"price": "32.00",
"addon_category_id": 2,
"user_id": 4,
"created_at": "2021-06-10 20:20:30",
"updated_at": "2021-06-10 20:38:51",
"is_active": 1
}, {
"id": 3,
"name": "Pene",
"price": "95.00",
"addon_category_id": 2,
"user_id": 4,
"created_at": "2021-06-10 23:52:53",
"updated_at": "2021-06-10 23:52:53",
"is_active": 1
}]
}],
"quantity": 1
}],
"coupon": null,
"location": {
"lat": "-5.187080900000001",
"lng": "-80.6348053",
"address": "midreccion",
"house": null,
"tag": null
},
"order_comment": null,
"total": {
"productQuantity": 1,
"totalPrice": 455
},
"method": "COD",
"payment_token": "",
"delivery_type": 1,
"partial_wallet": false,
"dis": 0,
"pending_payment": false,
"tipAmount": null
}
Working from the JSON string you posted here, you need to decode it first with json_decode(). Assuming no flags are set, this will decode to a mix of objects and arrays as represented in the JSON string.
$str = '{"token": "ey...WTg",
"user": {
...
}
}'; //The JSON string you posted in the question
$request = json_decode($str);
From there the path to the user_id is:
$request->order[0]->addon_categories[0]->user_id // 4
Use foreach loop.
Assuming that you have stored array in variable $array.
Code :
foreach($array as $arr)
{
if($arr['user_id'] == 4)
{
$user = $arr;
}
}
You will get the entire object which has user_id 4, you can get related column by
$user_id = $user['user_id];
Hope this will be useful.
I am working on restfull api's for mobile app development. I am returning an php array in json which is like,
"data": {
"Waxing": [
{
"id": "119",
"provider_id": "54",
"provider_service_id": "0",
"category_id": "15",
"date": "09-06-2018",
"time": "08.00 AM,10.00 AM,11.00 AM,12.00 PM,01.00 PM,02.00 PM,03.00 PM,04.00 PM,05.00 PM,06.00 PM,07.00 PM",
"repeat": "D",
"deleted_at": null,
"created_at": "2018-06-09 05:22:47",
"updated_at": "2018-06-09 14:22:04",
"setdate": "09 Jun",
"name": "Waxing"
}
],
"Massage": [
{
"id": "145",
"provider_id": "54",
"provider_service_id": "0",
"category_id": "4",
"date": "14-06-2018",
"time": "08.00 AM,09.00 AM,10.00 AM,11.00 AM,12.00 PM,01.00 PM,02.00 PM,03.00 PM,04.00 PM,05.00 PM,06.00 PM,07.00 PM",
"repeat": "O",
"deleted_at": null,
"created_at": "2018-06-13 14:24:15",
"updated_at": "2018-06-13 14:24:15",
"setdate": "14 Jun",
"name": "Massage"
}
]
},
and creating this output with these line of code:
foreach ($getproviderdatetime as $getproviderdatetimes){
$categorycheck = Category::where('id', '=', $getproviderdatetimes->category_id)->first();
if(count($categorycheck)>0){
$alldata[$categorycheck->name][]=$getproviderdatetimes;
}
}
but now I need data in this format:
"data": {
[
"categoryname": "Waxing",
"services": [
{
"id": "119",
"provider_id": "54",
"provider_service_id": "0",
"category_id": "15",
"date": "09-06-2018",
"time": "08.00 AM,10.00 AM,11.00 AM,12.00 PM,01.00 PM,02.00 PM,03.00 PM,04.00 PM,05.00 PM,06.00 PM,07.00 PM",
"repeat": "D",
"deleted_at": null,
"created_at": "2018-06-09 05:22:47",
"updated_at": "2018-06-09 14:22:04",
"setdate": "09 Jun",
"name": "Waxing"
}
]
],
[
"categoryname": "massage",
"services": [
{
"id": "145",
"provider_id": "54",
"provider_service_id": "0",
"category_id": "4",
"date": "14-06-2018",
"time": "08.00 AM,09.00 AM,10.00 AM,11.00 AM,12.00 PM,01.00 PM,02.00 PM,03.00 PM,04.00 PM,05.00 PM,06.00 PM,07.00 PM",
"repeat": "O",
"deleted_at": null,
"created_at": "2018-06-13 14:24:15",
"updated_at": "2018-06-13 14:24:15",
"setdate": "14 Jun",
"name": "Massage"
}
]
]
},
I have array of services and getting category name from database. I am using Laravel framework and returning data in json format. How can I achive this. Need help.
Here's the solution:
foreach ($getproviderdatetime as $getproviderdatetimes){
$categorycheck = Category::where('id', '=', $getproviderdatetimes->category_id)->first();
if(count($categorycheck)>0){
// append new item to all data:
$alldata[] = [
"categoryname" => $categorycheck->name,
"services" => [$getproviderdatetimes],
];
}
}
$testing = []
foreach ($alldata as $key=>$all){
array_push($testing,["categoryname"=>$key,"services"=>$all]);
}
print_r($testing);
Loop again ur $alldata..Bellow is ur solution
https://3v4l.org/9iT9l
I've been struggling around this for a while now and can't seem to get the right algorithm.
What I want is to sort this:
$variable = '{
"0": {
"id": "0",
"code": "main_colour",
"label": "Main Colour",
"options": [{
"id": "825",
"label": "White",
"products": ["1", "2", "3"]
}, {
"id": "840",
"label": "Cream",
"products": ["3", "4", "5"]
}],
"position": "0"
},
"2": {
"id": "0",
"code": "size",
"label": "Size",
"options": [{
"id": "825",
"label": "S",
"products": ["1", "2", "3"]
}, {
"id": "840",
"label": "M",
"products": ["3", "4", "5"]
}],
"position": "0"
}
}';
And the output would be
$variable = '{
"0": {
"id": "0",
"code": "main_colour",
"label": "Main Colour",
"options": [{
"id": "840",
"label": "Cream",
"products": ["3", "4", "5"]
},{
"id": "825",
"label": "White",
"products": ["1", "2", "3"]
}],
"position": "0"
},
"2": {
"id": "0",
"code": "size",
"label": "Size",
"options": [{
"id": "840",
"label": "M",
"products": ["3", "4", "5"]
}, {
"id": "825",
"label": "S",
"products": ["1", "2", "3"]
}],
"position": "0"
}
}';
So basically it should be sorted using the label inside the "options" without affecting the other fields. I've been trying usort and tried different algorithms but to no avail.
Current code that sorts only the outside "label" and not the inner label:
function optionsSort($a, $b) {
return strcmp($a['options'][0]['label'], $b['options'][0]['options']['label']);
}
usort($variable, "optionsSort"));
Here's the code:
// your array
$variable = '{
"0": {
"id": "0",
"code": "main_colour",
"label": "Main Colour",
"options": [{
"id": "825",
"label": "White",
"products": ["1", "2", "3"]
}, {
"id": "840",
"label": "Cream",
"products": ["3", "4", "5"]
}],
"position": "0"
},
"2": {
"id": "0",
"code": "size",
"label": "Size",
"options": [{
"id": "825",
"label": "S",
"products": ["1", "2", "3"]
}, {
"id": "840",
"label": "M",
"products": ["3", "4", "5"]
}],
"position": "0"
}
}';
$a = json_decode($variable);
foreach ($a as $item) {
// sorting function
usort($item->options, function($a, $b) { return strcmp($a->label, $b->label); });
}
I have a json tree with categories,
I would like to obtain the last level of elements for each different category.
For example on this json:
[
{
"category_id": "3",
"parent_id": "2",
"name": "Women",
"categories": [
{
"category_id": "11",
"parent_id": "3",
"name": "Clothing",
"categories": [
{
"category_id": "30",
"parent_id": "11",
"name": "T-shirts",
"categories": null
},
{
"category_id": "33",
"parent_id": "11",
"name": "jeans",
"categories": null
}
]
}
]
},
{
"category_id": "5",
"parent_id": "2",
"name": "Footwear ",
"categories": [
{
"category_id": "15",
"parent_id": "5",
"name": "Rings",
"categories": [
{
"category_id": "51",
"parent_id": "15",
"name": "Small Leathers",
"categories": null
}
]
},
{
"category_id": "16",
"parent_id": "5",
"name": "Bands",
"categories": [
{
"category_id": "41",
"parent_id": "16",
"name": "boots",
"categories": null
}
]
},
{
"category_id": "48",
"parent_id": "5",
"name": "Bracelets",
"categories": [
{
"category_id": "55",
"parent_id": "48",
"name": "Cocktail",
"categories": null
}
]
}
]
}
]
The result would be an array (T-shirts, Jeans, Small Leathers, boots, cocktail)
What I was thinking is to decode it on an array and search filter the array with all the categories that are null, but I'm not sure if it's the best option because the object have different levels.
(I'm sorry for the English)
Json string is not a valid. It can be made valid by enclosing the json string in {}.
$json = '{"categories": [
{
"category_id": "3",
"parent_id": "2",
"name": "Women",
"categories": [
{
"category_id": "11",
"parent_id": "3",
"name": "Clothing",
"categories": [
{
"category_id": "30",
"parent_id": "11",
"name": "T-shirts",
"categories": null
},
{
"category_id": "33",
"parent_id": "11",
"name": "jeans",
"categories": null
}
]
}
]
},
{
"category_id": "5",
"parent_id": "2",
"name": "Footwear ",
"categories": [
{
"category_id": "15",
"parent_id": "5",
"name": "Rings",
"categories": [
{
"category_id": "51",
"parent_id": "15",
"name": "Small Leathers",
"categories": null
}
]
},
{
"category_id": "16",
"parent_id": "5",
"name": "Bands",
"categories": [
{
"category_id": "41",
"parent_id": "16",
"name": "boots",
"categories": null
}
]
},
{
"category_id": "48",
"parent_id": "5",
"name": "Bracelets",
"categories": [
{
"category_id": "55",
"parent_id": "48",
"name": "Cocktail",
"categories": null
}
]
}
]
}
]}';
Decode the json string in to php opject
$json_object = json_decode($json);
Use this recursive function to fetch get all the last level categories in a array.
function getLastCategories($object){
$last_categories = array();
if(is_array($object->categories)){
foreach($object->categories as $categories){
$last_categories = array_merge( $last_categories, getLastCategories($categories));
}
}else{
$last_categories[]=$object->name;
}
return $last_categories;
}
print_r(getLastCategories($json_object));
Output:
Array
(
[0] => T-shirts
[1] => jeans
[2] => Small Leathers
[3] => boots
[4] => Cocktail
)
You can do this using recursive iterators.
$categories = json_decode($json);
// create the iterator
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($categories));
// iterate recursively
foreach ($iterator as $key => $subcategories) {
// if the 'categories' key is empty for the current level...
if ($key == 'categories' && !$subcategories) {
// then add the value of the 'name' key for the current level to your result array.
$result[] = $iterator->getSubIterator()->offsetGet('name');
}
}
This may not be the most efficient way to do it, but it makes the code pretty simple.
I have an array that has values that look like:
{
"1": {
"id": "1",
"user_id": "9",
"post_id": "61",
"parent_id": "0",
"message": "Level 0.",
"created_at": "2015-06-27 12:46:18",
"updated_at": "-0001-11-30 00:00:00",
"children": []
},
"2": {
"id": "2",
"user_id": "9",
"post_id": "61",
"parent_id": "1",
"message": "Level 1.",
"created_at": "2015-06-27 12:46:22",
"updated_at": "-0001-11-30 00:00:00",
"children": []
}
}
I then have a loop that looks like:
foreach ($tree as $key => &$value)
{
if ($value->parent_id != 0)
{
$tree[$value->parent_id]->children[] = &$value;
}
}
This, however, returns the following error:
Indirect modification of overloaded property Comment::$children has no effect
Why? And how can I fix this?