duanming7961 2013-10-14 18:53
浏览 62
已采纳

JQuery,Ajax,Json,PHP多维关联数组

I am trying to loop through a multidimensional associative array retrieved via jquery/ajax from a php file. The php array looks like this:

$pink =  array ( "newarray" => array 
( "varietyOne" => array
("name" => "Poublout", "year" => 2002),
"varietyTwo" => array
("name" => "Gerarde", "year" => 2003),
"varietyThree" => array
("name" => "Encore", "year" => 1956),
"varietyFour" => array
("name" => "Toujours", "year" => 1957),
"varietyFive" => array
("name" => "J'aime", "year" => 1958),
"varietySix" => array
("name" => "Alisee", "year" => 2001)
),
"varNumber" => array
("varietyOne",
"varietyTwo",
"varietyThree",
"varietyFour",
"varietyFive",
"varietySix"
)
);
print json_encode($pink);

The js looks like this:

$(document).ready(function () {
    $('.clicker').click(function () {
        $.ajax({
            type: 'GET',
            url: 'another.php',
            dataType: 'json',
            success: function (brandon) {
                for (var i = 0; i < brandon.newarray.length; i++) {
                    var catName = brandon.varNumber[i];
                    for (var wineName in brandon.newarray[i][catName]) {
                        console.log(branond.newarray[i][catName][wineName]);
                    }
                }
            }
        });
    });
});

And here is the json rather than the php:

{"newarray":
    {"varietyOne":{"name":"Poublout","year":2002},
     "varietyTwo":{"name":"Gerarde","year":2003},
     "varietyThree":{"name":"Encore","year":1956},
     "varietyFour":{"name":"Toujours","year":1957},
     "varietyFive":{"name":"J'aime","year":1958},
     "varietySix":{"name":"Alisee","year":2001}},
 "varNumber":
    ["varietyOne","varietyTwo","varietyThree","varietyFour","varietyFive","varietySix"]}

I've tried several different loops, changing my array values, but I can't make anything work. I can call an individual key=value pair in the array, but I can't get it to loop through all values.
Thank you.

And the results of console.log(brandon)

Object { newarray={...}, varNumber=[6]}
newarray
Object { varietyOne={...}, varietyTwo={...}, varietyThree={...}, more...}
varietyFive
Object { name="J'aime", year=1958}
varietyFour
Object { name="Toujours", year=1957}
varietyOne
Object { name="Poublout", year=2002}
varietySix
Object { name="Alisee", year=2001}
varietyThree
Object { name="Encore", year=1956}
varietyTwo
Object { name="Gerarde", year=2003}
varNumber
["varietyOne", "varietyTwo", "varietyThree", 3 more...]
0   "varietyOne"
1   "varietyTwo"
2   "varietyThree"
3   "varietyFour"
4   "varietyFive"
5   "varietySix"

So, the loop now outputs to console, but I want the text to be displayed on my site. Normally I use a $('#somediv').append(brandon.(whateverelse); and the information will appear. In this case it does not.

  • 写回答

1条回答 默认 最新

  • douwanc63652 2013-10-14 19:05
    关注

    You shouldn't need the varNumber array.

    All you need is:

    for (var index in brandon.newarray) {
        console.log(index);
        console.log(brandon.newarray[index]);
        console.log(brandon.newarray[index]['name']); // or brandon.newarray[index].name
        console.log(brandon.newarray[index]['year']); // or brandon.newarray[index].year
    }
    

    In this case, index will be varietyOne, varietyTwo, etc.

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?