【发布时间】:2021-05-13 14:30:08
【问题描述】:
我有一个数组。 它来自这个查询:
$table_array=array();
$table_array = $mydb->get_results($mydb->prepare("SELECT sid, stitle FROM tablename"));
我成功检索数据。控制台显示通过 ajax(成功函数)返回的 json_encode:
function align_surveys(){
jQuery.ajax({
type: 'POST',
url: MyAjax.ajaxurl,
data: {"action": "align_sids"},
success:
function(data){
console.log('data: '+ data)
}
});
}
"data:":[{"sid":"525331","stitle":"First Title"},{"sid":"367775","stitle":"Second Title"}]
但是,当我运行 foreach 循环时,我在尝试访问它的元素时收到 500 错误。
这个错误:
foreach($table_array as $row){
$s_ids[] = $row[0];
}
而且,这个错误:
foreach($table_array as $row){
$s_ids[] = $row['sid'];
}
gettype 告诉我这实际上是一个数组。但是,每当我引用数组元素时,我都会出错。我无法解决它。这与方括号内的额外花括号有关吗?任何人,请指出我正确的方向。已经好几个小时了......
更新: print_r 给我看这个:
Array ( [0] => Array ( [surveyls_survey_id] => 525331 [0] => 525331 [surveyls_title] => New Survey [1] => New Survey ) [1] => Array ( [surveyls_survey_id] => 367775 [0] => 367775 [surveyls_title] => Second Survey [1] => Second Survey ) )
这在我看来就像是一个数组数组,其中键实际上是值...不知道我得到的结果如何或为什么。
【问题讨论】:
标签: php arrays for-loop curly-braces