user8001250
user8001250

Reputation:

php mixing foreach and for return duplicate

Here is my code and you can run this with html output with this. i have an array (multiple select option) and i want to add selected="selected" to those equal with $districts array. so i write a foreach for array to string (select options) then a for loop for explode $districts

foreach ($readJson as $key => $value){
    if($city == $value["cityid"]){
        for ($i=0; $i<$length; $i++) {
            if($districts[$i] == $value["id"]){
                $selected = "selected='selected'";
            } else {
                $selected = "";
                //break 2;
            }
            echo "<option ".$selected." value='".$value["id"]."'>".$value["title"]."</option>";
        }
    }
}

Selected="selected" works fine but the problem is first for each repeated again in for loop, i don't know how can i stop repeating items, just used break but no success. Please see whole code in ideone, what i have done wrong?

My goal is to achieve this result in html:

<select name="Select-District[]" multiple="multiple">
<option selected="selected" value="1">test1</option>
<option selected="selected" value="2">test2</option>
<option value="3">test3</option>
<option value="4">test4</option>
<option value="5">test5</option>
</select>

Upvotes: 2

Views: 60

Answers (2)

Gaurav Kumar
Gaurav Kumar

Reputation: 336

I have modified your code and i guess its working.

<?php

    $readJson = array(
        0 => array(
            'id' => 1,
            'title' => 'test1',
            'cityid' => 1
        ),
        1 => array(
            'id' => 2,
            'title' => 'test2',
            'cityid' => 1
        ),
        2 => array(
            'id' => 3,
            'title' => 'test3',
            'cityid' => 1
        ),
        3 => array(
            'id' => 4,
            'title' => 'test4',
            'cityid' => 1
        ),
        4 => array(
            'id' => 5,
            'title' => 'test5',
            'cityid' => 1
        )
    );

    $city = 1;
    $districts = explode(',' , '2,1');
    $length = sizeof($districts);

    echo "<select name='Select-District[]' multiple='multiple'>";

    $printed = array();

    foreach ($readJson as $key => $value)
    {
        if($city == $value["cityid"])
        {
            if(in_array($value["id"], $districts))
                $selected = "selected='selected'";
            else 
                $selected = "";

            echo "<option ".$selected." value='".$value["id"]."'>".$value["title"]."</option>";          
        }
    }
    echo "</select>";

?>

Hope it works.

Upvotes: 1

user2652134
user2652134

Reputation:

You need in_array instead of looping it again.

foreach ($readJson as $key => $value){
    if($city == $value["cityid"]){
        if(in_array($value["id"], $districts)){
            $selected = "selected='selected'";
        } else {
            $selected = "";
        }
        echo "<option ".$selected." value='".$value["id"]."'>".$value["title"]."</option>";
    }
}

Upvotes: 2

Related Questions