Voting

: six plus zero?
(Example: nine)

The Note You're Voting On

marty8zhang at gmail dot com
4 years ago
"This function sorts an array such that array indices maintain their correlation with the array elements they are associated with."

Note this sentence in the description has a quite important implication especially when you're working with an originally non-indexed (or say integer-indexed) array and JSON encoding/decoding.
I'm not reporting this as a bug, because the end result does fit into the description.

<?php

$myArray
= ['small_image', 'image'];
var_dump(json_encode($myArray)); // string(23) "["small_image","image"]"
var_dump(is_object($myArray)); // bool(false)

asort($myArray);
$myJson = json_encode($myArray);
var_dump($myJson); // string(31) "{"1":"image","0":"small_image"}"
var_dump(is_object($myArray)); // bool(false)

/*
* class stdClass#1 (2) {
* public $1 =>
* string(5) "image"
* public $0 =>
* string(11) "small_image"
* }
*/
var_dump(json_decode($myJson));

?>

<< Back to user notes page

To Top