1

I have the following array:

<?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' ),
        ),
    ),
);
?>

The array holds the relevant data per each artist. Multidimensional, as seen above. I used a foreach loop to iterate through all of them then a second one to loop through the main keys. However, when I try to use a key name, I get "undefined offset." I tried the numeric offset and that gives me "illegal string offset" instead.

        <?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; ?>

Some additional information: the array is contained in its own phtml file and included in the phtml files where it's necessary. I'm using PHP 7.2.24 and this is for a Magento 2 site. If there is a Magento 2 specific solution, that would be great.

Not sure what I've done wrong and why this isn't working. I know something's wrong, else I wouldn't get an error. Am I structuring the loops incorrectly? Using the keys wrong? Would a switch be a better idea here?

10
  • 2
    $key is a string like id or images. $key[2] is the third character of the key. What do you expect $key[2][0] to be? Commented Jan 31, 2020 at 22:03
  • What is $cat, and how are you trying to match that against the array element? Commented Jan 31, 2020 at 22:06
  • And $value is either a string like Artist 01 or an array like ['engagement', 'wedding']. What are you expecting $value[2][1] to be? Commented Jan 31, 2020 at 22:07
  • I'm guessing that what you really want is if (in_array($cat, $artist['section'])) Commented Jan 31, 2020 at 22:09
  • I was expecting $key[2][0] to be the first element of its associated array, if it has one or whatever its string is. I'm trying to access the values of the keys. Probably need to call by $value instead of $key? Commented Jan 31, 2020 at 22:15

1 Answer 1

2

You don't need to loop over the elements of the associative arrays. Just check for the category in the section element.

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

The rest of the logic can use $artist['name'], $artist['featured_img'][$cat], etc.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.