【问题标题】:PHP Getting "undefined offset" when Trying to Access Elements in Multidimensional Arrays [duplicate]尝试访问多维数组中的元素时PHP获取“未定义的偏移量”[重复]
【发布时间】:2020-05-17 14:10:44
【问题描述】:

我有以下数组:

<?php
$bridal_artists = array(
    array(
        'id' => 1,
        'name' => 'Artist 01',
        'section' => array( 'engagement', 'wedding' ),
        'featured_img' => array( 'engagement' => 0, 'wedding' => 2 ),
        'images' => array(
            array( 'artist01-01.jpg', 'Classic Anne Engagement Ring' ),
            array( 'artist01-02.jpg', 'Wonky Diamond Band, 0.09ctw diamonds' ),
            array( 'artist01-03.jpg', 'Two Row Pavé Band, 0.24ctw diamonds' ),
            array( 'artist01-04.jpg', 'Narrow Pavé Diamond Band, 0.36ctw diamonds' ),
            array( 'artist01-05.jpg', 'Leslie Engagement Ring' ),
            array( 'artist01-06.jpg', 'Four Claw Pavé Diamond Engagement Ring' ),
            array( 'artist01-07.jpg', 'Louisa Engagement Ring' ),
            array( 'artist01-08.jpg', 'Original Halo Engagement Ring' ),
            array( 'artist01-09.jpg', 'Bark Textured Solitaire Engagement Ring' ),
            array( 'artist01-10.jpg', 'Alexa Engagement Ring' ),
            array( 'artist01-11.jpg', 'Dancing Diamond Engagement Ring' ),
            array( 'artist01-12.jpg', 'Trinity Engagement Ring' ),
            array( 'artist01-13.jpg', 'Scalloped Engagement Ring' ),
            array( 'artist01-14.jpg', 'Cushion Double Halo Engagement Ring' ),
            array( 'artist01-15.jpg', 'Triple Illusion Cushion Engagement Ring' ),
            array( 'artist01-16.jpg', 'Marion Engagement Ring' ),
        ),
    ),
    array(
        'id' => 2,
        'name' => 'Artist 2',
        'section' => array( 'engagement', 'wedding' ),
        'featured_img' => array( 'engagement' => 0, 'wedding' => 2 ),
        'images' => array(
            array( 'artist02-01.jpg', 'Bamboo Damascus Steel Band' ),
            array( 'artist02-02.jpg', 'Infinity Damascus Steel Band yellow gold liner and rails' ),
            array( 'artist02-03.jpg', 'Vortex Damascus Steel Band white gold liner and rails' ),
            array( 'artist02-04.jpg', 'Wood Grain Damascus Steel Band' ),
            array( 'artist02-05.jpg', 'Hailey Engagement Ring  damascus steel and gold with 0.25ct diamond' ),
            array( 'artist02-06.jpg', 'Beech Mokume-gane Band 18k yellow gold, palladium, sterling silver' ),
            array( 'artist02-07.jpg', 'Arcturus Meteorite Band, white gold, meteorite' ),
            array( 'artist02-08.jpg', 'Arcturus Meteorite Band, yellow gold, meteorite' ),
            array( 'artist02-09.jpg', 'Sirius Meteorite Band, meteorite, white gold liner and rails' ),
            array( 'artist02-10.jpg', 'Sirius Meteorite Band, meteorite, yellow gold liner and rails' ),
        ),
    ),
    array(
        'id' => 3,
        'name' => 'Artist 3',
        'section' => array( 'wedding' ),
        'featured_img' => array( 'wedding' => 1 ),
        'images' => array(
            array( 'artist03-01.jpg', 'ACE000 Mokume-gane band, 18k yellow gold, 14k white and rose gold, sterling silver, etched finish' ),
            array( 'artist03-02.jpg', 'ACE000 Mokume-gane band, 18k yellow gold, 14k white and rose gold, sterling silver, smooth finish' ),
            array( 'artist03-03.jpg', 'ZCE000 Mokume-gane band, sterling silver, palladium, tight wood-grain etched finish' ),
            array( 'artist03-04.jpg', 'ACN000 Mokume-gane band, 18k yellow gold, 14k white and rose gold, sterling silver, smooth finish' ),
            array( 'artist03-05.jpg', 'HCN000 Mokume-gane band, 14k white and rose gold, sterling silver, smooth finish' ),
            array( 'artist03-06.jpg', 'CCE000 Mokume-gane band, 14k white gold, palladium, sterling silver, etched finish' ),
        ),
    ),
    array(
        'id' => 4,
        'name' => 'Artist 4',
        'section' => array( 'engagement', 'wedding' ),
        'featured_img' => array( 'engagement' => 4, 'wedding' => 1 ),
        'images' => array(
            array( 'artist04-01.jpg', 'Cava Engagement Ring' ),
            array( 'artist04-02.jpg', 'Cava Ch Rd Band, 0.14ctw diamonds' ),
            array( 'artist04-03.jpg', 'Iris Pavé Engagement Ring, 0.19ctw diamond sides' ),
            array( 'artist04-04.jpg', 'Perth Pavé Engagement Ring and Band, 0.24ctw diamond sides' ),
            array( 'artist04-05.jpg', 'Poppy Engagement Ring, 0.22ctw diamond sides' ),
            array( 'artist04-06.jpg', 'Poppy Pavé Band, 0.17ctw sides' ),
            array( 'artist04-07.jpg', 'Sanday Engagement Ring' ),
            array( 'artist04-08.jpg', 'Scotasay Engagement Ring, 0.55ctw diamond sides' ),
        ),
    ),
);
?>

