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>

4 Answers 4

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:

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

3 Comments

Thank you! This is exactly what I am looking for. Working with multidimensional arrays make my head hurt...
@airconductor, no problems. I'm glad to help. I know that the array seems quite formatted in the question, but doing an echo '<pre>'; print_r($array_name); echo '</pre>'; helps a lot. At least it does for me...
Good advice, I will give that a try next time I am dealing with arrays. Cheers
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.

}

Comments

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>

Comments

0
foreach ($colors as $color){
    $image = array_rand($color);
    echo '<li>' . $color[$image] . '</li>';
}

1 Comment

You should always provide information behind your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.