#25879 [Opn->Bgs]: SORT_ASC when passed into a function can NOT be passed as a string

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default #25879 [Opn->Bgs]: SORT_ASC when passed into a function can NOT be passed as a string

    ID: 25879
    Updated by: [email]mgf@php.net[/email]
    Reported By: it at oh-barcelona dot com
    -Status: Open
    +Status: Bogus
    Bug Type: Variables related
    Operating System: Windows
    PHP Version: 4.3.2
    New Comment:

    Thank you for taking the time to write to us, but this is not
    a bug. Please double-check the documentation available at
    [url]http://www.php.net/manual/[/url] and the instructions on how to report
    a bug at [url]http://bugs.php.net/how-to-report.php[/url]

    SORT_ASC, SORT_DESC, etc. are constants, not strings, so just treat
    them as if they were numeric values:

    $sortFlag = SORT_ASC;
    $new_array = array_column_sort($someArray,'column',$sortFlag)




    Previous Comments:
    ------------------------------------------------------------------------

    [2003-10-15 08:25:18] it at oh-barcelona dot com

    Description:
    ------------
    Hello,
    I've found a perplexing bug with sorting flags. When you create a
    function to create (for example) an dynamic array_multisort() and one
    of the arguements which is passed is a SORT_ASC (or any other flag)
    which is used to create the array_multisort() it MUST be passed without
    quotes and not as a string.

    In the example below you will see that if you gettype() of the $arg the
    SORT_DESC returns an integer not a string when passed without quotes
    and a string with.

    If the SORT flag is passed as a string the array_multisort acts as if
    it were null.

    This stops me from setting the SORT flag via if statement.

    I have written a work around but this problem should be addressed
    because if you settype() a string it becomes "0".

    The function call is:
    $ new_array = array_column_sort($someArray,'column',SORT_DESC)

    The function does not work if you use:
    $sortFlag = "SORT_ASC";
    $ new_array = array_column_sort($someArray,'column',$sortFlag )



    Reproduce code:
    ---------------
    function array_column_sort(){
    $args = func_get_args();
    $array = array_shift($args);
    // make a temporary copy of array for which will fix the
    // keys to be strings, so that array_multisort() doesn't
    // destroy them
    $array_mod = array();
    foreach ($array as $key => $value)
    $array_mod['_' . $key] = $value;

    $i = 0;
    $multi_sort_line = "return array_multisort( ";
    foreach ($args as $arg) {
    $i++;
    if ( is_string($arg) ){
    foreach ($array_mod as $row_key => $row){
    $sort_array[$i][] = $row[$arg];
    }
    }else{
    $sort_array[$i] = $arg;
    }
    $multi_sort_line .= "\$sort_array[" . $i . "], ";
    }
    $multi_sort_line .= "\$array_mod );";
    eval($multi_sort_line);
    // now copy $array_mod back into $array, stripping off the "_"
    // that we added earlier.
    $array = array();
    foreach ($array_mod as $key => $value)
    $array[ substr($key, 1) ] = $value;

    return $array;
    }

    Expected result:
    ----------------
    The array should be sorted by column and SORT flag.

    Actual result:
    --------------
    The array_multisort only excepts the SORT flag without quotes and not
    as a string.


    ------------------------------------------------------------------------


    --
    Edit this bug report at [url]http://bugs.php.net/?id=25879&edit=1[/url]
    mgf@php.net Guest

  2. Similar Questions and Discussions

    1. Can be string array passed to and from an XML Web service?
      Hi all, The first methot of the sample code works well, the second returns an error*. Can be string array passed to and from an XML Web service?...
    2. #25879 [Bgs]: SORT_ASC when passed into a function can NOT be passed as a string
      ID: 25879 Updated by: alan_k@php.net Reported By: it at oh-barcelona dot com Status: Bogus Bug Type: ...
    3. #25879 [Bgs->Opn]: SORT_ASC when passed into a function can NOT be passed as a string
      ID: 25879 User updated by: it at oh-barcelona dot com Reported By: it at oh-barcelona dot com -Status: Bogus...
    4. #25879 [NEW]: SORT_ASC when passed into a function can NOT be passed as a string
      From: it at oh-barcelona dot com Operating system: Windows PHP version: 4.3.2 PHP Bug Type: Variables related Bug...
    5. Special Characters in passed Query String
      Hello all. I am a very new ASP programmer and am having problems with a special character in a query string. The first pages pulls up a record and...
  3. #2

    Default #25879 [Opn->Bgs]: SORT_ASC when passed into a function can NOT be passed as a string

    ID: 25879
    Updated by: [email]georg@php.net[/email]
    Reported By: it at oh-barcelona dot com
    -Status: Open
    +Status: Bogus
    Bug Type: Variables related
    Operating System: Windows
    PHP Version: 4.3.2
    New Comment:

    As mfg already wrote, this is not a bug!


    Previous Comments:
    ------------------------------------------------------------------------

    [2003-10-16 03:14:14] it at oh-barcelona dot com

    Hello,
    I realize that it is a constant but I was clear enough in my
    description. The sort flag that I am using comes from a
    $_GET[sortFlag]. The bug that I would like you look at is that there
    is no way to create a constant from a string. So if the needed
    constant is passed from one page to another it must be as string and
    therefore can not be used in a function call.

    ------------------------------------------------------------------------

    [2003-10-15 11:22:47] [email]mgf@php.net[/email]

    Thank you for taking the time to write to us, but this is not
    a bug. Please double-check the documentation available at
    [url]http://www.php.net/manual/[/url] and the instructions on how to report
    a bug at [url]http://bugs.php.net/how-to-report.php[/url]

    SORT_ASC, SORT_DESC, etc. are constants, not strings, so just treat
    them as if they were numeric values:

    $sortFlag = SORT_ASC;
    $new_array = array_column_sort($someArray,'column',$sortFlag)



    ------------------------------------------------------------------------

    [2003-10-15 08:25:18] it at oh-barcelona dot com

    Description:
    ------------
    Hello,
    I've found a perplexing bug with sorting flags. When you create a
    function to create (for example) an dynamic array_multisort() and one
    of the arguements which is passed is a SORT_ASC (or any other flag)
    which is used to create the array_multisort() it MUST be passed without
    quotes and not as a string.

    In the example below you will see that if you gettype() of the $arg the
    SORT_DESC returns an integer not a string when passed without quotes
    and a string with.

    If the SORT flag is passed as a string the array_multisort acts as if
    it were null.

    This stops me from setting the SORT flag via if statement.

    I have written a work around but this problem should be addressed
    because if you settype() a string it becomes "0".

    The function call is:
    $ new_array = array_column_sort($someArray,'column',SORT_DESC)

    The function does not work if you use:
    $sortFlag = "SORT_ASC";
    $ new_array = array_column_sort($someArray,'column',$sortFlag )



    Reproduce code:
    ---------------
    function array_column_sort(){
    $args = func_get_args();
    $array = array_shift($args);
    // make a temporary copy of array for which will fix the
    // keys to be strings, so that array_multisort() doesn't
    // destroy them
    $array_mod = array();
    foreach ($array as $key => $value)
    $array_mod['_' . $key] = $value;

    $i = 0;
    $multi_sort_line = "return array_multisort( ";
    foreach ($args as $arg) {
    $i++;
    if ( is_string($arg) ){
    foreach ($array_mod as $row_key => $row){
    $sort_array[$i][] = $row[$arg];
    }
    }else{
    $sort_array[$i] = $arg;
    }
    $multi_sort_line .= "\$sort_array[" . $i . "], ";
    }
    $multi_sort_line .= "\$array_mod );";
    eval($multi_sort_line);
    // now copy $array_mod back into $array, stripping off the "_"
    // that we added earlier.
    $array = array();
    foreach ($array_mod as $key => $value)
    $array[ substr($key, 1) ] = $value;

    return $array;
    }

    Expected result:
    ----------------
    The array should be sorted by column and SORT flag.

    Actual result:
    --------------
    The array_multisort only excepts the SORT flag without quotes and not
    as a string.


    ------------------------------------------------------------------------


    --
    Edit this bug report at [url]http://bugs.php.net/?id=25879&edit=1[/url]
    georg@php.net Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139