Reputation: 2975
I have a JSON array I am sorting with PHP. The JSON is sent to the server with AJAX. For some reason, my sort will only work for most of the indexes in my JSON array.
Here is the class
class FieldSorter {
public $field;
function __construct($field) {
$this->field = $field;
}
function sortIt($a, $b) {
if ($a[$this->field] == $b[$this->field]) return 0;
return ($a[$this->field] > $b[$this->field]) ? 1 : -1;
}
}
A sample* of my JSON would be something similar to
record {
"key": "AAA",
"default_title": "SOME DEFAULT TITLE",
"big_stamp": "101515004186",
"date_stamp": "1015",
"time_stamp": "15004186",
"real_title": "SOME TITLE",
"display_title": "SOME TITLE (3)",
"display_stamp": "Oct 15th - 3:00:41 pm"
}
I can sort by:
For some reason It shows as undefined index when I search by real_title
or display_title
I have verified spelling in all instances.
EDIT Error output to console from AJAX
Notice: Undefined index: display_title
Upvotes: 0
Views: 46
Reputation: 782498
The error implies that some of the array elements are missing some fields, so you need to handle missing data.
function sortIt($a, $b) {
$field_a = isset($a[$this->field]) ? $a[$this->field] : '';
$field_b = isset($b[$this->field]) ? $b[$this->field] : '';
if ($field_a == $field_b) return 0;
return ($field_a > $field_b) ? 1 : -1;
}
Upvotes: 1