0

I have the following array

$array = array("Farbe" => array("blau", "rot", "grün"),
               "Größe" => array("klein", "mittel", "groß"));

The order is random, so "Farbe" could be the first array but "Größe" could also be the first array.

In my foreach i want only the array with "Farbe". How can i tell my foreach to only loop through the "Farbe" array?

1
  • foreach($array['Farbe'] as $val). That's kinda basic stuff. Commented Sep 23, 2015 at 17:23

3 Answers 3

3

You can access by key to the array and then loop it:

foreach($array['Farbe'] as $farbe) {
    //Do something with $farbe.
}
Sign up to request clarification or add additional context in comments.

Comments

1

Here are two ways depending on what you want to do.

foreach($array['Farbe'] as $key){
    //code goes here
}

If you needed to do something with that data in your html like adding it to a table you could do something like this.

<table>
<?php foreach($array['Farbe'] as $key):?>
    <tr>
        <td><?php echo $key; ?></td>
    </tr>
<?php endforeach; ?>
</table>

Comments

1
$farbeArray = $array['Farbe'];
foreach($farbeArray as $value){
    echo $value;
}

Comments

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.