有人可以告诉我为什么这不会创建一个关联数组?

时间:2016-08-17 09:25:38

标签: php associative-array zend-form

我试图将项目添加到关联数组中,这是针对Zend表单(选择选项)但由于某种原因,这些值与选项不同。

我正在使用

$thearray[$test->address1] = $test->address1; 

我希望密钥和值都完全相同($test->address1的值)。发生的事情是钥匙编号(0,1,2,3,4等),但价值是正确的,有没有人有任何想法?

这是我的代码

$tests = json_decode($result);

$testtwo = $tests->_embedded->house ;

$thearray = array();

foreach ($testtwo  as $test) {

    $i = $test->address1;
    $thearray[$test->address1] = $test->address1;

};

然后像这样创建表单

   $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将是

array(365) { 
    [0]=> string(18) "1 property address" 
    [1]=> string(27) "2 another address" 
    [2]=> string(18) "3 another addresst"
    ....

$ test的var_dump(在循环内)

   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" 
        ...

$ result的print_r将是

{
   "_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",
            "c‌​ity":"city",
            "county":‌​"county",
            "postcode":"‌​NN11 1NN",
            "branch":"BRANCH NAME",
            "propertytyp‌​e":"property type",
            "landlord":"L‌​andlord name",
            "_links":{
               "sel‌​f":{
                  "href":"http:\/\/‌​shop.dev\/house\/1"
               }
            }            ‌​
         }

1 个答案:

答案 0 :(得分:0)

我设法通过使用

来解决这个问题
 $c = array_combine($thearray, $thearray);

然后

 'options' => array(
             'label' => 'Address',
             'value_options' => $c,
             'disable_inarray_validator' => true,
         ),

我不确定这是否是一个正确的解决方案,但它至少解决了我的问题。

感谢大家的投入