Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-03-2013, 05:01 PM   PM User | #1
doubledee
Regular Coder

 
doubledee's Avatar
 
Join Date: Mar 2011
Location: Arizona
Posts: 640
Thanks: 20
Thanked 0 Times in 0 Posts
doubledee has a little shameless behaviour in the past
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
doubledee is offline   Reply With Quote
Old 02-03-2013, 06:28 PM   PM User | #2
student101
Regular Coder

 
student101's Avatar
 
Join Date: Nov 2007
Posts: 610
Thanks: 80
Thanked 13 Times in 13 Posts
student101 is on a distinguished road
What errors are u getting?
Edit:
option 2 seems right
__________________
Thanks for your support!
Update MySQL with checkboxes | Tell A Friend | Delete MySQL with checkboxes

Give thanks & resolve when done :thumbsup:

Last edited by student101; 02-03-2013 at 06:38 PM..
student101 is offline   Reply With Quote
Old 02-03-2013, 09:56 PM   PM User | #3
doubledee
Regular Coder

 
doubledee's Avatar
 
Join Date: Mar 2011
Location: Arizona
Posts: 640
Thanks: 20
Thanked 0 Times in 0 Posts
doubledee has a little shameless behaviour in the past
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
doubledee is offline   Reply With Quote
Old 02-03-2013, 10:30 PM   PM User | #4
djm0219
Senior Coder

 
djm0219's Avatar
 
Join Date: Aug 2003
Location: Wake Forest, North Carolina
Posts: 1,229
Thanks: 2
Thanked 190 Times in 188 Posts
djm0219 is on a distinguished road
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
djm0219 is offline   Reply With Quote
Old 02-03-2013, 11:56 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,748
Thanks: 4
Thanked 2,467 Times in 2,436 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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'); 
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:52 PM.


Advertisement
Log in to turn off these ads.