提问人:Marisa 提问时间:2/1/2020 更新时间:2/1/2020 访问量:69
PHP 在尝试访问多维数组中的元素时出现“未定义的偏移量” [duplicate]
PHP Getting "undefined offset" when Trying to Access Elements in Multidimensional Arrays [duplicate]
问:
我有以下数组:
<?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特定的解决方案,那就太好了。
不确定我做错了什么以及为什么这不起作用。我知道出了什么问题,否则我不会出错。我是否错误地构建了循环?用错了钥匙?在这里切换会是一个更好的主意吗?
答:
2赞
Barmar
2/1/2020
#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]
评论
$key
是类似 或 的字符串。 是密钥的第三个字符。你期望成为什么?id
images
$key[2]
$key[2][0]
$cat
$value
Artist 01
['engagement', 'wedding']
$value[2][1]
if (in_array($cat, $artist['section']))