combine multiple arrays so that there are a variety of options - php

So let's say I have this array:
"variants": [
{
"id": 5,
"name": "color",
"item_id": 3,
"created_at": "2018-11-02 15:08:19",
"updated_at": "2018-11-02 15:08:19",
"options": [
{
"id": 13,
"name": "red",
"variant_id": 5,
"created_at": "2018-11-02 15:08:21",
"updated_at": "2018-11-02 15:08:21"
},
{
"id": 14,
"name": "blue",
"variant_id": 5,
"created_at": "2018-11-02 15:08:21",
"updated_at": "2018-11-02 15:08:21"
},
{
"id": 15,
"name": "green",
"variant_id": 5,
"created_at": "2018-11-02 15:08:22",
"updated_at": "2018-11-02 15:08:22"
}
]
},
{
"id": 6,
"name": "size",
"item_id": 3,
"created_at": "2018-11-02 15:08:19",
"updated_at": "2018-11-02 15:08:19",
"options": [
{
"id": 16,
"name": "small",
"variant_id": 6,
"created_at": "2018-11-02 15:08:22",
"updated_at": "2018-11-02 15:08:22"
},
{
"id": 17,
"name": "medium",
"variant_id": 6,
"created_at": "2018-11-02 15:08:22",
"updated_at": "2018-11-02 15:08:22"
},
{
"id": 18,
"name": "large",
"variant_id": 6,
"created_at": "2018-11-02 15:08:22",
"updated_at": "2018-11-02 15:08:22"
}
]
}
]
how would you go about combining all of the possibilities so that i'd have:
red small, red medium, red large, blue small, blue medium, blue large, green small, green medium, green large. Also the arrays would not necessarily always be the same sizes.
This project is being written in PHP specifically using the laravel framework

To accommodate for multiple variants:
function getVariants($obj)
{
$variant = array_shift($obj["variants"]); // we use the variants as a stack
$results = array(); // we will store the results here
foreach($variant["options"] AS $k=>$v) // we iterate the current variants
{
if(count($obj["variants"]) > 0) // if we have more variants still
{
$sub = getVariants($obj); // we call getVariants to build next level
foreach($sub AS $sub_v) // iterate over the results of the child level
{
// concatenate whatever came from children to the current names
$results[] = $v["name"]." ".$sub_v;
}
}
else
{
$results[] = $v["name"]; // this is the last variant so we just add the names.
}
}
return $results;
}
This should work with any depth of combinations needed.
What this code does it use the variants as a stack processing the variant and then if the stack is longer still calling itself to do the same on the next level. Each level returns an array of themselves and their children if any.
https://en.wikipedia.org/wiki/Recursion_(computer_science)

Related

make a new variable array [closed]

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
});
}

How can loop & get values of unlimited parent-child & sub-child tree data in laravel