该数组包含每个艺术家的相关数据。多维,如上所示。我使用了一个 foreach 循环遍历所有这些,然后使用第二个循环遍历主键。但是,当我尝试使用键名时,我得到“未定义的偏移量”。我尝试了数字偏移,这给了我“非法字符串偏移”。

        <?php foreach ($bridal_artists as $artist): ?>
            <?php foreach ($artist as $key => $value): ?>
                <?php if($key[2][0] == $cat || $value[2][1] == $cat): ?>
                   rest of logic here
                <?php endif; ?>
            <?php endforeach; ?>
        <?php endforeach; ?>

一些附加信息:数组包含在它自己的 phtml 文件中,并包含在必要的 phtml 文件中。我正在使用 PHP 7.2.24,这是一个 Magento 2 站点。如果有 Magento 2 特定的解决方案,那就太好了。

不确定我做错了什么以及为什么这不起作用。我知道有问题,否则我不会出错。我是否错误地构建了循环?用错钥匙?在这里换一个更好的主意吗?

【问题讨论】:

  • $key 是一个类似idimages 的字符串。 $key[2] 是密钥的第三个字符。你期望$key[2][0] 是什么?
  • 什么是$cat,您如何尝试将其与数组元素进行匹配?
  • $value 要么是像Artist 01 这样的字符串,要么是像['engagement', 'wedding'] 这样的数组。你期待$value[2][1] 是什么?
  • 我猜你真正想要的是if (in_array($cat, $artist['section']))
  • 我期待 $key[2][0] 成为其关联数组的第一个元素,如果它有一个或任何它的字符串。我正在尝试访问键的值。可能需要通过 $value 而不是 $key 调用?

标签: php multidimensional-array undefined-index


【解决方案1】:

您不需要遍历关联数组的元素。只需检查 section 元素中的类别即可。

        <?php foreach ($bridal_artists as $artist): ?>
            <?php if(in_array($cat, $artist['section'])): ?>
               rest of logic here
            <?php endif; ?>
        <?php endforeach; ?>

其余逻辑可以使用$artist['name']$artist['featured_img'][$cat]

【讨论】:

    猜你喜欢
    • 2012-06-20
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 2023-01-15
    • 2013-09-17
    • 1970-01-01
    • 2020-11-01
    • 1970-01-01
    相关资源
    最近更新 更多