简体   繁体   English

php键未定义,但是有键

[英]Php key is undefined, but there is key

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. 如果同一封电子邮件有更多结果,则我将array_push为现有密钥。

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 (!) 我收到错误Undefined index: test@test.com ,如果调试阵列,则存在键值为'test@test.com'键,因此已定义键(!)

so var $curVal key is undefined 因此var $curVal键是undefined

Result 结果

So the goal of foreach is to filter all objects in array with same email, here is the example: 因此,foreach的目标是用同一封电子邮件过滤数组中的所有对象,这是示例:

$data = [
  'test@test.com' => [
    0 => {data},
    1 => {data},
    ...
  ],
  'bla@test.com' => [
    0 => {data},
    1 => {data},
    ...
  ],

];

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! $data是您在上面的2行中初始化的空数组,因此它不包含任何键或数据!

this line $curVal = $data[$products->custom_product_email]; 这行$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. 是没有用的,并且是引发错误的原因:您只是将$ data初始化为一个空数组,逻辑上未定义索引。

You should test directly if (!isset($data[$products->custom_product_email])) { 您应该直接测试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 . 然后说明:检索未定义的数组的索引值与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. 但是您不能在测试之前将其存储在变量中。

Check, if $data[$products->custom_product_email] is already set in $data array 检查$data[$products->custom_product_email]是否已在$data数组中设置

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM