0

I am looking to loop through the array below and then populate a <UL> with 1 random image from each color array. I am having trouble getting my head around how to access the specific color arrays... Do I need a loop? Or would array_rand() be enough? How would I go about this?

$colors = array( 
    'green' => array(
        'images/green1.jpg',
        'images/green2.jpg',
        'images/green3.jpg',
        'images/green4.jpg',
        'images/green5.jpg'
        ),
    'red' => array(
        '/images/red1.jpg',
        '/images/red2.jpg',
        '/images/red3.jpg',
        '/images/red4.jpg',
        '/images/red5.jpg'
        ),
    'blue' => array(
        '/images/blue1.jpg',
        '/images/blue2.jpg',
        '/images/blue3.jpg',
        '/images/blue4.jpg',
        '/images/blue5.jpg'
        ),
    'purple' => array(
        '/images/purple1.jpg',
        '/images/purple2.jpg',
        '/images/purple3.jpg',
        '/images/purple4.jpg',
        '/images/purple5.jpg'
        )
    );

<div>
    <span>Colors</span>
        <ul>
            <li>"1 img from 'green' array would go here"</li>
            <li>"1 img from 'red' array would go here"</li>
            <li>"1 img from 'blue' array would go here"</li>
            <li>"1 img from 'purple' array would go here"</li>
        </ul>
</div>
Mr Lister
  • 45,515
  • 15
  • 108
  • 150

4 Answers4

2

As you mentioned, array_rand() could be used, but you'll need to loop through the colors. For each one, get a random image:

$arr = array();
foreach($colors as $k=>$v){
    $arr[] = $v[array_rand($v)];
}

print_r($arr);

Output 1:

Array
(
    [0] => images/green3.jpg
    [1] => /images/red3.jpg
    [2] => /images/blue2.jpg
    [3] => /images/purple1.jpg
)

Running again:

Array
(
    [0] => images/green5.jpg
    [1] => /images/red3.jpg
    [2] => /images/blue1.jpg
    [3] => /images/purple4.jpg
)

If you want to output it like in the question, it would be something like this:

// div span ul
$arr = array();
foreach($colors as $k=>$v){
    echo '<li><img src="' . $v[array_rand($v)] . '"></li>';
}
// /div /ul

Side notes:

Community
  • 1
  • 1
FirstOne
  • 6,033
  • 7
  • 26
  • 45
  • Thank you! This is exactly what I am looking for. Working with multidimensional arrays make my head hurt... – airconductor Mar 07 '16 at 20:40
  • @airconductor, no problems. I'm glad to help. I know that the array seems quite formatted in the question, but doing an `echo '
    '; print_r($array_name); echo '
    ';` helps a lot. At least it does for me...
    – FirstOne Mar 07 '16 at 20:43
  • Good advice, I will give that a try next time I am dealing with arrays. Cheers – airconductor Mar 07 '16 at 20:45
0
foreach ($colors as $color){
    $image = array_rand($color);
    echo '<li>' . $color[$image] . '</li>';
}
zeropsi
  • 682
  • 9
  • 24
0

I would so something along the lines of

foreach ($colors as $color){
     //This gets you each color array in turn, in here you can 
     //use array_rand() to get a random entry from each array.

}
Cal Irvine
  • 1,104
  • 8
  • 17
0

This worked for me:

<?php
$colors = array(
'green' => array(
'images/green1.jpg',
'images/green2.jpg',
'images/green3.jpg',
'images/green4.jpg',
'images/green5.jpg'
),
'red' => array(
'/images/red1.jpg',
'/images/red2.jpg',
'/images/red3.jpg',
'/images/red4.jpg',
'/images/red5.jpg'
),
'blue' => array(
'/images/blue1.jpg',
'/images/blue2.jpg',
'/images/blue3.jpg',
'/images/blue4.jpg',
'/images/blue5.jpg'
),
'purple' => array(
'/images/purple1.jpg',
'/images/purple2.jpg',
'/images/purple3.jpg',
'/images/purple4.jpg',
'/images/purple5.jpg'
)
);
?>
<div>
    <span>Colors</span>
    <ul>
        <?php
        foreach ($colors as $key=>$value){
            echo '<li>'.$value[array_rand($value,1)]."</li>";
        }
?>
    </ul>
</div>
Kolter
  • 1
  • 1