【问题标题】:Php key is undefined, but there is keyphp key 未定义,但是有key
【发布时间】:2018-05-31 13:40:06
【问题描述】:

我正在从另一个数组中创建自己的数组,使用电子邮件字段作为键值。如果同一电子邮件有更多结果,我将 array_push 转至现有密钥。

我总是在我的数组中获取数据(通过电子邮件),这是示例

输入数据

示例数据

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

代码

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

错误

我收到错误 Undefined index: test@test.com,如果我调试我的数组,则有一个值为 'test@test.com' 的键,因此定义了键 (!)

所以var $curVal 的键是undefined

结果

所以 foreach 的目标是用相同的电子邮件过滤数组中的所有对象,示例如下:

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

];

【问题讨论】:

  • @Kaddath 让它成为答案
  • 我会说整个 if() 是不必要的。检查是否设置了数组,如果没有,则在此处创建一个空数组,然后将数据添加到空数组中。您不需要检查它是否存在,您可以将数据推送到那里。 PHP 将在您将数据推送到那里时创建数组。
  • 示例数据不是有效的PHP。
  • @Kaddath 谢谢,简单而干净的解决方案......我忽略了那个错误:D
  • @Kaddath 请在下面添加您的答案,我会将其标记为正确的答案以防止其他人的 cmets:D

标签: php arrays undefined-index array-filter


【解决方案1】:

$curVal = $data[$products->custom_product_email]; 这一行是无用的,是引发错误的那一行:你刚刚将 $data 初始化为一个空数组,逻辑上索引是未定义的。

你应该直接测试if (!isset($data[$products->custom_product_email])) {

然后解释:检索未定义的数组索引的值与isset 中的相同代码之间存在根本区别。后者评估变量的存在,您可以将不存在的内容放入其中(例如未定义的数组索引访问)。但是你不能在测试之前将它存储在变量中。

【讨论】:

    【解决方案2】:

    你没有看到错误信息吗?

    解析错误:语法错误,此代码中的 ..... 中出现意外的“{”

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

    {}更改为[]以正确生成数组。

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

    您的下一个问题在此代码中

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

    $data 是一个空数组,您在上面初始化了 2 行,因此它不包含任何键或数据!

    【讨论】:

      【解决方案3】:

      检查$data[$products->custom_product_email] 是否已在$data 数组中设置

      试试这个代码

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

      【讨论】:

      • 我会给你机会添加一些 cmets。所有答案都应该有 cmets 或解释说明它做了什么(或做得更好)。
      猜你喜欢
      • 2021-07-06
      • 1970-01-01
      • 2014-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多