dsa4d4789789 2016-08-17 09:25
浏览 27
已采纳

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

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",
            "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条回答 默认 最新

  • dounangqie4819 2016-08-17 11:48
    关注

    I managed to solve this by using;

     $c = array_combine($thearray, $thearray);
    

    and then

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

    I'm not sure if this is a correct solution but it has at least solved my problem.

    Thank you all for your input

    Clint

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)