【问题标题】:select2 array from API从 API 中选择 2 数组
【发布时间】:2015-10-06 09:51:36
【问题描述】:

是否可以将 json 解码数组设置为genemu_jqueryselect2_choice 输入的选项?

我尝试了以下方法:

$search = $this->createFormBuilder()
        ->add('departure', 'genemu_jqueryselect2_choice', array(
            'choices' => $airports,
            'label' => 'Departure',
        ))
        ->getForm()
    ;

导致

Warning: Illegal offset type

$airports 是一个 stdClass 对象数组,解码后看起来像这样:

array:1711 [▼
  0 => {#1036 ▼
+"lfi": "XXXXXXXX1"
+"icao": "ENEG"
+"iata": null
+"faaCode": null
+"name": " Eggemoen"
+"city": " Eggemoen"
+"countryCode": "FR"
+"latitudeDecimal": "-60.2272220"
+"longitudeDecimal": "10.3058330"
+"utcStdConversion": null
}
1 => {#1037 ▼
+"lfi": "SP80307"
+"icao": "LECO"
+"iata": "LCG"
+"faaCode": null
+"name": "A CORUNA"
+"city": "LA CORUNA"
+"countryCode": "ES"
+"latitudeDecimal": "43.3020611"
+"longitudeDecimal": "-8.3772556"
+"utcStdConversion": "+1"
}
... and so on

【问题讨论】:

    标签: json jquery-select2


    【解决方案1】:

    我将借用这个 SO php - How do I fix this illegal offset type error 的答案

    当您尝试使用对象或数组作为索引键访问数组索引时,会发生非法偏移类型错误。

    例子:

    $x = new stdClass();
    $arr = array();
    echo $arr[$x];
    //illegal offset type
    

    您不能使用数组或对象作为键。这样做会导致警告:非法偏移类型。检查您的代码。

    【讨论】:

    • 谢谢。有没有办法将 stdClass 属性之一用作键?
    猜你喜欢
    • 2012-06-10
    • 2016-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-07
    • 2013-03-21
    相关资源
    最近更新 更多