Jump to content

Question about doing foreach on an array


DR4296

Recommended Posts

Greetings All !

 

So, based on this....

foreach ($r as $result){
    	// format results
    	
    	if ((array_key_exists('sr_number', $result))  && ($result['sr_number'] == "")){
			$result['sr_number'] = " ";
	}
	if ((array_key_exists('start_dt_tm', $result)) && ($result['start_dt_tm'] !="")){
		$result['start_dt_tm'] = date("m-d-y h:i A", strtotime($result['start_dt_tm']));
	}
		

}

... it occurs to me that I'm updating the value of $result, but I'm not really updating $r.   I want to update it within $r, so that I can use all of the contents of $r.

 

I'm basically trying to write a function that will prep some variables, if they exist, in an HTML table.   I've got two PHP pages/ scripts where I'm essentially doing the same thing, but the variables / table columns differ slightly, so I thought I'd take the repetitive code from the two pages and try to combine them into a single function.

 

So $r is going to be an array of results destined to be displayed as table rows, where each $r will essentially wind up as a single row.

 

Thanks!

 

-= Dave =-

 

Link to comment
Share on other sites

No need to pass by reference, because mainly, it will not solve your problem.

Leave the ampersand (&) out of there and check this out.

 

You're using the following foreach syntax

foreach ( $r as $result )

Judging by what follows, $r is a multidimensional array, right,... meaning $result will be a sub array of $r..... right.

You want to preserve the formatted data, from what I gather.  Therefore, if you were to pass anything at all by reference, it would be the main array $r.

 

But,... you are not within a function, so the scope of your variable has not changed.  Therefore, you can edit $r within the foreach WITHOUT an ampersand (&), without any kind of referencing whatsoever!

 

In order to preserve your data, you need to know exactly which item in the $r array to edit, ...and the easiest way to locate data in an array is with the key.

Fortunately, foreach has slightly different syntax that allows you to retrieve the key as well.

 

So now, you should understand why this way is much much better.

foreach ($r as $key=>$result){
    // format results
    
    if ((array_key_exists('sr_number', $result)) && ($result['sr_number'] == "")){
            $r[$key]['sr_number'] = " ";
    }
    if ((array_key_exists('start_dt_tm', $result)) && ($result['start_dt_tm'] !="")){
        $r[$key]['start_dt_tm'] = date("m-d-y h:i A", strtotime($result['start_dt_tm']));
    }
}

Without knowing exactly what you array looks like, I can't guarantee that the above code will work.  I merely wanted to point out that the key of the array IS accessible.
Even if you were not using a foreach loop, rather a while loop or something else, you could use PHP's key function and store it into a variable.

Edited by Zane
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.