*Hi! I'm working on my task, and after I query my table. It gave me this result *
Collection {#499
#items: array:3 [
0 => {#497
+"id": 1
+"text": "Category 1"
+"sub": "sub-cat 1"
+"question": "first question"
+"description": "category first description"
}
1 => {#501
+"id": 2
+"text": "Category 1"
+"sub": "sub-cat 2"
+"question": "second question"
+"description": "category second description"
}
2 => {#502
+"id": 3
+"text": "Category 2"
+"sub": "another sub-cat"
+"question": "third question"
+"description": "category third description"
}
]
}
And I need to transform this into this
Collection {#499
#items: array:3 [
0 => {#497
+"id": 1
+"text": "Category 1"
+"sub": "sub-cat 1"
[{
+"question": "first question"
+"description": "category first description"
}]
+"sub": "sub-cat 2"
[{
+"question": "second question"
+"description": "category second description"
}]
}
1 => {#502
+"id": 3
+"text": "Category 2"
+"sub": "another sub-cat"
+"question": "third question"
+"description": "category third description"
}
]
}
I need to merge an array with the same key and make it as associative array
Thanks in advance
Something like this?
$results = [];
foreach ($mycollection as $v) {
if (!isset($results[$v->text])) {
$results[$v->text] = [
'id' => $v->id,
'text' => $v->text,
'sub' => []
];
}
$results[$v->text]['sub'][$v->sub] = [
'question' => $v->question,
'description' => $v->description
];
}
// If you want a numeric index for your array :
// $results = array_values($results);
Related
I have the following array structure from a database query:
I extended my previous posts, with more details about the array structure. Hope that helps to better understand my example.
array:7 [▼
0 => array:4 [▼
0 => array:2 [▼
"my_first_key" => "option 1"
"SUMME" => "6"
]
1 => array:2 [▼
"my_first_key" => "option 2"
"SUMME" => "22"
]
2 => array:2 [▼
"my_first_key" => "option 3"
"SUMME" => "37"
]
3 => array:2 [▼
"my_first_key" => "option 4"
"SUMME" => "42"
]
],
0 => array:4 [▼
0 => array:2 [▼
"my_second_key" => "option 1"
"SUMME" => "6"
]
1 => array:2 [▼
"my_second_key" => "option 2"
"SUMME" => "22"
]
2 => array:2 [▼
"my_second_key" => "option 3"
"SUMME" => "37"
]
3 => array:2 [▼
"my_second_key" => "option 4"
"SUMME" => "42"
]
]
...
]
How can I transform it to a much simpler structure like:
array:8 [▼
"my_first_key" => array:4 [▼
"option 1" => "6"
"option 2" => "22"
"option 3" => "37"
"option 4" => "42"
],
"my_second_key" => array:4 [▼
"option 1" => "6"
"option 2" => "22"
"option 3" => "37"
"option 4" => "42"
]
...
]
I tried to give array_walk_recursive a try, but I can't grab the SUMME key and bind it to the previous option?
$tmpArr = [];
array_walk_recursive($data, static function ($value, $key) use (&$tmpArr) {
$tmpArr[$key][$value] = $value; // not $value? SUMME here?
}, $tmpArr);
You need to simple do it like below using foreach()
$tmpArr = [];
foreach($data as $dat){
foreach($dat as $d){
$key = array_keys($d)[0];
$tmpArr[$key][$d[$key]] = $d['SUMME'];
}
}
Output : https://3v4l.org/hmu5r
If you want to use array_walk_recursive you can do it like this:
$output = [];
array_walk_recursive($arr, function ($item, $key) use (&$output) {
if ($key === 'SUMME') {
end($output);
$a = key($output);
end($output[$a]);
$b = key($output[$a]);
$output[$a][$b] = $item;
} else {
$output[$key][$item] = '';
}
});
Is it possible to use $request->except on a nested array? This is the request data:
[▼
"id" => 1
"products" => array:1 [▼
0 => array:4 [▼
"id" => 1
"name" => "sample product"
"date" => "07/04/2022"
"frequency" => array:1 [ …1]
]
]
]
My goal is to remove some key value pairs e.g. removing id, date and frequency. My desired result would be:
[▼
"id" => 1
"products" => array:1 [▼
0 => array:1 [▼
"name" => "sample product"
]
]
]
What I've tried so far is to use Arr:except function:
$request->products = Arr::except($request->products['0'], ['id', 'date', 'frequency']);
But this should be applied on all items. Let's say I have two item products, the desired result would be:
[▼
"id" => 1
"products" => array:2 [▼
0 => array:1 [▼
"name" => "sample product"
]
1 => array:1 [▼
"name" => "sample product 2"
]
]
]
Need your inputs on what's the best approach for this. Thank you!
Unless I am missing something obvious, could you not just iterate over the products in your $request object?
$filtered = [];
foreach ($request->products as $product) {
$filtered[] = Arr::except($product, ['id', 'date', 'frequency']);
}
dd($filtered);
Resulting in $filtered containing:
^ array:2 [▼
"id" => 1
"products" => array:2 [▼
0 => array:1 [▼
"name" => "sample product"
]
1 => array:1 [▼
"name" => "sample product 2"
]
]
]
Update 1
I can see that the $request except only works on the top level
Not sure what you mean by this as the above principle works for me.
$filtered = [];
foreach ($request->products as $product) {
$filtered[] = Arr::except($product, ['id', 'date', 'frequency']);
}
$request->merge(['products' => $filtered]);
If I supply the above with the following:
{
"id": 1,
"products": [
{
"id": 1,
"name": "sample product",
"date": "07/04/2022",
"frequency": []
},
{
"id": 2,
"name": "sample product 2",
"date": "07/04/2022",
"frequency": []
}
]
}
Then do a dd($request->products); I get:
^ array:2 [▼
0 => array:1 [▼
"name" => "sample product"
]
1 => array:1 [▼
"name" => "sample product 2"
]
]
I am trying to create string associated with an object and create an array. Instead of having 0 => array:1, 1 =>array:1, 2 =>array:1. I want to have just "orange" => Option {}, "mango" => Option{}, "banana" => Option {} in an array as Array:5
array:5 [▼
0 => array:1 [▼
"orange" => Option {#76 ▼
-name: "american orange"
-botanical: "citrus"
-year: "1"
-color: "orange"
-size: "2"
-scope: "season"
-choices: []
}
]
1 => array:1 [▼
"Mango" => Option {#3098 ▼
-name: "american orange"
-botanical: ""
-year: "2"
-color: "green"
-size: "4"
-scope: "season"
-choices: []
}
]
2 => array:1 [▼
"banana" => Option {#3099 ▼
-name: "Billy"
-botanical: ""
-year: "1"
-color: "dark"
-size: "2"
-scope: "season"
-choices: array:5 [▼
"" => ""
"status" => "hard"
"condition" => "ripe"
"thickness" => "thick"
"long" => "long"
]
}
]
]
This is my code, $options[] = [$resultKey => $option]; This is where i am getting it wrong. [$resultKey => $option] is the one creating an array. All I want is $resultKey => $option so that I can achieve "Orange" => Option{}. I tried $options[] = (object)[$resultKey => $option]; it is still displaying indices. as I only want pair values without indices. ["Orange" => Option{}, "Mango" => Option{}, "Banana" => Option{} ]
$options = [];
foreach ($result as $resultKey => $resultValue) {
$option = new Option($resultKey['scope'], $resultValue['name'], '', $resultValue['botanical'], $resultValue['year'], $resultValue['color'], $resultValue['size'] !== 'choice' ? [] :$num[0]);
$options[] = [$resultKey => $option];
// dump([$resultKey => $option]);
}
```
I'm having 2 multidimensional arrays on php, and I'm trying to get only the subarrays that have matching column value.
1st Array :
0 => {#1 ▼
+"name": "John Doe"
+"education": "Finance"
+"age": "23"
+"mark": "10"
}
1 => {#2 ▼
+"name": "Jane Doe"
+"education": "History"
+"age": "25"
+"mark": "9"
}
2 => {#3 ▼
+"name": "Anne Smith"
+"education": "Computer Science"
+"age": "22"
+"mark": "8"
}
2nd Array :
0 => {#1 ▼
+"name": "Bob Builder"
+"country": "US"
+"city": "Massachusetts"
}
1 => {#2 ▼
+"name": "Jane Doe"
+"country": "France"
+"city": "Paris"
}
2 => {#3 ▼
+"name": "Anne Smith"
+"country": "Germany"
+"city": "Berlin"
}
I want to match on column name.
Desired output should be as follows :
0 => {#2 ▼
+"name": "Jane Doe"
+"country": "France"
+"city": "Paris"
+"education": "History"
+"age": "25"
+"mark": "9"
}
1 => {#3 ▼
+"name": "Anne Smith"
+"country": "Germany"
+"city": "Berlin"
+"education": "Computer Science"
+"age": "22"
+"mark": "8"
}
Any help on this would be appreciated.
Here is one way to do it.
First, reindex the first array by name. This will simplify matching names in the next part.
$first = array_column($first, null, 'name');
Then iterate the second array. If the person exists in the first array, merge the values from the two arrays and append that to the product. If not, don't do anything, since you're only looking for the intersection.
foreach ($second as $person) {
if (isset($first[$person['name']])) {
$product[] = array_merge($first[$person['name']], $person);
}
}
One potential problem with this is that names aren't unique, and this will break if you ever have any people with the same name. It would be much better if you had some unique identifier to use for a person rather than name.
I'm currently dumping this array:
0 => {#407 ▼
+"item_id": "11873"
+"type": "Item Title"
+"comment": "Item Title"
+"typet_id": "2"
}
1 => {#408 ▼
+"item_id": "11873"
+"type": "Item Instruction"
+"comment": "test instruction"
+"typet_id": "2"
}
2 => {#409 ▼
+"item_id": "11875"
+"type": "Item Title"
+"comment": "Test Title"
+"typet_id": "2"
}
3 => {#410 ▼
+"item_id": "11874"
+"type": "Item Title"
+"comment": "Item Title"
+"typet_id": "2"
}
4 => {#411 ▼
+"item_id": "11874"
+"type": "Item Instruction"
+"comment": "test instruction"
+"typet_id": "2"
}
and it does show all of the correct data, but I'm trying to figure out how to set up a foreach loop with the structure I need.
As you can see, two of the items above have the same item_id because they each have different 'type' values. So each Item can have a Item title and a Item instruction, and I'm just getting multiple results for each Item.
I'm trying to get a structure where they are grouped by item_id and then I can have both 'type' values
0 => {#407 ▼
+"item_id": "11873"
2=> {
type: "Item Title"
comment: "Item Title"
},
{
type:"Item Instruction"
comment: "Test Instruction"
}
typet_id:"2"
}
1 => {#408 ▼
+"item_id": "11874"
2=> {
type: "Item Title"
comment: "Item Title"
},
{
type:"Item Instruction"
comment: "Test Instruction"
}
typet_id:"2"
}
2 => {#409 ▼
+"item_id": "11875"
2=> {
type: "Item Title"
comment: "Item Title"
}
typet_id:"2"
}
I know I can set the first foreach around the id, but I'm unclear how to structure the rest to get the correct output I'm looking for so that I can differentiate between the type and comment. Basically if type is Title, I need to set it to one variable and if type is Instruction I need to set it to a different value because they will go in different columns of an html table.
foreach($getItem as $id => $components){
$id = $components->item_id;
}