I have an array of objects that I need to sort by parent (from lowest to highest).
Here's the structure of the array:
[{
"term_id": 9,
"name": "pve",
"slug": "pve",
"term_group": 0,
"term_taxonomy_id": 9,
"taxonomy": "post_tag",
"description": "pve guides",
"parent": 15,
"count": 0,
"filter": "raw"
}, {
"term_id": 8,
"name": "pvp",
"slug": "pvp",
"term_group": 0,
"term_taxonomy_id": 8,
"taxonomy": "post_tag",
"description": "pvp guides",
"parent": 15,
"count": 2,
"filter": "raw"
}, {
"term_id": 11,
"name": "greataxe",
"slug": "greataxe",
"term_group": 0,
"term_taxonomy_id": 11,
"taxonomy": "post_tag",
"description": "greataxe guides",
"parent": 14,
"count": 1,
"filter": "raw"
}, {
"term_id": 12,
"name": "musket",
"slug": "musket",
"term_group": 0,
"term_taxonomy_id": 12,
"taxonomy": "post_tag",
"description": "musket guides",
"parent": 14,
"count": 0,
"filter": "raw"
}, {
"term_id": 13,
"name": "spear",
"slug": "spear",
"term_group": 0,
"term_taxonomy_id": 13,
"taxonomy": "post_tag",
"description": "",
"parent": 14,
"count": 1,
"filter": "raw"
}, {
"term_id": 10,
"name": "sword",
"slug": "sword",
"term_group": 0,
"term_taxonomy_id": 10,
"taxonomy": "post_tag",
"description": "sword guides",
"parent": 14,
"count": 0,
"filter": "raw"
}, {
"term_id": 15,
"name": "Play Type",
"slug": "playtype",
"term_group": 0,
"term_taxonomy_id": 15,
"taxonomy": "post_tag",
"description": "",
"parent": 0,
"count": 0,
"filter": "raw"
}, {
"term_id": 14,
"name": "Weapons",
"slug": "weapons",
"term_group": 0,
"term_taxonomy_id": 14,
"taxonomy": "post_tag",
"description": "",
"parent": 0,
"count": 0,
"filter": "raw"
}]
I tried sorting it with the follow
$new_array = usort($tags, function($a, $b) {
return $b->parent - $a->parent;
})
My desired outcome would be an array which reorganizes the array by parent (so all the objects with a parent of 0 would be on the top). Instead, I'm getting a value of true returned.
Any help would be appreciated.
usort (http://php.net/usort) performs the sort directly on the provided array. The return value just returns boolean telling if the sorting succeeded.
Furthermore,
The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
therefore you should provide $a->parent - $b->parent to get the items sorted in an ascending order.
usort($tags, function($a, $b) {
return $a->parent - $b->parent;
});
$new_array = $tags; // The $tags is now sorted
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 days ago.
Improve this question
I need to make new array from another array variable. So I have 3 variable
$transaction
[
{
"id": 2,
"member_id": 19,
"date_start": "2023-03-14",
"created_at": "2023-02-12T10:03:46.000000Z",
"updated_at": "2023-02-12T10:03:46.000000Z",
"name": "Lee Rosenbaum"
},
{
"id": 3,
"member_id": 14,
"date_start": "2023-03-31",
"created_at": "2023-02-12T10:03:46.000000Z",
"updated_at": "2023-02-12T10:03:46.000000Z",
"name": "Erik Spinka"
},
{
"id": 4,
"member_id": 15,
"date_start": "2023-03-26",
"created_at": "2023-02-12T10:03:46.000000Z",
"updated_at": "2023-02-12T10:03:46.000000Z",
"name": "Anabel Monahan"
},
{
"id": 5,
"member_id": 9,
"date_start": "2023-02-21",
"created_at": "2023-02-12T10:03:46.000000Z",
"updated_at": "2023-02-12T10:03:46.000000Z",
"name": "Anthony Wilkinson"
},
{
"id": 6,
"member_id": 15,
"date_start": "2023-02-23",
"created_at": "2023-02-12T10:03:46.000000Z",
"updated_at": "2023-02-12T10:03:46.000000Z",
"name": "Anabel Monahan"
},
{
"id": 7,
"member_id": 13,
"date_start": "2023-04-08",
"created_at": "2023-02-12T10:03:46.000000Z",
"updated_at": "2023-02-12T10:03:46.000000Z",
"name": "Pink Moen"
},
{
"id": 9,
"member_id": 10,
"date_start": "2023-02-23",
"created_at": "2023-02-12T10:03:46.000000Z",
"updated_at": "2023-02-12T10:03:46.000000Z",
"name": "Viva Wilkinson"
}
]
$jumlah
[
{
"transaction_id": 2,
"total": "5"
},
{
"transaction_id": 3,
"total": "7"
},
{
"transaction_id": 4,
"total": "2"
},
{
"transaction_id": 5,
"total": "4"
},
{
"transaction_id": 6,
"total": "4"
},
{
"transaction_id": 7,
"total": "2"
},
{
"transaction_id": 9,
"total": "2"
}
]
$tanggal
[
{
"transaction_id": 2,
"tgl_kembali": "2023-05-25"
},
{
"transaction_id": 3,
"tgl_kembali": null
},
{
"transaction_id": 4,
"tgl_kembali": null
},
{
"transaction_id": 5,
"tgl_kembali": null
},
{
"transaction_id": 6,
"tgl_kembali": null
},
{
"transaction_id": 7,
"tgl_kembali": "2023-02-17"
},
{
"transaction_id": 9,
"tgl_kembali": "2023-04-21"
}
]
I need to make a new variabel like $newData
I expected the result should be like this for $newData :
[
{
"id": 2,
"member_id": 19,
"date_start": "2023-03-14",
"created_at": "2023-02-12T10:03:46.000000Z",
"updated_at": "2023-02-12T10:03:46.000000Z",
"name": "Lee Rosenbaum",
"tgl_kembali": "2023-05-25",
"total": "5"
},
{
"id": 3,
"member_id": 14,
"date_start": "2023-03-31",
"created_at": "2023-02-12T10:03:46.000000Z",
"updated_at": "2023-02-12T10:03:46.000000Z",
"name": "Erik Spinka",
"tgl_kembali": null,
"total": "7"
},
{
"id": 4,
"member_id": 15,
"date_start": "2023-03-26",
"created_at": "2023-02-12T10:03:46.000000Z",
"updated_at": "2023-02-12T10:03:46.000000Z",
"name": "Anabel Monahan",
"tgl_kembali": null,
"total": "2"
},
{
"id": 5,
"member_id": 9,
"date_start": "2023-02-21",
"created_at": "2023-02-12T10:03:46.000000Z",
"updated_at": "2023-02-12T10:03:46.000000Z",
"name": "Anthony Wilkinson",
"tgl_kembali": null,
"total": "4"
},
{
"id": 6,
"member_id": 15,
"date_start": "2023-02-23",
"created_at": "2023-02-12T10:03:46.000000Z",
"updated_at": "2023-02-12T10:03:46.000000Z",
"name": "Anabel Monahan",
"tgl_kembali": null,
"total": "4"
},
{
"id": 7,
"member_id": 13,
"date_start": "2023-04-08",
"created_at": "2023-02-12T10:03:46.000000Z",
"updated_at": "2023-02-12T10:03:46.000000Z",
"name": "Pink Moen",
"tgl_kembali": "2023-02-17",
"total": "2"
},
{
"id": 9,
"member_id": 10,
"date_start": "2023-02-23",
"created_at": "2023-02-12T10:03:46.000000Z",
"updated_at": "2023-02-12T10:03:46.000000Z",
"name": "Viva Wilkinson",
"tgl_kembali": "2023-04-21",
"total": "2"
}
]
what I can to solve this problem? I work on php(laravel).For some reason I have not been able to find the answer anywhere. I assume it is possible but I just need to make sure.
const newData = [];
for (const transaction of transactions) {
const jumlah = jumlahs.find(j => j.transaction_id === transaction.id);
const tanggal = tanggals.find(t => t.transaction_id === transaction.id);
newData.push({
id: transaction.id,
member_id: transaction.member_id,
date_start: transaction.date_start,
created_at: transaction.created_at,
updated_at: transaction.updated_at,
name: transaction.name,
total: jumlah.total,
tgl_kembali: tanggal.tgl_kembali
});
}
Here is the JSON object, so if I look at below object it has children till level 3 and object at level 3 has no children's.
So what is the efficient way to find out?
{
"id": 2,
"name": "Economic Segment",
"maxLevel": 10,
"parentId": null,
"children": [
{
"id": 7,
"name": "Revenue",
"maxLevel": 9,
"parentId": 2,
"children": [
{
"id": 12,
"name": "R Child",
"maxLevel": 8,
"parentId": 7,
"children": [
{
"id": 12,
"name": "R Child",
"maxLevel": 8,
"parentId": 7,
"children": []
}
]
}
]
},
{
"id": 8,
"name": "Expenditure",
"maxLevel": 9,
"parentId": 2,
"children": []
},
{
"id": 9,
"name": "Asset",
"maxLevel": 9,
"parentId": 2,
"children": []
}
]
}}
I have the following models
Order (id,number)
Detail (id,item_id,count,user_id ...etc)
Item (id,name)
Cat (id,name)
User (id,name) which is not important in my case
Each order has Many details
Each detail belong to item
Each item belong to category
Each detail belong to user
I wrote this line of code
public function show(Order $order)
{
//
$orders= $order->details()->with(['item','user'])->get();
return response()->json(['details'=>$orders],200);
}
to obtains a response like this :
"details": [
{
"id": 3089,
"count": 3,
"item_id": 102,
"order_id": 1,
"user_id": 10,
"created_at": "2019-11-22 22:19:23",
"updated_at": "2019-11-22 22:19:23",
"user": {
"id": 10,
"name": "Ms. Eda Stoltenberg DVM",
"username": "Brandy Murazik",
"verified": "1",
"verification_token": null,
"status": "1",
"dept_id": 5,
"created_at": "2019-11-22 22:15:47",
"updated_at": "2019-11-22 22:15:47"
},
"item": {
"id": 102,
"code": "Lamar Hansen",
"cat_id": 91,
"created_at": "2019-11-22 22:15:54",
"updated_at": "2019-11-22 22:15:54"
}
},
{
"id": 3428,
"count": 1,
"item_id": 15,
"order_id": 1,
"user_id": 10,
"created_at": "2019-11-22 22:19:41",
"updated_at": "2019-11-22 22:19:41",
"user": {
"id": 10,
"name": "Ms. Eda Stoltenberg DVM",
"username": "Brandy Murazik",
"verified": "1",
"verification_token": null,
"status": "1",
"dept_id": 5,
"created_at": "2019-11-22 22:15:47",
"updated_at": "2019-11-22 22:15:47"
},
"item": {
"id": 15,
"code": "Hilario Dicki",
"cat_id": 20,
"created_at": "2019-11-22 22:15:51",
"updated_at": "2019-11-22 22:15:51"
}
},
{
"id": 3493,
"count": 1,
"item_id": 129,
"order_id": 1,
"user_id": 6,
"created_at": "2019-11-22 22:19:45",
"updated_at": "2019-11-22 22:19:45",
"user": {
"id": 6,
"name": "Prof. Marina Kiehn",
"username": "Nickolas Hessel",
"verified": "0",
"verification_token": "mwWkL95jjyi5di9PSH3T3LXXZEE1W2DmegTxAOtN",
"status": "1",
"dept_id": 3,
"created_at": "2019-11-22 22:15:47",
"updated_at": "2019-11-22 22:15:47"
},
"item": {
"id": 129,
"code": "Dr. Donnell Harber",
"cat_id": 54,
"created_at": "2019-11-22 22:15:55",
"updated_at": "2019-11-22 22:15:55"
}
},
{
"id": 4032,
"count": 1,
"item_id": 221,
"order_id": 1,
"user_id": 8,
"created_at": "2019-11-22 22:20:10",
"updated_at": "2019-11-22 22:20:10",
"user": {
"id": 8,
"name": "Prof. Thaddeus Boehm",
"username": "Frederick Kshlerin",
"verified": "1",
"verification_token": null,
"status": "1",
"dept_id": 3,
"created_at": "2019-11-22 22:15:47",
"updated_at": "2019-11-22 22:15:47"
},
"item": {
"id": 221,
"code": "Maia Hettinger V",
"cat_id": 57,
"created_at": "2019-11-22 22:15:58",
"updated_at": "2019-11-22 22:15:58"
}
},
{
"id": 4311,
"count": 1,
"item_id": 139,
"order_id": 1,
"user_id": 3,
"created_at": "2019-11-22 22:20:21",
"updated_at": "2019-11-22 22:20:21",
"user": {
"id": 3,
"name": "Cletus Walter I",
"username": "Johan Kemmer",
"verified": "0",
"verification_token": "M6MWe7mOmzcyM0rrlbMwVCX7q2vyULFKwSAJmQwl",
"status": "0",
"dept_id": 5,
"created_at": "2019-11-22 22:15:46",
"updated_at": "2019-11-22 22:15:46"
},
"item": {
"id": 139,
"code": "Tanner Schimmel",
"cat_id": 80,
"created_at": "2019-11-22 22:15:55",
"updated_at": "2019-11-22 22:15:55"
}
},
{
"id": 4821,
"count": 2,
"item_id": 243,
"order_id": 1,
"user_id": 1,
"created_at": "2019-11-22 22:20:41",
"updated_at": "2019-11-22 22:20:41",
"user": {
"id": 1,
"name": "Mrs. Fay Cassin",
"username": "Ryley Bode",
"verified": "0",
"verification_token": "E93hzJbIp2eCxbBGgtcsYDckRreASTPL6ZEAyyKP",
"status": "1",
"dept_id": 1,
"created_at": "2019-11-22 22:15:46",
"updated_at": "2019-11-22 22:15:46"
},
"item": {
"id": 243,
"code": "Miss Jaida Simonis DVM",
"cat_id": 53,
"created_at": "2019-11-22 22:15:59",
"updated_at": "2019-11-22 22:15:59"
}
}
]
But i also need to get category object inside item object, which is already relationship is Item belong to Category
I know that i can make a join query but i prefer to find a best solution with relation based between models
So how can i do that if it is possible?
thanks
You may try this (Since item belongs to category):
$orders = $order->details()->with(['item.category', 'user'])->get();
I am using Magento 2 rest api for listing all the catgories.
{{production_url}}/index.php/rest/V1/categories
It will return all the categories,
{
"id": 2,
"parent_id": 1,
"name": "Default Category",
"is_active": true,
"position": 1,
"level": 1,
"product_count": 0,
"children_data": [{
"id": 3,
"parent_id": 2,
"name": "T-shirts",
"is_active": true,
"position": 1,
"level": 2,
"product_count": 8,
"children_data": []
}, {
"id": 4,
"parent_id": 2,
"name": "Phants",
"is_active": true,
"position": 2,
"level": 2,
"product_count": 0,
"children_data": []
}, {
"id": 5,
"parent_id": 2,
"name": "Chridar",
"is_active": true,
"position": 3,
"level": 2,
"product_count": 0,
"children_data": []
}]
}
But i need custom attributes for every categories in the result.But now i have to call the below api for getting custom attributes.
{{production_url}}/index.php/rest/V1/categories/3
It will return ,
{
"id": 3,
"parent_id": 2,
"name": "T-shirts",
"is_active": true,
"position": 1,
"level": 2,
"children": "",
"created_at": "2017-06-02 11:21:16",
"updated_at": "2017-06-02 11:21:16",
"path": "1/2/3",
"available_sort_by": [],
"include_in_menu": true,
"custom_attributes": [
{
"attribute_code": "description",
"value": "<p>retest</p>"
},
{
"attribute_code": "image",
"value": "Screen_Shot_2017-06-16_at_4.06.35_PM.png"
},
{
"attribute_code": "display_mode",
"value": "PRODUCTS"
},
{
"attribute_code": "is_anchor",
"value": "1"
},
{
"attribute_code": "path",
"value": "1/2/3"
},
{
"attribute_code": "children_count",
"value": "0"
},
{
"attribute_code": "custom_use_parent_settings",
"value": "0"
},
{
"attribute_code": "custom_apply_to_products",
"value": "0"
},
{
"attribute_code": "url_key",
"value": "redwine"
},
{
"attribute_code": "url_path",
"value": "redwine"
}
]
}
Suppose if there are n catgories i need to call n api for getting custom attributes.Is there any single api for getting all the attributes for all categories in a single API?
The Magento Api CatalogTreeInterface doesn't extend Magento\Framework\Api\ExtensibleDataInterface which means that custom attributes or extension attributes cannot be added to the tree response. The only workaround for me was to create my own module and a new api call that extends the tree interface to add my custom attributes.
I suppose Ruben is right. I've built Magento extension to add image attribute to the category tree, please have a look https://github.com/troublediehard/mma-customapi
I'm dealing with searching in categories by slug and I want to exclude results which contain dash & number at the end (like: powerbank-1).
My query :
$categories = Category::where('active_products', ">", 0)
->where('slug', 'LIKE', "%$search_term%")
->where('slug', 'not regexp', "/$search_term-[0-9]/")
->get();
My result still contain unwanted results with -1 & -2 endings in slug:
{
"query": "powerbank",
"categories": [
{
"id": 18,
"parent_id": 17,
"lft": 108,
"rgt": 109,
"depth": 1,
"name": "Powerbank",
"description": null,
"description2": null,
"products": 43,
"active_products": 38,
"created_at": "2016-08-25 20:51:42",
"updated_at": "2016-09-20 06:06:06",
"slug": "powerbank"
},
{
"id": 20,
"parent_id": 19,
"lft": 124,
"rgt": 125,
"depth": 1,
"name": "Powerbank",
"description": null,
"description2": null,
"products": 43,
"active_products": 38,
"created_at": "2016-08-25 20:51:43",
"updated_at": "2016-09-20 06:06:06",
"slug": "powerbank-1"
},
{
"id": 22,
"parent_id": 21,
"lft": 136,
"rgt": 137,
"depth": 1,
"name": "Powerbank",
"description": null,
"description2": null,
"products": 43,
"active_products": 38,
"created_at": "2016-08-25 20:51:43",
"updated_at": "2016-09-20 06:06:06",
"slug": "powerbank-2"
}]
}
Found the problem why was not working: delete slashes from regex, so itt will be:
->where('slug', 'not regexp', "$search_term-[0-9]")