usort(), $GLOBALS, global keywords, php, array-of-objects, HELP!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil Powell

    usort(), $GLOBALS, global keywords, php, array-of-objects, HELP!

    I am having to sort an array of objects by an arbitrary object field
    name that you are not going to know in advance, therefore, I cannot
    name the sorting function accordingly nor reference the object
    accordingly:

    class ResizeView extends View {

    function displayHTML() {

    $field = $safe->getField();
    global $field;
    $safe = null;
    if (!$field) $field = 'image_name'; // DEFAULT FIELD NAME

    usort($imageArr ay, array($this, 'field_name_sor t'));
    }


    function field_name_sort (&$a, $b) {
    global $field;
    if ($a[???]->$field === $b[???]->$field) return 0;
    return (strcmp(strtolo wer($a[???]->$field),
    strtolower($b->$field));
    }

    }

    I am stuck. I can't pass the value of $field, which is required for
    the sorting to be dynamically possible according to the value of
    $field within the array of objects who happens to have an object
    property named the value of $field. How then can I sort my array of
    objects? The PHP manual offers no help for this at all since it
    doesn't involve anything LIKE this!

    Thanx
    Phil
Working...