I have data like parent-child with unlimited levels of sub-child & sub-child data in the database which you can see below like a binary tree with nodes that have multiple children and these children can have unlimited sub-child.
Please give me a solution or suggestion how can show that. Thanks.
This is my database JSON data which I'm getting from the database...
Controller Code
$products = Product::where('project_id', $project_id )->whereNull('parent_id')->with('categories')->with('childProducts')->get();
Getting data from the above object this data want to save in the other way you can see below
{
"data": {
"products": [{
"id": 1,
"name": "ParentProduct",
"project_id": 1,
"parent_id": null,
"created_by": 1,
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00",
"categories": [{
"id": 6,
"product_id": 1,
"project_id": 1,
"name": "TopLevelCategory",
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00"
}],
"child_products": [{
"id": 2,
"name": "LevelOneProduct",
"project_id": null,
"parent_id": 1,
"created_by": null,
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00",
"categories": [{
"id": 1,
"product_id": 2,
"project_id": 1,
"name": "LevelOneProductCategory",
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00"
}],
"child_products": [{
"id": 3,
"name": "LevelOneProductChildFolder",
"project_id": null,
"parent_id": 2,
"created_by": null,
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00",
"categories": [{
"id": 2,
"product_id": 3,
"project_id": 1,
"name": "evelOneProductChildFolderCategory",
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00"
}],
"child_products": [{
"id": 4,
"name": "LevelOneProductSubChildFolder",
"project_id": null,
"parent_id": 3,
"created_by": null,
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00",
"categories": [{
"id": 3,
"product_id": 4,
"project_id": 1,
"name": "LevelOneProductSubChildFolderCategory",
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00"
}],
"child_products": []
}]
}]
},
{
"id": 5,
"name": "LeveTwoProduct",
"project_id": null,
"parent_id": 1,
"created_by": null,
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00",
"categories": [],
"child_products": [{
"id": 6,
"name": "LeveTwoProductFolder",
"project_id": null,
"parent_id": 5,
"created_by": null,
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00",
"categories": [{
"id": 4,
"product_id": 6,
"project_id": 1,
"name": "LeveTwoProductFolderCategory",
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00"
}],
"child_products": [{
"id": 7,
"name": "LeveTwoProductChildFolder",
"project_id": null,
"parent_id": 6,
"created_by": null,
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00",
"categories": [{
"id": 5,
"product_id": 7,
"project_id": 1,
"name": "LeveTwoProductChildFolderCategory",
"created_at": "2021-08-06 13:18:00",
"updated_at": "2021-08-06 13:18:00"
}],
"child_products": []
}]
}]
}
]
}]
}
}
I want to take this data to another JSON data which I have to give you an example as mentioned below I want to loop the above JSON data and want to make their need JSON with this data as like Dear Problem is that How can loop above data and can give to the other JSON keys you can see below. I want all data with loop and want to get all values and want equal all values to the keys as below
foreach ($products as $product) {
foreach ($product->categories as $singleCaegory) {
$singleCaegoryData = [
"name" => $singleCaegory->name,
"request" => [
'method' => $singleCaegory->type,
'body' => [
"mode" => "raw",
'raw' => $singleCaegory->raw_data
],
'url' => [
"raw" => $singleCaegory->url
],
],
];
}
}
I think you understand my problem. that I want to loop above data which I have already in the database and fetching from that. I only want to loop all data and equal to the needed keys. Thanks.
for saving Id's you only need:-parent Id, child_Id_count(count of the only direct child), total_child_count(count all children below).that's it by using these data and created date you can generate a tree view.

PHP Group Json Objects

"forms": [
{
"id": 1,
"name": "new_patient",
"deleted_at": null,
"created_at": "2017-10-24 05:12:24",
"updated_at": "2017-10-24 05:12:24",
"pivot": {
"institution_id": 1,
"form_id": 1,
"field_name_id": 21,
"field_caption": "Gön.Hekim"
}
},
{
"id": 1,
"name": "new_patient",
"deleted_at": null,
"created_at": "2017-10-24 05:12:24",
"updated_at": "2017-10-24 05:12:24",
"pivot": {
"institution_id": 1,
"form_id": 1,
"field_name_id": 22,
"field_caption": "Kurum"
}
},
{
"id": 1,
"name": "new_patient",
"deleted_at": null,
"created_at": "2017-10-24 05:12:24",
"updated_at": "2017-10-24 05:12:24",
"pivot": {
"institution_id": 1,
"form_id": 1,
"field_name_id": 23,
"field_caption": "Endikasyon"
}
},
{
"id": 2,
"name": "edit_patient",
"deleted_at": null,
"created_at": "2017-10-24 05:12:24",
"updated_at": "2017-10-24 05:12:24",
"pivot": {
"institution_id": 1,
"form_id": 2,
"field_name_id": 24,
"field_caption": "Materyal"
}
}
]
I want to group forms object in same name/id.
How i want...
"forms": [
{
"id": 1,
"name": "new_patient",
"deleted_at": null,
"created_at": "2017-10-24 05:12:24",
"updated_at": "2017-10-24 05:12:24",
"fields": [{
"field_name_id": 21,
"field_caption": "Gön.Hekim"
},{
"field_name_id": 22,
"field_caption": "Kurum"
},{
"field_name_id": 23,
"field_caption": "Endikasyon"
}]
},
{
"id": 2,
"name": "edit_patient",
"deleted_at": null,
"created_at": "2017-10-24 05:12:24",
"updated_at": "2017-10-24 05:12:24",
"fields": [{
"field_name_id": 24,
"field_caption": "Materyal"
}]
}
]
i searched in so and googled, but i could not find any method for my problem. There are answers to similar questions.
i did with foreach loop but i thought may be there is a method for this in php.
And also, if i have over an hundred objects in json, foreach can be slow.
Thanks in advice.
Try the following:
$arr = json_decode($str, true);
$arr2 = [];
$ids = [];
foreach($arr['forms'] as $form)
{
if(!in_array($form['id'], $ids))
{
$ids[] = $form['id'];
$arr2[$form['id']] = $form;
}
unset($arr2[$form['id']]['pivot']);
$arr2[$form['id']]['fields'][] = [$form['pivot']['field_name_id'], $form['pivot']['field_caption']];
}
$str2 = json_encode(['forms' => array_values($arr2)]);
echo $str2;
This uses a foreach loop to get unique forms and will create a new key fields to hold the field_name_id and field_caption and also removes the pivot key

