1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Undefined index...

Discussion in 'Web Design & Development' started by JiminSA, Jul 10, 2012.

Thread Status:
Not open for further replies.
Advertisement
  1. JiminSA

    JiminSA Thread Starter

    Joined:
    Dec 15, 2011
    Messages:
    2,058
    I am trying to sort an array using a value in that array, using the following code...
    PHP:
    $vehicles LoadVehicles();
    if ( (array) 
    $vehicles == $vehicles 
    {
        function 
    compare_datetime($a$b)
        {
            return 
    strnatcmp($a['datetime'], $b['datetime']);
        }
        
    usort($vehicles'compare_datetime');
        
    var_dump ($vehicles);
    }
    exit;
    which I gleaned from this tutorial.
    Unfortunately I am getting a notification - viz:-
    Does one need to "define" or "declare" an associative array, before one can use it? If so, how?
     
  2. dukevyner

    dukevyner

    Joined:
    Nov 4, 2011
    Messages:
    787
    "This error appears because of your PHP error reporting settings. Usually, it appears when your variable is not properly set."
    make sure that all variables are set such as "$a" and "$b"
     
  3. JiminSA

    JiminSA Thread Starter

    Joined:
    Dec 15, 2011
    Messages:
    2,058
    I have modified the code thus but still pick up the notice
    PHP:
    $vehicles_in LoadVehicles();

    if ( (array) 
    $vehicles_in == $vehicles_in 
    {
        function 
    compare_datetime($a$b)
        {
            return 
    strnatcmp($a['datetime'], $b['datetime']);
        }
    }
        
    $a $vehicles_in;
        
    $b $vehicles_in;
        
    usort($vehicles_in'compare_datetime');
        
    var_dump ($vehicles_in);
    exit;
     
  4. JiminSA

    JiminSA Thread Starter

    Joined:
    Dec 15, 2011
    Messages:
    2,058
    I don't see that it's necessary, but I initialise the array thus...
    PHP:
    $vehicles_in = array(
        
    $vehicles_in["fame"] = "",
        
    $vehicles_in["phone"] = "",
        
    $vehicles_in["cell"] = "",
        
    $vehicles_in["suburb"] = "",
        
    $vehicles_in["make"] = "",
        
    $vehicles_in["model"] = "",
        
    $vehicles_in["year"] = "",
        
    $vehicles_in["kms"] = "",
        
    $vehicles_in["colour"] = "",
        
    $vehicles_in["extras"] = "",
        
    $vehicles_in["servhist"] = "",
        
    $vehicles_in["accidents"] = "",
        
    $vehicles_in["settlement"] = "",
        
    $vehicles_in["finhouse"] = "",
        
    $vehicles_in["monthsrem"] = "",
        
    $vehicles_in["monthsarr"] = "",
        
    $vehicles_in["amtrem"] = "",
        
    $vehicles_in["price"] = "",
        
    $vehicles_in["comments"] = "",
        
    $vehicles_in["tprice"] = "",
        
    $vehicles_in["rprice"] = "",
        
    $vehicles_in["natis"] = "",
        
    $vehicles_in["pics4"] = "",
        
    $vehicles_in["wcdisc"] = "",
        
    $vehicles_in["aprice"] = "",
        
    $vehicles_in["status"] = "",
        
    $vehicles_in["notes"] = "",
        
    $vehicles_in["datetime"] = ""
    );
     
  5. allnodcoms

    allnodcoms

    Joined:
    Jun 30, 2007
    Messages:
    613
    Hi Ho Jim...

    I think the problem here is that your two sort functions are expecting a single dimensional array, and unless I'm very much mistaken, your LoadVehicles(); function will return a multidimensional one (as your strnatcmp(); call is comparing similar keys and, obviously, because you told me the other day!).

    This ain't gonna work fella. The array_multisort(); function is the way to go on this as it sieves multidimensional and multiple arrays by a single key.

    Do I really need to mention that your array definition looks like a table in a MySQL database and that you could sort your records with a single call, using standard PHP? No... thought not. :p

    Danny
     
  6. JiminSA

    JiminSA Thread Starter

    Joined:
    Dec 15, 2011
    Messages:
    2,058
    Thanks for the solution Danny.

    I shall indeed be looking to come out of hibernation with this project and have already opened one eye inasmuch as I have set up the vehicles db on my server:eek:

    Can you recommend a good tutorial, without too much PHPspeak?;)
     
  7. allnodcoms

    allnodcoms

    Joined:
    Jun 30, 2007
    Messages:
    613
    Give this one a bash mate. If you've got any questions you know the addy ;)

    Have fun fella... It's a doddle once you get going.

    Danny
     
  8. JiminSA

    JiminSA Thread Starter

    Joined:
    Dec 15, 2011
    Messages:
    2,058
    Aargh!:confused:

    No Joy - adjusted the code so...
    PHP:
    $vehicles_in LoadVehicles();

    if ( (array) 
    $vehicles_in == $vehicles_in 
    {

        foreach (
    $vehicles_in as $key => $row) {
            
    $datetime[$key]  = $row['datetime'];
        }

        
    array_multisort($datetimeSORT_DESC$vehicles_in);
        
    $vehicles_out $vehicles_in;
        
    var_dump $vehicles_out );
    }
    exit;

    But still get..
    Line 88 = $datetime[$key] = $row['datetime'];

    This is vexing!:mad:
     
  9. allnodcoms

    allnodcoms

    Joined:
    Jun 30, 2007
    Messages:
    613
    Right, I've taken a good long look at this and there are two possible solutions:
    1. Explicitly define $datetime as an array
    2. There is an issue with your LoadVehicles(); function
    To test theory 1, add:
    PHP:
    $datetime = array();
    before your 'foreach()'. This will ensure that $datetime is defined as an array, but I don't think that's it...

    LoadVehicles() should return a correctly formatted, multidimensional array:
    PHP:
    $vehicle = array(); 
    $vehicle[] = array('key1' => 'value1''key2' => 'value2');
    $vehicle[] = array('key1' => 'value1''key2' => 'value2');
    $vehicle[] = array('key1' => 'value1''key2' => 'value2'); 
    // and so on...
    Give these a go and see if we are getting any nearer. Keep me posted!

    Danny
     
  10. JiminSA

    JiminSA Thread Starter

    Joined:
    Dec 15, 2011
    Messages:
    2,058
    This a var_dump of a portion (1st record) of the array retrieved by Loadvehicles
    PHP:
    array(2) { ["vehicles"]=> array(59) { [1]=> array(30) { ["fame"]=> string(6"Victor" ["email"]=> string(18"[email protected]["phone"]=> string(10"0727504912" ["cell"]=> string(10"0727504912" ["suburb"]=> string(11"Sunninghill" ["make"]=> string(3"BMW" ["model"]=> string(4"325i" ["year"]=> string(4"2008" ["kms"]=> string(6"87 000" ["colour"]=> string(5"white" ["extras"]=> string(73"sunroof, climate control, auto cruise control, PDC, motor plan up to 100k" ["servhist"]=> NULL ["accidents"]=> string(4"none" ["settlement"]=> string(10"Yes - Full" ["finhouse"]=> string(6"toyota" ["monthsrem"]=> string(3"n/a" ["monthsarr"]=> string(3"n/a" ["amtrem"]=> string(3"n/a" ["price"]=> string(7"210 000" ["comments"]=> string(6"change" ["photos"]=> array(0) { } ["tprice"]=> string(4"0.00" ["rprice"]=> string(4"0.00" ["natis"]=> string(3"Yes" ["pics4"]=> string(9"Requested" ["wcdisc"]=> string(3"Yes" ["aprice"]=> string(0"" ["status"]=> string(10"No Contact" ["notes"]=> string(0"" ["datetime"]=> int(1341054720
    ["datetime"] is right at the record's end
    I've adjusted the code for the array define...
    PHP:
    $vehicles_in LoadVehicles();

    $datetime = array();
    $row = array();
    $row["datetime"] = "";
    var_dump $vehicles_in );

    if ( (array) 
    $vehicles_in == $vehicles_in 
    {

        foreach (
    $vehicles_in as $key => $row) {
            
    $datetime[$key]  = $row['datetime'];
        }

        
    array_multisort($datetimeSORT_ASC$vehicles_in);

        
    $vehicles_out $vehicles_in;
        
    var_dump $vehicles_out );
    }
    exit;

    still get the notification! for this line ... $datetime[$key] = $row['datetime'];
     
  11. JiminSA

    JiminSA Thread Starter

    Joined:
    Dec 15, 2011
    Messages:
    2,058
    $vehicles is multi-dimensional - I forgot to define it correctly - $vehicles_in["vehicles"]

    Now we are getting closer to the actual sort and have hit this problem...
    where line 97 is
    PHP:
        array_multisort($datetimeSORT_ASC$vehicles_in); 
    the current code is...
    PHP:
    $vehicles_in LoadVehicles();

    $vehicle $vehicles_in["vehicles"][1];
    echo 
    "Date-Time " $vehicle["datetime"];

    if ( (array) 
    $vehicles_in == $vehicles_in 
    {

        foreach (
    $vehicles_in["vehicles"] as $key => $row) { 
            
    $datetime[$key] = $row['datetime']; 
        } 

        
    array_multisort($datetimeSORT_ASC$vehicles_in); 

        
    $vehicles_out $vehicles_in; }
    /*    var_dump ( $vehicles_out ); }*/
    exit;
     
  12. allnodcoms

    allnodcoms

    Joined:
    Jun 30, 2007
    Messages:
    613
    Inconsistent array sizes is down to your loading / saving code mate. If there is an empty field you'll need to write out a string, a blank or empty string may not make it through your serialisation and this would lead to your problem. It's the number of fields in each sub-array and not the length of them that is the issue, so I'm guessing it's down to the read / write on your csv file.

    That's where I'd start looking anyway...

    Danny
     
  13. JiminSA

    JiminSA Thread Starter

    Joined:
    Dec 15, 2011
    Messages:
    2,058
    Ta Danny - I'll look tomz... ni-night...
     
  14. JiminSA

    JiminSA Thread Starter

    Joined:
    Dec 15, 2011
    Messages:
    2,058
    Danny, would I be right in assuming that these difficulties would be alleviated if I went MySql and read the array in from there?
     
  15. allnodcoms

    allnodcoms

    Joined:
    Jun 30, 2007
    Messages:
    613
    By Jove I think he's got it! :D

    Yes mate - "SELECT * FROM 'vehicles' ORDER BY 'datetime'" - that's it!

    Good choice Jamesy, any problems - you know how to find me.

    Danny
     
  16. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1060359