1

i am sending an ordered json_encode list of some MySQL tables, from php, but when i retrieve it with jquery it is not in order any more? everything works fine and in order on the php side. it's the client side that i'm having trouble with. i think the problem is that i'm sending a multidimensional array from php as json. what would be the most efficient solution? also why has the order changed when retrieved by jQuery.

PHP CODE:

$user_data = array();

while($row = mysql_fetch_array($retval, MYSQL_ASSOC){
    $user_id = $row['user_id'];

    if(!isset($user_data[$user_id]){
        $user_data[$user_id] = array(
            'first_name'    => $row['first_name'],
            'last_name'     => $row['last_name'],
            'dept'          => $row['dept'],
            'quals'         => array()
        );
    }

    $quals = array(
        'qual_cat'    => $row['qual_cat'],
        'qual'        => $row['qual'],
        'count'       => $row['count']
    )

    $user_data[$user_id]['quals'][] = $quals;
}


echo json_encode($user_data);

jQuery:

$.post('page.php', function(post){
    $.each(post, function(i,data){
        alert(data.first_name+' '+data.last_name+' - '+data.dept);
    });
});

PHP VAR_DUMP:

array
10 => 
array
  'first_name' => string 'David' (length=5)
  'last_name' => string 'Dan' (length=3)
  'dept' => string 'web' (length=3)
  'count' => string '5' (length=1)
  'quals' => 
    array
      0 => 
        array
          ...
      1 => 
        array
          ...
      2 => 
        array
          ...
      3 => 
        array
          ...
      4 => 
        array
          ...
4
  • Give us that data as var_dump please Commented Mar 22, 2012 at 9:06
  • Are you using Chrome? This could be the issue - stackoverflow.com/questions/640745/… Commented Mar 22, 2012 at 9:07
  • @safarov i just updated my question to include the var_dump on the bottom Commented Mar 22, 2012 at 9:15
  • @benedict_w I am using chrome i'll look in to that Commented Mar 22, 2012 at 9:15

1 Answer 1

1

In php, array is by default associative, so that's why you have this behavior as associative array order is not guaranteed (as per explanation in the link given by benedict_w).

To overcome this, you could try the following:

echo json_encode(array_values($user_data));

This will effectively turn your json from

["10":{prop1:val1, prop2:val2}, "25":{prop1:val1, prop2:val2}]

into

[{prop1:val1, prop2:val2}, {prop1:val1, prop2:val2}]

If you need to keep track of the id, put it inside your user_data in your php:

if(!isset($user_data[$user_id]){
    $user_data[$user_id] = array(
        'id' => $user_id,
        'first_name'    => $row['first_name'],
        'last_name'     => $row['last_name'],
        'dept'          => $row['dept'],
        'quals'         => array()
    );
}
Sign up to request clarification or add additional context in comments.

3 Comments

You are wrong about the associative by default part. Its numeric
sg.php.net/manual/en/language.types.array.php An array in PHP is actually an ordered map. A map is a type that associates values to keys.
@ddan, Even performance wise, array_values() is slower. Check here

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.