I'm trying to add items to an associative array, this is for a Zend form (the select options) but for some reason the values are not the same as the option.
I am using
$thearray[$test->address1] = $test->address1;
which I would expect both the key and the value to be exactly the same (the value of $test->address1
). What is happening is that the key is numbered (0,1,2,3,4 etc) but the value is correct, does anyone have any ideas why?
Here is my code
$tests = json_decode($result);
$testtwo = $tests->_embedded->house ;
$thearray = array();
foreach ($testtwo as $test) {
$i = $test->address1;
$thearray[$test->address1] = $test->address1;
};
and then the form is created like this
$this->add(array(
'name' => 'address',
'type' => 'Select',
'options' => array(
'label' => 'Address',
'value_options' => $thearray,
'disable_inarray_validator' => true,
),
'attributes' => array(
'class' => 'form-control',
'id' => 'propertyaddress',
),
));
var_dump of the array would be
array(365) {
[0]=> string(18) "1 property address"
[1]=> string(27) "2 another address"
[2]=> string(18) "3 another addresst"
....
var_dump of $test (inside the loop)
object(stdClass)#271 (11) {
["id"]=> string(1) "1" ["address1"]=> string(22) "1 property address"
["address2"]=> NULL
["town"]=> string(10) "the town"
["city"]=> string(10) "The city"
["county"]=> string(10) "The county" ["postcode"]=> string(8) "NN11 1NN"
...
print_r of $result would be
{
"_links":{
"self":{
"href":"http:\/\/shop.dev\/house?page=1"
},
"first":{
"href":"http:\/\/shop.dev\/house"
} ,
"last":{
"href":"http:\/\/shop.dev\/house?page=1"
}
},
"_embedded" :{
"house":[
{
"id":"1",
"address1":"1 property address",
"address2":null,
"town":"town",
"city":"city",
"county":"county",
"postcode":"NN11 1NN",
"branch":"BRANCH NAME",
"propertytype":"property type",
"landlord":"Landlord name",
"_links":{
"self":{
"href":"http:\/\/shop.dev\/house\/1"
}
}
}