array_multisort on $GLOBALS array

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

    array_multisort on $GLOBALS array

    I'm trying to use array_multisort to sort by one of the dimensions of an
    array stored in $GLOBALS['urls'] like this:

    array_multisort ($GLOBALS['urls']['desc'], SORT_STRING, SORT_DESC);

    Each "row" in $GLOBALS['urls'] contains a value for 'href', 'desc', and
    'info' but when I try to pass one of these "columns" to array_multisort () it
    whinges about it not being an array or a sort value :-(

    Can PHP not understand arrays stored in $GLOBALS or something?


  • Pedro Graca

    #2
    Re: array_multisort on $GLOBALS array

    Frostillicus wrote:[color=blue]
    > I'm trying to use array_multisort to sort by one of the dimensions of an
    > array stored in $GLOBALS['urls'] like this:
    >
    > array_multisort ($GLOBALS['urls']['desc'], SORT_STRING, SORT_DESC);
    >
    > Each "row" in $GLOBALS['urls'] contains a value for 'href', 'desc', and
    > 'info' but when I try to pass one of these "columns" to array_multisort () it
    > whinges about it not being an array or a sort value :-([/color]

    Do a

    print_r($GLOBAL S['urls']);

    to verify the structure of your array.
    [color=blue]
    > Can PHP not understand arrays stored in $GLOBALS or something?[/color]

    Yes, it can. IMO it is /you/ who are not understanding it correctly :)


    How do you specify the first row (however you define that) of your
    array?

    $GLOBALS['urls']?????


    I guess it should be something like

    $GLOBALS['urls'][0]


    which is an array!

    $GLOBALS['urls'][0] = array('href'=>' x', 'desc'=>'y', 'info'=>'z');


    OTOH -- $GLOBALS['urls']['desc'] does not exist!


    Anyway, for what you want, I think you're better off with usort()
    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


    --
    USENET would be a better place if everybody read: : mail address :
    http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
    http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
    http://www.expita.com/nomime.html : to 10K bytes :

    Comment

    • Frostillicus

      #3
      Re: array_multisort on $GLOBALS array

      Thanks, Pedro. Here's how I'm declearing my "array":

      $GLOBALS['urls'] = array();
      $temp['href'] = html_entity_dec ode($GLOBALS['href']); // turn & into &
      $temp['desc'] = $GLOBALS['desc'];
      $temp['info'] = $GLOBALS['info'];
      $GLOBALS['urls'][] = $temp;

      Then, to loop through, I'm doing this:

      foreach ($GLOBALS['urls'] as $subarray) {
      print "\t\t<li><a
      href=\"{$subarr ay['href']}\">{$subarra y['desc']}</a></li>\n";
      }


      Am I somehow creating an array of non-arrays, which is why array_multisort ()
      doesn't know what to do with it?

      I'm no PHP expert, in fact, I'd say I'm a PHP newbie (I'm more confident
      with Perl, ASP, and Java).

      Thanks for any help you can provide.


      Comment

      • Pedro Graca

        #4
        Re: array_multisort on $GLOBALS array

        Frostillicus wrote:[color=blue]
        > Thanks, Pedro. Here's how I'm declearing my "array":
        >
        > $GLOBALS['urls'] = array();[/color]

        $GLOBALS['urls'] is an empty array now.
        [color=blue]
        > $temp['href'] = html_entity_dec ode($GLOBALS['href']); // turn &amp; into &
        > $temp['desc'] = $GLOBALS['desc'];
        > $temp['info'] = $GLOBALS['info'];[/color]

        if $temp didn't exist before these three lines,
        $temp is an array with three indexes.
        [color=blue]
        > $GLOBALS['urls'][] = $temp;[/color]

        add /one/ element to $urls (oops ... $GLOBALS['urls']) (accessible as
        $urls[0]) and assign $temp to it.

        So you have
        $urls[0]['href']
        $urls[0]['desc']
        $urls[0]['info']
        [color=blue]
        > Then, to loop through, I'm doing this:
        >
        > foreach ($GLOBALS['urls'] as $subarray) {
        > print "\t\t<li><a
        > href=\"{$subarr ay['href']}\">{$subarra y['desc']}</a></li>\n";
        > }[/color]

        $subarray['href'] and $subarray['desc'] do not exist!
        You may want to say this instead:

        foreach ($GLOBALS['urls'][0] as $subarray) {


        or


        foreach ($GLOBALS['urls'] as $subarray) {
        print "... {$subarray[0]['href']} ...";
        }


        or, better yet, review how you're using the arrays :-)

        [color=blue]
        > Am I somehow creating an array of non-arrays,[/color]

        No, that (the array of non-arrays) is what you want;
        but, in fact, you are creating an array of arrays!
        [color=blue]
        > which is why array_multisort () doesn't know what to do with it?[/color]

        array_multisort () is sorting your sub-arrays in $urls (the [0]), not the
        href, info, desc stuff.


        --
        USENET would be a better place if everybody read: : mail address :
        http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
        http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
        http://www.expita.com/nomime.html : to 10K bytes :

        Comment

        • Frostillicus

          #5
          Re: array_multisort on $GLOBALS array

          "Pedro Graca" <hexkid@hotpop. com> wrote in message
          news:slrncbohoc .15k.hexkid@ID-203069.user.uni-berlin.de...[color=blue][color=green]
          > > Then, to loop through, I'm doing this:
          > >
          > > foreach ($GLOBALS['urls'] as $subarray) {
          > > print "\t\t<li><a
          > > href=\"{$subarr ay['href']}\">{$subarra y['desc']}</a></li>\n";
          > > }[/color]
          >
          > $subarray['href'] and $subarray['desc'] do not exist!
          > You may want to say this instead:
          >
          > foreach ($GLOBALS['urls'][0] as $subarray) {
          >
          >
          > or
          >
          >
          > foreach ($GLOBALS['urls'] as $subarray) {
          > print "... {$subarray[0]['href']} ...";
          > }
          >
          >
          > or, better yet, review how you're using the arrays :-)[/color]

          $subarray['desc'] does exist because I'm looping through $GLOBALS['urls']
          quite successfully and retrieving the value for 'desc', 'href', etc for each
          row in $GLOBALS['urls'] without problem.


          Comment

          • Pedro Graca

            #6
            Re: array_multisort on $GLOBALS array

            Frostillicus wrote:[color=blue]
            > "Pedro Graca" <hexkid@hotpop. com> wrote in message
            > news:slrncbohoc .15k.hexkid@ID-203069.user.uni-berlin.de...[color=green][color=darkred]
            >> > Then, to loop through, I'm doing this:
            >> >
            >> > foreach ($GLOBALS['urls'] as $subarray) {
            >> > print "\t\t<li><a
            >> > href=\"{$subarr ay['href']}\">{$subarra y['desc']}</a></li>\n";
            >> > }[/color]
            >>
            >> $subarray['href'] and $subarray['desc'] do not exist!
            >> You may want to say this instead:
            >>
            >> foreach ($GLOBALS['urls'][0] as $subarray) {
            >>
            >>
            >> or
            >>
            >>
            >> foreach ($GLOBALS['urls'] as $subarray) {
            >> print "... {$subarray[0]['href']} ...";
            >> }
            >>
            >>
            >> or, better yet, review how you're using the arrays :-)[/color]
            >
            > $subarray['desc'] does exist because I'm looping through $GLOBALS['urls']
            > quite successfully and retrieving the value for 'desc', 'href', etc for each
            > row in $GLOBALS['urls'] without problem.[/color]


            Oops, indeed they exist. Sorry, I didn't test it.
            But $GLOBALS['urls']['desc'] does not exist.

            ========
            pedro@localhost :~$ cat xx.php
            <?php

            $GLOBALS['urls'] = array();
            $temp['href'] = '4href: something';
            $temp['desc'] = '4desc: someotherthing' ;
            $temp['info'] = '4info: yetanotherthing ';
            $GLOBALS['urls'][] = $temp;
            $temp['href'] = '7href: something';
            $temp['desc'] = '7desc: someotherthing' ;
            $temp['info'] = '7info: yetanotherthing ';
            $GLOBALS['urls'][] = $temp;
            $temp['href'] = '2href: something';
            $temp['desc'] = '2desc: someotherthing' ;
            $temp['info'] = '2info: yetanotherthing ';
            $GLOBALS['urls'][] = $temp;

            echo '$GLOBALS[\'urls\']: '; print_r($GLOBAL S['urls']);

            ?>

            pedro@localhost :~$ php xx.php
            $GLOBALS['urls']: Array
            (
            [0] => Array
            (
            [href] => 4href: something
            [desc] => 4desc: someotherthing
            [info] => 4info: yetanotherthing
            )

            [1] => Array
            (
            [href] => 7href: something
            [desc] => 7desc: someotherthing
            [info] => 7info: yetanotherthing
            )

            [2] => Array
            (
            [href] => 2href: something
            [desc] => 2desc: someotherthing
            [info] => 2info: yetanotherthing
            )

            )
            ========

            So, when you do

            array_multisort ($GLOBALS['urls']['desc'], ...);

            PHP complains.



            If I were you I'd try usort instead

            function compare_desc_de sc($a, $b) {
            if ($a['desc'] == $b['desc']) return 0;
            return $a['desc'] < $b['desc'] ? 1 : -1;
            }
            usort($GLOBALS['urls'], 'compare_desc_d esc')


            HTH

            Sorry again for the mess (I got myself into).
            --
            USENET would be a better place if everybody read: : mail address :
            http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
            http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
            http://www.expita.com/nomime.html : to 10K bytes :

            Comment

            Working...