I am making my own array from another one, using email field as key value. If there is more results with same email I am amking array_push
to existing key.
I am getting always data in my array (with email) and here is the example
Input data
Example data
$saved_data = [
0 => ['custom_product_email' => 'test@test.com',...],
1 => ['custom_product_email' => 'test@test.com',...],
2 => ['custom_product_email' => 'bla@test.com',...],
3 => ['custom_product_email' => 'bla@test.com',...],
...
];
Code
$data = [];
foreach ($saved_data as $products) {
$curVal = $data[$products->custom_product_email];
if (!isset($curVal)) {
$data[$products->custom_product_email] = [];
}
array_push($data[$products->custom_product_email], $products);
}
Error
I am getting error Undefined index: test@test.com
and if I debug my array, there is key with value of 'test@test.com'
, so key is defined (!)
so var $curVal
key is undefined
Result
So the goal of foreach is to filter all objects in array with same email, here is the example:
$data = [
'test@test.com' => [
0 => {data},
1 => {data},
...
],
'bla@test.com' => [
0 => {data},
1 => {data},
...
],
];