Php Getting matching vaues from multidimensional arrays based on column - php

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.

Related

PHP - How can I get rid of square bracket and index in array

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

How to insert variable and value on collection laravel

I'm having trouble put values into collection laravel
I've read this with the same problem but didnt worked on me
reference
This is my collection
Illuminate\Support\Collection {#1348
#items: array:9 [
0 => {#1350
+"id": 532
+"id_user": "BR001"
+"name": "Jack"
+"department": "HR
+"City": "London"
+"updated_at": "2021-04-08 15:37:32"
}
1 => {#1351
+"id": 522
+"id_user": "BR002"
+"name": "Rose"
+"department": "IR"
+"city": "London"
+"updated_at": "2021-04-08 15:36:19"
}
2 => {#1352
+"id": 532
+"id_user": "AR023"
+"name": "Michael"
+"department": "RnD"
+"City": "London"
+"updated_at": "2021-04-08 15:45:43"
}
Expected output
Illuminate\Support\Collection {#1348
#items: array:9 [
0 => {#1350
+"id": 532
+"id_user": "BR001"
+"name": "Jack"
+"department": "HR
+"City": "London"
+"updated_at": "2021-04-08 15:37:32"
+"Status": "Single"
}
1 => {#1351
+"id": 522
+"id_user": "BR002"
+"name": "Rose"
+"department": "IR"
+"city": "London"
+"updated_at": "2021-04-08 15:36:19"
+"Status": "Single"
}
2 => {#1352
+"id": 532
+"id_user": "AR023"
+"name": "Michael"
+"department": "RnD"
+"City": "London"
+"updated_at": "2021-04-08 15:45:43"
+"Status": "Single"
}
How to add +"Status": "Single" ?
any help would be appreciated
Thank you
I have assumed your collection name as $collection.
You can either use map directly or create a function using macro,
Using Map Directly
$val = $collection->map(function ($value) {
$value["status"] = "Single";
return $value;
});
dd($val);
Creating custom function using macro
Collection::macro('toAddStatusProperty', function () {
return $this->map(function ($value, $key) {
$value["status"] = "Single";
return $value;
});
});
$val = $collection->toAddStatusProperty();
dd($val);

Re-structuring a multidimensional array for conditional variables

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

Laravel Database innerjoin primary key of the table does not display

I cannot update using my javascript or in basic update because the id is the same.. Do you have any idea on how to resolve this? Please help...
How can I solve this, I have this in my controller
$dataReqorder = DB::table('reqorders')
->join('productquantities', 'reqorders.item_id', '=', 'productquantities.id')
->join('products', 'productquantities.prod_id', '=', 'products.id')
->where('req_id', '=', $shoppingId)
->get();
dd($dataReqorder);
And it has a result like this
Collection {#272 ▼
#items: array:3 [▼
0 => {#265 ▼
+"id": 1
+"req_id": "ZJXYGX42RN"
+"item_id": "1"
+"quantity": 100
+"amount": "6600"
+"status": "0"
+"remember_token": null
+"created_at": "2018-03-10 15:18:38"
+"updated_at": "2018-03-11 13:03:15"
+"prod_id": 1
+"brand_id": 1
+"supplier_id": 1
+"branch_id": 3
+"category_id": "1"
+"price": "200"
+"saleprice": "100"
+"priceoption": "regular"
+"description": "brake for yamaha"
+"lenght": "10"
+"width": "10"
+"height": "10"
+"weight": "10"
+"unit": "piece"
+"pic": "1520752307.png"
+"product_name": "Brake"
}
1 => {#286 ▼
+"id": 1
+"req_id": "ZJXYGX42RN"
+"item_id": "2"
+"quantity": 100
+"amount": "67000"
+"status": "0"
+"remember_token": null
+"created_at": "2018-03-10 15:18:38"
+"updated_at": "2018-03-11 13:03:15"
+"prod_id": 1
+"brand_id": 1
+"supplier_id": 1
+"branch_id": 3
+"category_id": "1"
+"price": "200"
+"saleprice": "100"
+"priceoption": "regular"
+"description": "brake for yamaha"
+"lenght": "10"
+"width": "10"
+"height": "10"
+"weight": "10"
+"unit": "piece"
+"pic": "1520752614.png"
+"product_name": "Brake"
}
2 => {#289 ▼
+"id": 1
+"req_id": "ZJXYGX42RN"
+"item_id": "4"
+"quantity": 33
+"amount": "1000"
+"status": "0"
+"remember_token": null
+"created_at": "2018-03-10 15:18:38"
+"updated_at": "2018-03-11 13:03:15"
+"prod_id": 1
+"brand_id": 1
+"supplier_id": 1
+"branch_id": 3
+"category_id": "1"
+"price": "2"
+"saleprice": "2"
+"priceoption": "regular"
+"description": "brake for yamaha"
+"lenght": "12"
+"width": "2"
+"height": "2"
+"weight": "2"
+"unit": "roll"
+"pic": "1520752847.png"
+"product_name": "Brake"
}
]
}
However, I cannot update using my javascript or in basic update because the id is the same.. Do you have any idea on how to resolve this? Please help...
Your query selects all columns in all joined tables.
If multiple columns have the same name (e.g. id), the last one overwrites the previous ones.
You have to limit the selected columns and/or use aliases:
->get(['reqorders.*', 'products.id as products_id']);

assertJson failing even when fragment exists

assertJson fails even though fragment exists in response:
// test
...
$response->assertJson([
'type' => 'multiple_choice'
]);
...
response dump
.....array:3 [
0 => {#1503
+"id": 1
+"title_en": "Multiple Choice"
+"title_fr": "Multiple Choice"
+"type": "multiple_choice"
+"created_at": null
+"updated_at": null
}
1 => {#1143
+"id": 2
+"title_en": "Multiple Select"
+"title_fr": "Multiple Select"
+"type": "multiple_select"
+"created_at": null
+"updated_at": null
}
2 => {#981
+"id": 3
+"title_en": "Text"
+"title_fr": "Text"
+"type": "text"
+"created_at": null
+"updated_at": null
}
]
Use
$response->assertJsonFragment([
'type' => 'multiple_choice'
]);

Categories