Merge two objects, while one is inside of an array

I need to merge 2 objects using PHP (Laravel) one of them is in an array, the other is a plain object, but also contains a nested object, is there any simple way to do it?
Edit: The result should be a single object
[
{
"id": 1,
"release_date": "1998",
"license_plate": "098HBV",
"state_id": 1,
"type_id": 7,
"created_at": "2016-11-18 11:39:19",
"updated_at": "2016-11-18 11:39:19",
"deleted_at": null
}
]
and
{
"id": 1,
"parent_id": null,
"name": "Chrysler",
"state_id": 1,
"type_id": 1,
"created_at": "2016-11-18 11:39:10",
"updated_at": "2016-11-18 11:39:10",
"deleted_at": null,
"pivot": {
"vehicle_id": 1,
"brand_id": 1
}
}
Desired output should be:
{
"id": 1,
"release_date": "1998",
"license_plate": "098HBV",
"name": "Chrysler",
}
It has to be something similar, otherwise it would break a lot of JavaScript code connected to the first object.
<?php
//Merge the two as an array
$merged = array_merge($array,(array)$object);
//As an object
$merged = (object)$merged;

Trying to get property of non-object in Laravel even when it is present

the data i am testing on is like this in feed variable
{
"id": 32,
"user_id": 1,
"target_id": 3,
"feedable_id": 7,
"feedable_type": "review",
"created_at": "2016-05-23 14:18:22",
"updated_at": "2016-05-23 14:18:22",
"feedable": {
"id": 7,
"user_id": 1,
"slug_id": 3,
"review": "y this product is so quite?",
"stars": 9,
"created_at": "2016-05-23 14:18:22",
"updated_at": "2016-05-23 14:18:22",
"slug": {
"id": 3,
"value": "Quite",
"views": 5,
"user_id": 1,
"category_id": 1,
"created_at": "2016-05-23 14:18:03",
"updated_at": "2016-05-24 12:47:29"
}
}
}
but i am not be able to access the value:quite in my view
{{ $feed->feedable->slug->value }}
Laravel throws
Trying to get property of non-object (View: ...
This is a json variable so you need to decode it first then access the value. Using json_decode you can decode the variable and get the desire value.
Check online.
$json = '{
"id": 32,
"user_id": 1,
"target_id": 3,
"feedable_id": 7,
"feedable_type": "review",
"created_at": "2016-05-23 14:18:22",
"updated_at": "2016-05-23 14:18:22",
"feedable": {
"id": 7,
"user_id": 1,
"slug_id": 3,
"review": "y this product is so quite?",
"stars": 9,
"created_at": "2016-05-23 14:18:22",
"updated_at": "2016-05-23 14:18:22",
"slug": {
"id": 3,
"value": "Quite",
"views": 5,
"user_id": 1,
"category_id": 1,
"created_at": "2016-05-23 14:18:03",
"updated_at": "2016-05-24 12:47:29"
}
}
}';
$feed = json_decode ($json);
echo $feed->feedable->slug->value; //Quite

Categories