samedi 31 juillet 2021

PHP - If statement not working properly in foreach loop

I am getting array in foreach loop,There are two keys "sequence"(unique,not repeated) and "asset_type"(can duplicate/same) and i want to check condition of every "sequence" with every "asset_type",So how can i do this ?

For example,Here is my current array

Array
(
    [id] => 78
    [sequence] => 1
    [asset_type] => 8
)
(
    [id] => 3
    [sequence] => 2
    [asset_type] => 11
)
(
    [id] => 3
    [sequence] => 3
    [asset_type] => 25
)

And here is my code which is working fine but this is too long,I am checking every "sequence" with every "asset_type", is there another way for do this ?

foreach ($feed as $key => $fd) {
    $total = count($fd);
    switch (true) {
    /*case 1 here */
         case ($fd['sequence'] == "1"):
            $asset_type = $fd['asset_type'];
            //showing articles (start)
            if ($fd['sequence'] == "1" && $fd['asset_type'] == "1") {
                    // showing html here
            }
            if ($fd['sequence'] == "1" && $fd['asset_type'] == "2") {
                    // showing html here
            }
            ... and so on ....to sequence 1 and asset_type ="n" (last asset type)
    /*case 2 here */
         case ($fd['sequence'] == "2"):
            $asset_type = $fd['asset_type'];
            //showing articles (start)
            if ($fd['sequence'] == "2" && $fd['asset_type'] == "1") {
                    // showing html here
            }
            if ($fd['sequence'] == "2" && $fd['asset_type'] == "2") {
                    // showing html here
            }
            ... and so on ....to sequence 1 and asset_type ="n" (last asset type)       
            
}           

Aucun commentaire:

Enregistrer un commentaire