I have an array like this & I need to group an array based on value.
array:3 [
0 => array:5 [
"id" => 1
"maxView" => 0
"maxClick" => 20
"companyName" => "Project A"
"email" => "user1#email.com"
]
1 => array:5 [
"id" => 1
"maxView" => 0
"maxClick" => 20
"companyName" => "Project A"
"email" => "user2#email.com"
]
2 => array:5 [
"id" => 1
"maxView" => 0
"maxClick" => 20
"companyName" => "Project A"
"email" => "user3#email.com"
]
3 => array:5 [
"id" => 1
"maxView" => 0
"maxClick" => 20
"companyName" => "Project B"
"email" => "user1#email.com"
]
4 => array:5 [
"id" => 1
"maxView" => 0
"maxClick" => 20
"companyName" => "Project B"
"email" => "user6#email.com"
]
]
So far, I have following lines of codes. This is grouping my array based on fieldName = companyName But I am not able to add emails belonging to companyname under an array.
foreach ($records as $record) {
if (!array_key_exists($record[$fieldName], $groupedArray)) {
$groupedArray[$record[$fieldName]] = [];
}
$groupedArray[$record[$fieldName]] = $record;
$groupedArray[$record[$fieldName]]['email'] = [];
if (!in_array($record['email'], $groupedArray[$record[$fieldName]]['email'], true)) {
$groupedArray[$record[$fieldName]]['email'][] = $record['email'];
}
}
My code is adding only last email to the array.
"Project A" => [
....
"email" => array:1 [
0 => "user3#email.com"
]
Can anybody help me what I am missing here ?
I am using Laravel. I have a complex array and i want to make it more readable and I am not sure how to convert it into more readable form and i want to pass only the required information. this is loop
foreach($dataArray as $value){
$addressArray[] = [
'id' => $value['place_id'],
'address'=> $value['terms'],
];
}
dd($addressArray);
}
and this array returns response in following format and I want to format this array
array:5 [
0 => array:4 [
"id" => "ChIJH6WK3tQ0K4gRm0XKDAjRGfA"
"label" => "12 York Street, Toronto, ON, Canada"
"value" => "12 York Street, Toronto, ON, Canada"
"address" => array:5 [
0 => array:2 [
"offset" => 0
"value" => "12"
]
1 => array:2 [
"offset" => 3
"value" => "York Street"
]
2 => array:2 [
"offset" => 16
"value" => "Toronto"
]
3 => array:2 [
"offset" => 25
"value" => "ON"
]
4 => array:2 [
"offset" => 29
"value" => "Canada"
]
]
]
]
2 => array:4 [
"id" => "ChIJJa4u6nqipBIRthG2uMmbv2M"
"label" => "Avinguda de Rius i Taulet, 12, Barcelona, Spain"
"value" => "Avinguda de Rius i Taulet, 12, Barcelona, Spain"
"address" => array:4 [
0 => array:2 [
"offset" => 0
"value" => "Avinguda de Rius i Taulet"
]
1 => array:2 [
"offset" => 27
"value" => "12"
]
2 => array:2 [
"offset" => 31
"value" => "Barcelona"
]
3 => array:2 [
"offset" => 42
"value" => "Spain"
]
]
]
3 => array:4 [
"id" => "ChIJUXFxe6qMwokRT2J7mbHQE3o"
"label" => "1250 Waters Place, Bronx, NY, USA"
"value" => "1250 Waters Place, Bronx, NY, USA"
"address" => array:5 [
0 => array:2 [
"offset" => 0
"value" => "1250"
]
1 => array:2 [
"offset" => 5
"value" => "Waters Place"
]
2 => array:2 [
"offset" => 19
"value" => "Bronx"
]
3 => array:2 [
"offset" => 26
"value" => "NY"
]
4 => array:2 [
"offset" => 30
"value" => "USA"
]
]
]
4 => array:4 [
"id" => "ChIJfz6STcJYwokRFONBxt9Ytvs"
"label" => "1275 York Avenue, New York, NY, USA"
"value" => "1275 York Avenue, New York, NY, USA"
"address" => array:5 [
0 => array:2 [
"offset" => 0
"value" => "1275"
]
1 => array:2 [
"offset" => 5
"value" => "York Avenue"
]
2 => array:2 [
"offset" => 18
"value" => "New York"
]
3 => array:2 [
"offset" => 28
"value" => "NY"
]
4 => array:2 [
"offset" => 32
"value" => "USA"
]
]
]
]
However I want to format data in this format.
array:5 [
0 => array:4 [
"id" => "ChIJH6WK3tQ0K4gRm0XKDAjRGfA"
"label" => "12 York Street, Toronto, ON, Canada"
"value" => "12 York Street, Toronto, ON, Canada"
"address" => [
'number' => 12,//from address array, index 0 value
'street'=>'York Street',//from address array, index 1 value
'city'=>'Tornonto,//from address array, index 2 value
'state=>'ON',//from address array, index 3 value
'country'=>'Canada'//from address array, index 4 value
];
}
Any help? Thanks
You can set the value by referencing each index of the terms key manually
$number = '';
$street = '';
$city = '';
$state = '';
$country = '';
if (isset($value['terms'][0]['value']))
$number = $value['terms'][0]['value'];
if (isset($value['terms'][1]['value']))
$street = $value['terms'][1]['value'];
if (isset($value['terms'][2]['value']))
$city = $value['terms'][2]['value'];
if (isset($value['terms'][3]['value']))
$state = $value['terms'][3]['value'];
if (isset($value['terms'][4]['value']))
$country = $value['terms'][4]['value'];
$addressArray[] = [
'id' => $value['place_id'],
'label' => $value['description'],
'value' => $value['description'],
'address' => [
'number' => $number,
'street' => $street,
'city' => $city,
'state' => $state,
'country' => $country
]
];
I'm dealing with a specifically shaped array in Laravel where items are grouped by keys that represent names of categories.
In below example I have elements from categories 1, 5 and 76. Each element has a key called order which is a nullable integer that should indicate the order within this category.
array:3 [
"category_1" => array:4 [
1 => array:2 [
"option_name" => "Some name 1"
"option_items" => array:1 [
0 => array:5 [
"id" => 1
"price" => 200
"time" => 0
"order" => 999
]
]
]
2 => array:2 [
"option_name" => "Some name 2"
"option_items" => array:1 [
0 => array:5 [
"id" => 2
"price" => 780
"time" => 5
"order" => null
]
]
]
3 => array:2 [
"option_name" => "Some name 3"
"option_items" => array:1 [
0 => array:5 [
"id" => 3
"price" => 400
"time" => 4
"order" => null
]
]
]
4 => array:2 [
"option_name" => "Some name 4"
"option_items" => array:1 [
0 => array:5 [
"id" => 4
"price" => 300
"time" => 2
"order" => 434
]
]
]
]
"category_5" => array:2 [
6 => array:2 [
"option_name" => "Some name 5"
"option_items" => array:1 [
0 => array:5 [
"id" => 6
"price" => 890
"time" => 3
"order" => null
]
]
]
7 => array:2 [
"option_name" => "Some name 6"
"option_items" => array:1 [
0 => array:5 [
"id" => 7
"price" => 1290
"time" => 5
"order" => null
]
]
]
]
"category_76" => array:2 [
10 => array:2 [
"option_name" => "Some name 7"
"option_items" => array:1 [
0 => array:5 [
"id" => 10
"price" => 320
"time" => 4
"order" => 33
]
]
]
11 => array:2 [
"option_name" => "Some name 8"
"option_items" => array:2 [
0 => array:5 [
"id" => 11
"price" => 600
"time" => 0
"order" => 500
]
1 => array:5 [
"id" => 12
"price" => 2000
"time" => 9
"order" => 500
]
]
]
]
]
I simply want to sort this entire array per category by order key ascending (with nulls at the end), so for example the order of elements in category_1 would be:
Some name 4 (order of 434)
Some name 1 (order of 999)
Some name 2 (order is nor present / null)
Some name 3 (same as above)
What did you try?
Obviously I've tried converting this plain PHP array to collection and calling sortBy with a closure via:
$sorted = collect($array)->sortBy(function ($elementsInCategory, $key) {
dd($elementsInCategory); // $key is category_1
})->toArray();
but I simply don't know what to return from the callback to make it sort the way I've described.
BTW. I don't want to complicate too much but please notice that option_items sub-array may sometimes have more than one item (key 11 from category_76) but this does not matter - elements in this sub array will always have the same order value. This inner array should not be sorted - can be left as is.
You can use Laravel collection macro for that
$Collection::macro('sortByOrder', function (string $column = 'order', bool $descending = false) {
/* #var $this Collection */
return $this->sortBy(function ($order) use ($column) {
return strtotime(((object)$order)->$column);
}, SORT_REGULAR, $descending);
});
And you can use it like
$categories->sortByOrder('order',false);
I have an array like below :
array:3 [
"2021-08-07" => array:3 [
"id" => "1"
"date" => "2021-08-07"
"numbers" => array:2 [
0 => 1
1 => 2
]
]
"2021-08-08" => array:3 [
"id" => "1"
"date" => "2021-08-08"
"numbers" => array:2 [
0 => 1
1 => 2
]
]
]
What I want to do is simply to remote the parent 2021-08-08 items because I have the date inside the array already . Now, what I have tried so far is :
$result = array_map(function ($el){
return $el[0];
},$test);
dd($result);
But it gives me error of undefined index[0] . what I want this array to look like is like below :
array:3 [
"id" => "1"
"date" => "2021-08-07"
"numbers" => array:2 [
0 => 1
1 => 2
],
"id" => "1"
"date" => "2021-08-08"
"numbers" => array:2 [
0 => 1
1 => 2
]
]
Won't just array_values() do?
array_values($test);
$test = array_values(yourArray);
$test = (object)$yourArray;
When I used
return $request->all();
network preview tab
0 => array:10 [
"id" => 1
"user_id" => 1
"product_id" => 1
"brand_id" => 1
"quantity" => 2
"total" => "90000"
"created_at" => "2021-05-21T07:48:34.000000Z"
"updated_at" => "2021-05-21T07:48:34.000000Z"
"product" => array:10 [
"id" => 1
"name" => "S1 Pro"
"category_id" => 1
"sub_category_id" => 1
"brand_id" => 1
"buy_price" => "40000.00"
"sell_price" => "45000.00"
"description" => "this is a new phone"
"created_at" => "2021-05-20T06:02:28.000000Z"
"updated_at" => "2021-05-20T06:02:28.000000Z"
]
"brand" => array:4 [
"id" => 1
"name" => "Vevo"
"created_at" => "2021-05-20T05:48:01.000000Z"
"updated_at" => "2021-05-20T05:48:01.000000Z"
]
]
1 => array:10 [
"id" => 2
"user_id" => 1
"product_id" => 2
"brand_id" => 2
"quantity" => 1
"total" => "195000"
"created_at" => "2021-05-21T08:49:47.000000Z"
"updated_at" => "2021-05-21T08:49:47.000000Z"
"product" => array:10 [
"id" => 2
"name" => "Note 20 ultra"
"category_id" => 1
"sub_category_id" => 2
"brand_id" => 2
"buy_price" => "180000.00"
"sell_price" => "195000.00"
"description" => "A new phone a new Era"
"created_at" => "2021-05-21T06:39:43.000000Z"
"updated_at" => "2021-05-21T06:39:43.000000Z"
]
"brand" => array:4 [
"id" => 2
"name" => "Samsung"
"created_at" => "2021-05-21T06:37:54.000000Z"
"updated_at" => "2021-05-21T06:37:54.000000Z"
]
]
]
But when Im using foreach loop to access every item it give error of "Trying to get property 'product' of non-object"
method I'm using
public function sellProduct(Request $request){
// dd($request->all());
$cardData = $request->all();
foreach ($cardData as $value) {
return $value->product;
}
}
please help to resolve this issue, basically I'm creating a cart app through request I'm getting all the details and product
Access the product like $value['product'] since it's an array and not an object.