I have a JSON decoded array in php with foreach that includes a select option in a form every time I select an option from this drop down it returns the last value in the list. The option is just the selector not the value I'm trying to save. I'm trying to save the flatnumber,building,address1,address2,town,county variables into hidden inputs. How do I go about this? Any help really appreciated!!

    $api_url = 'http://rest.api?postcode=' . urlencode($_GET['postcode']);
    $api_json = file_get_contents($api_url);
    $api_array = json_decode($api_json, true);
    $address = $api_array['addresses'];
    foreach($address as $addresses){
    echo '<option>'.$addresses['address'].'</option>';
        $flatnumber = $addresses['flatNumber'];
        $address1 = $addresses['building'];
        $address2 = $addresses['address1'];
        $town = $addresses['town'];
        $county = $addresses['county'];
    }
Member Avatar for diafol

I really am lost with regard to your snippet. You're echoing an option on each iteration of the loop, which makes me think that this php code is jumbled up in the middle of some HTML markup. Also the $flatnumber (and all other vars following it) are being overwritten at each iteration, so be the end of the loop, they'll contain the data from the last item in the loop.

Here's a more full example, this should give you a better understanding what I am trying to achieve

$api_url = 'http://rest.api?postcode=' . urlencode($_GET['postcode']);
$api_json = file_get_contents($api_url);
$api_array = json_decode($api_json, true);
print_r($api_array);
$address = $api_array['addresses'];

?>
        <div class="row">
            <div id="area-chart-spline" style="width: 100%; height: 300px; display: none;">
            </div>
            <div class="col-md-6">
                <div class="panel panel-blue" style="background:#FFF;">
                    <div class="panel-heading">Address</div>
                    <div class="panel-body pan">
                        <form action='' method='post' border='0'>                            
                        <div class="form-body pal">
                            <div class="row">
                                <div class="col-md-2">Select Address</div>                                
                                <div class="col-md-10">
                                    <div class="form-group">
                                        <select>
                                            <?php foreach($address as $addresses){
                                                    echo '<option>'.$addresses['address'].'</option>';
                                                    $flatnumber = $addresses['flatNumber'];
                                                    $address1 = $addresses['building'];
                                                    $address2 = $addresses['address1'];
                                                    $town = $addresses['town'];
                                                    $county = $addresses['county'];
                                                    $postcode = $addresses['postCode'];
                                                } ?>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <input type='hidden' name='address1' value='<?php echo $flatnumber.' '.$address1; ?>' />
                            <input type='hidden' name='address2' value='<?php echo $address2; ?>' />
                            <input type='hidden' name='town' value='<?php echo $town; ?>' />
                            <input type='hidden' name='county' value='<?php echo $county; ?>' />
                            <input type='hidden' name='postcode' value='<?php echo $postcode; ?>' />
Member Avatar for diafol

Yes well this is my point. You've got the variables going through the foreach loop (being overwritten many times), ending up with the data for the last "record". Then after exiting the loop you're echoing out the data for the last record in the hidden inputs.

So confused as to what you're trying to achieve with this.

Hello, as diafol wrote (twice) the way you are doing it keeps only the last record data. @see foreach at php.net . There are many ways to do it client side without all these hidden fields (e.g. data attributes in the option) but there is many simpler (and more secure) ways server side. E.g. (I will use your own code)

$addresses = $api_array["addresses"];
$_SESSION["addresses"] = $addresses;
for($i = 0; $i < count($addresses); $i++)
{
    echo "<option value='".$i."'>".$addresses[$i]["address"]."</option>";
}

That way when you will receive the value of this select from post you will just get the array that has the same index from the session stored variable

Ok so your code brings up the full address but I need to get the values from the individual variables saved.

So $address[$i]["address"] is the full address, but I don't want to save this value I want this to select the relevant values from the json_decode

$address[$i]['flatNumber'];
$address[$i]['building'];
$address[$i]['address1'];
$address[$i]['town'];
$address[$i]['county'];
$address[$i]['postCode'];

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.