Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,098
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Best way to refer to Array Values?

    I have a question about the best way to refer to Keys and Values in an Array.


    In my code, I build an array like this...
    PHP Code:
        // ************************
        // Build Decision Array.    *
        // ************************
        
    foreach($_POST['friendRequestDecision'] as $requestorID => $decision){
            
    // Cast to Integers.
            
    $requestorID = (int)$requestorID;
            
    $decision = (int)$decision;

            
    // Validate Request Decision.
            
    if (($decision == 0) || ($decision == 1) || ($decision == 2)){
                
    // Valid Response.

                // Take Submitted Form Data and put into Decision Array.
                
    $decisionArray[$requestorID] = $decision;

            }else{
                
    // Invalid Response.
                // Display Error.
            


    And then a little farther down in my code I iterate through the array items to take action...
    PHP Code:
        // ******************************
        // Attempt to Update Decisions.    *
        // ******************************
        
    foreach($decisionArray as $requestorID => $decision){


        } 

    In the loop above, which is the proper way to refer to my Array Values...

    Option #1:
    PHP Code:
        if ($decisionArray[$requestorID] == 0){ 

    OR...


    Option #2:
    PHP Code:
        if ($decision == 0){ 
    Sincerely,



    Debbie

  • #2
    Regular Coder student101's Avatar
    Join Date
    Nov 2007
    Posts
    634
    Thanks
    80
    Thanked 15 Times in 15 Posts
    What errors are u getting?
    Edit:
    option 2 seems right
    Last edited by student101; 02-03-2013 at 06:38 PM.
    Thanks for your support!
    Update MySQL with checkboxes | Tell A Friend | Delete MySQL with checkboxes

    Give thanks & resolve when done :thumbsup:

  • #3
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,098
    Thanks
    27
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by student101 View Post
    What errors are u getting?
    Edit:
    option 2 seems right
    No errors, just thinking that Option #1 is more correct or something?!


    Debbie

  • #4
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,302
    Thanks
    4
    Thanked 204 Times in 201 Posts
    Option 2. You've already extracted the array value so why make PHP reference the array again to extract the value to be compared when you've already got it.
    Dave .... HostMonster for all of your hosting needs

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Option 2. The first one takes more work to extract from the array again.
    The key is only useful during copy operations as you have done in your example. It carries no other value to it since the value can be modified if set by reference, and unsetting during an iteration is incredibly unwise.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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