来自API的select2数组

时间:2015-10-06 09:51:36

标签: json jquery-select2

是否可以将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

1 个答案:

答案 0 :(得分:0)

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

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

示例:

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

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