2

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},
    ...
  ],

];
JohnWayne
  • 651
  • 9
  • 28
  • @Kaddath make it an answer – Andreas Dec 18 '17 at 08:48
  • I'd go as far as saying the whole if() is unnecessary. You check if the array is set, if not you create an empty array there then you add the data to the empty array. You dont need to check if it exists or not, you can push the data there either way. PHP will create the array as you push data there. – Andreas Dec 18 '17 at 08:51
  • The example data is not valid PHP. – axiac Dec 18 '17 at 08:59
  • @Kaddath thank you, simple and clean solution... I have overlooked at that mistake :D – JohnWayne Dec 18 '17 at 08:59
  • @Kaddath please add your answer below and I will mark it as right one to prevent comments from others :D – JohnWayne Dec 18 '17 at 09:23

3 Answers3

3

this line $curVal = $data[$products->custom_product_email]; is useless and is the one provoking the error: you just initialized $data as an empty array, logically the index is undefined.

You should test directly if (!isset($data[$products->custom_product_email])) {

Then explanation: there is a fundamental difference between retreiving the value of an array's index which is undefined and the same code in an isset. The latter evaluating the existence of a variable, you can put inside something that doesn't exist (like an undefined array index access). But you can't store it in a variable before the test.

Kaddath
  • 5,933
  • 1
  • 9
  • 23
2

Did you not see the error message?

Parse error: syntax error, unexpected '{' in ..... from this code

$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',...},
    ...
];

Change the {} to [] to correctly generate the array.

$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',...],
    ...
];

Your next issue is in this code

$data = [];
foreach ($saved_data as $products) {
  $curVal = $data[$products->custom_product_email];
//          ^^^^^

$data is an empty array that you initialised 2 lines above, so it does not contain any keys or data!

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0

Check, if $data[$products->custom_product_email] is already set in $data array

Try This code

$data = [];

foreach ($saved_data as $products) {
  $curVal = isset($data[$products->custom_product_email]) ? $data[$products->custom_product_email] : null;
  if (!isset($curVal)) {
    $data[$products->custom_product_email] = [];
  }
  array_push($data[$products->custom_product_email], $products);
}
Ariel Pepito
  • 619
  • 4
  • 13
  • 1
    I'll give you a chance to add some comments. All answers should have comments or explanations saying what it does (or does better). – Andreas Dec 18 '17 at 08:56