#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    448
    Rep Power
    12

    Cannot use [] for reading, array problem


    Cannot use [] for reading

    This is the error i get. Basically im populating an array from values in a mysql database then i want to sort the array based on one of these values. here's the array format

    myarray [$i] = array($value1,$value2);

    it goes through a while loop populating it (from inside a database query) incrementing $i each time.

    value1 is a number id of the database record and value2 is a string name value.

    here is the code i'm attempting to use to sort the array. ( want to sort this array alphabetically by value2 the name. I don't think i'm doing it right, but i'm not sure. the error "cannot use [] for reading" occurs on the line "$arr = $myarray[];"

    Code:
    $arr = $myarray[]; 
    foreach ($arr as $k => $v) 
    { 
        $name[$k] = $v[1]; 
    } 
    array_multisort($name, SORT_ASC, $arr);
    print_r($arr);
    any help would be greatly appreciated. thanks!
    -Sotonin
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Location
    USA
    Posts
    477
    Rep Power
    55
    '[]' is basically an operator, and the error is self explanatory. You cannot use that operator to read a value. You must specify an array index. Like $array[1], $array['example']...
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    448
    Rep Power
    12
    that doesn't really help my problem. (

    So how would i take this array i have and sort it by the second value in the array?

    would i have to do yet another loop using that same code? or is my sorting code completely wrong?
    -Sotonin
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    448
    Rep Power
    12
    OK. i figured out one of my problems, my array wasn't being built properly. Ok here goes. this is my format of the array.

    Code:
    Array ( 
    [0] => Array ( 
    	[value_id] => 1 
    	[value_name] => blablabla
    	[value_image] => http://www.blahblah.com
    	[value_forum] => http://www.blahblah.com
    	[value_gallery] => http://www.blahblah.com
    	) 
    [1] => Array ( 
    	[value_id] => 2 
    	[value_name] => blablabla2
    	[value_image] => http://www.blahblah2.com
    	[value_forum] => http://www.blahblah2.com
    	[value_gallery] => http://www.blahblah2.com
    	) 
    )
    This array is built from a mysql query. Now the biggest question here is how can I "access" the array to call upon needed data. I won't know what exact number it is, I will just know that i need to get the value_name of the item in the array with the value_id of 2 or something to that effect.

    lets say the name of the array is $myarray

    normally you'd have to call a variable like this
    $myarray[1] [value_name] right? but i'm not going to know which position in the array it is currently at.

    sigh, i'm confusing myself, im probably confusing you as well!
    -Sotonin
  8. #5
  9. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,932
    Rep Power
    4033
    You'll have to search the array to find the position. Best way would be to make a function to either get the position of the array, or extract your values.

    Example:
    PHP Code:
    //Find out which position in the array contains the id
    function getIDPosition(&$arr$id){
         
    $count=count($arr);
         for (
    $i=0$i<$count$i++){
             if (
    $id == $arr[$i]['value_id']){
                return 
    $i;
             }
         }

    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    448
    Rep Power
    12
    thanks, that does the trick!.

    Originally Posted by kicken
    You'll have to search the array to find the position. Best way would be to make a function to either get the position of the array, or extract your values.

    Example:
    PHP Code:
    //Find out which position in the array contains the id
    function getIDPosition(&$arr$id){
         
    $count=count($arr);
         for (
    $i=0$i<$count$i++){
             if (
    $id == $arr[$i]['value_id']){
                return 
    $i;
             }
         }

    -Sotonin

IMN logo majestic logo threadwatch logo seochat tools logo