0

Hi i need to print the names of $aSpelers and $aRugnummers when array $Posities of the $aSpelers is 'verdediger'

So for example:
Janmaat 7 Verdediger
de Vrij 3 Verdediger

So the first value of the array doesnt has to print out because it's not a 'verdediger'

Here are the arrays i have to use

$aSpelers = array('Cilessen', 'Janmaat', 'de Vrij' , 'Vlaar', 'Blind', 'de Jong', 'Sneijder');
$aRugnummers = array(1, 7, 3, 2, 5,8, 10 );
$Posities = array('doel', 'verdediging', 'verdediging', 'verdediging', 'verdediging','middenveld','middenveld'); 

I have to use a foreach loop this is what i have already

foreach()
{

}
2
  • 1
    Why you dont create only one array and using 3 different arrays? Commented Jun 16, 2016 at 13:03
  • 3
    Well, you weren't kidding when you said you only had a foreach. Commented Jun 16, 2016 at 13:10

3 Answers 3

2

I suggest using a multidimensional array. This makes your goal (get it?) a lot easier.

$aSpelers = array(
    array(
        'naam' => 'Cilessen',
        'rugnummer' => '1',
        'positie' => 'doel',
    ),
    array(
        'naam' => 'Janmaat',
        'rugnummer' => '7',
        'positie' => 'verdediging',
    ),
    array(
        'naam' => 'de Vrij',
        'rugnummer' => '3',
        'positie' => 'verdediging',
    ),
    array(
        'naam' => 'Vlaar',
        'rugnummer' => '2',
        'positie' => 'verdediging',
    ),
    array(
        'naam' => 'Blind',
        'rugnummer' => '5',
        'positie' => 'verdediging',
    ),
    array(
        'naam' => 'de Jong',
        'rugnummer' => '8',
        'positie' => 'middenveld',
    ),
    array(
        'naam' => 'Sneijder',
        'rugnummer' => '10',
        'positie' => 'middenveld',
    ),
);

foreach ($aSpelers as $speler) {
    if ($speler['positie'] == 'verdediger') {
        echo $speler['naam'].' heeft rugnummer '.$speler['rugnummer'].' en speelt positie '.$speler['positie'].'<br />';
    }
}

That should do the job.

Edit

I added an if condition to check if the speler is a verdediger and only echo if that is the case.

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

Comments

1

All array contain same number of value so you can use for loop

for ($i = 0; $i < count($aSpelers); $i++) {
    if ($Posities[$i] == "verdediging") {// check condition to match Verdediger
        echo $aSpelers[$i] . " " . $aRugnummers[$i] . " " . $Posities[$i];
    }
}

Comments

0

Use like this for foreach. change array as object and use

$object = (object) $Posities;
$i=0;
foreach($object as $cont){
    if($cont=="verdediging")
    echo  $aSpelers[$i]." - ".$aRugnummers[$i]." - ".$aRugnummers[$i]."<br>";
$i++;
}

1 Comment

You can also use the key as $i.

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.