-1

I want to sort php json data from multidimensional array using javascript or jquery my php is

$array = array();
$x = 0;
while($row = $get_images->fetch_assoc()){
    $name = $row['name'];
    $image_type = $row['image_type'];
    $caption = stripslashes($row['caption']);
    $arr = array('name' => $name, 'type' => $image_type, 'caption' => $caption);
    $array[] = $arr;
    //array_push($array, $$arr);
    $x++;
}

echo json_encode($array);

the result looks like this:

[{"name":"2323dffd","type":"jpg","caption":"ddd"},{"name":"323232323","type":"jpg","caption":"dddfdf"},{"name":"dffdd","type":"jpg","caption":"dfdfdfere"}]

I want append the data to a document prefebly using a while loop in javascript but I am not sure how to use the data from the json file (I am not sure if it is valid). I tried some solutions such as $.each but they did not work. This is my JS:

$(function($){

    $.getJSON("http://www.xxxxx.com/json.php", function(json) {
        while(){
                      //your code here
                      $("#content").append(json...);
                    }
    });

});

1 Answer 1

3

Not sure how your $.each() doesn't work, but this will work fine.

$.each(json, function(i,obj){
    console.log(obj);
});

Where obj will be:

{"name":"2323dffd","type":"jpg","caption":"ddd"}

And you can do

console.log(obj.name);

and so on.

Further, here's a jsFiddle illustrating your JSON iteration.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.