Question

How to avoid the double "my favorite colors" from code below? What I need is to SKIP double my favorite colors. Thanks before...

<?php

$color = array(
            "blue"=> array("status"=>"my favorite colors"),
            "green"=> array("status"=>"my favorite colors"),
            "red"=> array("status"=>"NOT my favorite colors")
                );

    foreach ( $color as $myfav ) {

        echo $myfav['status']."<br>";

        }

?>
Was it helpful?

Solution

You can consider this as an example:

$color = array(
    "blue"=> array("status"=>"my favorite colors"),
    "green"=> array("status"=>"my favorite colors"),
    "red"=> array("status"=>"NOT my favorite colors")
);

$favorite = array();
foreach($color as $key => $myfav) {
    if(!in_array($myfav['status'], $favorite)) {
        $favorite[$key] = $myfav['status'];
    }
}


print_r($favorite);

Output:

Array
(
    [blue] => my favorite colors
    [red] => NOT my favorite colors
)

Put them first inside the array, then check if already exists, then it will just skip.

EDIT: If you want to get the keys (you want to keep them, because you both like blue and green) you can use this:

foreach($color as $key => $myfav) {
    $exists = array_search($myfav['status'], $favorite);
    if($exists === false) {
        $favorite[$key] = $myfav['status'];
    } else {
        unset($favorite[$exists]);
        $exists .= ', ' . $key;
        $favorite[$exists] = $myfav['status'];
    }
}

Output:

Array
(
    [blue, green] => my favorite colors
    [red] => NOT my favorite colors
)

OTHER TIPS

use continue; this will end the current iteration in a loop, make sure to place it before the any code you DO NOT need to perform that iteration

<?php

$color = array(
            "blue"=> array("status"=>"my favorite colors"),
            "green"=> array("status"=>"my favorite colors"),
            "red"=> array("status"=>"NOT my favorite colors")
                );

    foreach ( $color as $myfav ) {

        if($myfav['status'] == 'my favorite colors') continue;

        echo $myfav['status']."<br>";

        }

?>

EDIT / FURTHER INFO: if you wanted to run the iteration the first time you get a condition you can increment an integer every time the condition appears and then use continue; when it reaches 2, or other number

This will help you,

<?php

$color = array(
            "blue"=> array("status"=>"my favorite colors"),
            "green"=> array("status"=>"my favorite colors"),
            "red"=> array("status"=>"NOT my favorite colors")
                );

    foreach ( $color as $myfav ) {
        $lastStatus = '';
        if ($myfav['status'] != $lastStatus) {
            echo $myfav['status']."<br>";
        }
        $lastStatus = $myfav['status'];

        }

?>

If you want any other help, just let me know.

$arr = array();

foreach ($color as $key => $myfav) $arr[$key] = $myfav['status']; //strtolower()?

foreach (array_unique($arr) as $key => $myfav)
{
    //$color[$key]
}

unset($arr);

Use a temporary variable with keys and 'status' text from $color to apply the array_unique function. Finally we iterate unique array with original keys from $color.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top