Php 如何迭代对象数组
我正在从JQuery ajax方法调用一个PHP函数。下面是ajax调用的响应: 我的问题是:如何在jquery中迭代这个数组以获得值 如果使用$.each进行迭代,我会得到“无法使用'in'运算符在数组中搜索'length' 代码如下:Php 如何迭代对象数组,php,jquery,wordpress,Php,Jquery,Wordpress,我正在从JQuery ajax方法调用一个PHP函数。下面是ajax调用的响应: 我的问题是:如何在jquery中迭代这个数组以获得值 如果使用$.each进行迭代,我会得到“无法使用'in'运算符在数组中搜索'length' 代码如下: $("#member_country").change(function() { var country = $(this).val(); //alert(country); var ajax_url_mtyp
$("#member_country").change(function() {
var country = $(this).val();
//alert(country);
var ajax_url_mtypes = ins_membership_ajax.ajax_url;
//alert(ajax_url_mtypes);
jQuery.ajax({
url:ajax_url_mtypes,
data:{country:country,
action: "ins_get_membershiptypes_from_country"},
method:'GET',
contentType: "application/json;charset=utf-8",
success:function(response) {
//var resp = $.trim(response);
alert(response);
$.each(response, function( key, value ) {
alert( key );
alert( value );
});
}
});
PHP代码:
public static function getMembershipTypesNonAdmin($country){
$rtn = array();
global $wpdb;
$table = $wpdb->prefix.self::TABLE_NAME;
$query = "SELECT * FROM {$table} where admin_only = 'false' order by member_type";
$results = $wpdb->get_results($query);
if($results){
foreach($results as $row){
$membership_type = INS_Membership_Types::loadById($row->id);
$membership_type->setDues(INS_Membership_Types::getDuesByEconomy($country, $membership_type->getMemberType()));
$rtn[] = $membership_type;
}
}
return $rtn;
}
private $dues;
public function getDues()
{
return $this->dues;
}
public function setDues($dues)
{
return $this->dues = $dues;
}
您可以通过for in循环对对象进行迭代。如下所示:
for(var prop in object){
console.log(object[prop]);
}
希望这有帮助。您可以通过for in循环迭代对象。如下所示:
for(var prop in object){
console.log(object[prop]);
}
希望这有帮助。尝试使用json。
使用
json_encode()
在php代码和
数据类型:“json”
在ajax调用中,尝试使用json。
使用
json_encode()
在php代码和
数据类型:“json”
在ajax调用中
success:function(response){
-检查这response
这里是数组还是字符串。您应该使用json_encode()将数组编码为json然后它将给出适当的json作为响应,您可以迭代through@Manish:如何检查?typeof response
@ManishGiri:我检查过了。它是一个字符串。我现在如何获取值?即使我给json_encode,它也是一个字符串success:function(response){
-检查此响应是数组还是字符串。您应该使用json_encode()将数组编码为json然后它将给出适当的json作为响应,您可以迭代through@Manish:如何检查?typeof response
@ManishGiri:我检查过了。它是一个字符串。我现在如何获取值?即使我给json_encode,它也是一个字符串