DR4296 Posted February 28, 2014 Share Posted February 28, 2014 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 =- Quote Link to comment Share on other sites More sharing options...
ginerjm Posted February 28, 2014 Share Posted February 28, 2014 You know - the PHP manual is just great for finding out how to do things. A quick check in there found this: foreach ($r as &$result).... to update the value clause of a foreach statement. Quote Link to comment Share on other sites More sharing options...
DR4296 Posted February 28, 2014 Author Share Posted February 28, 2014 So, in other words, by shoving an "&" in front of the $result, that tells it that if I update $result, $r will also get updated? I was thinking I probably would have to throw everything into a brand new array. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted February 28, 2014 Share Posted February 28, 2014 If you read the manual you'll get a complete understanding of how an & works in front of a variable name in other circumstances as well. Quote Link to comment Share on other sites More sharing options...
gristoi Posted February 28, 2014 Share Posted February 28, 2014 have a google on passing by value and passing by reference Quote Link to comment Share on other sites More sharing options...
Zane Posted February 28, 2014 Share Posted February 28, 2014 (edited) 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 February 28, 2014 by Zane Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.