[FX.php List] recids and each() vs. foreach() coding style

DC dan.cynosure at dbmscan.com
Wed Sep 6 13:51:17 MDT 2006


Hi Mark,

I'd suggest that better coding style would be to use the each()  
function rather than the confusing foreach() with a semicolon.

each() returns the key and value of the current array element which  
in the case of a new array is the first element.

using each() it is clear that you only want one record and by default  
it is the first record.

if you want to be really strict then consider the reset() function  
which will force the internal array pointer to the first array  
element before returning it. this is useful in case you aren't sure  
if one of your previous array manipulations left the internal array  
point "awry".

by using foreach() with semicolon it suggests that you might be  
iterating over the array. and in fact you *are* iterating the array.

because of that, foreach() with semicolon style returns the *last*  
element of the array rather than the first element.

in fact, before the recent post i considered foreach() with a  
semicolon to be a syntax error since i'd never seen foreach() used to  
accomplish what each() ( and reset() ) do very clearly.

here is some code that will make it easy to see what i mean:
-----------------------------------------------------------
<?php

// mock up array like FX.php data
$searchResult['data'] = array(
'1.1' => array('fieldname' => array('data1')),
'2.2' => array('fieldname' => array('data2')),
'3.3' => array('fieldname' => array('data3')) );

// this is clear coding style
// get the first array item
list($key,$value) = each($searchResult['data']);
list($recid,$mod)=explode('.',$key);

// shows that each gets first array item
echo '<pre>';
print_r($recid);
print_r($mod);
echo '</pre>';


//--------------------------------
echo '<hr>';

// this is unusual coding style
// use semicolon foreach()
foreach($searchResult['data'] as $key => $value);
list($recid,$mod)=explode('.',$key);

// shows that foreach gets last array item
echo '<pre>';
print_r($recid);
print_r($mod);
echo '</pre>';

//--------------------------------
echo '<hr>';

// here's a weird thing I found at php.net
// comments on the foreach() function
// and semicolon syntax
// you can use it like array_keys()
// and array_values() at the same time!
// use unusual semicolon style foreach(),
// but this time use array notation to get
// keys into one array and values into another
foreach($searchResult['data'] as $keys[] => $values[]);

// shows that foreach gets all array items
// into two arrays in one line!!
// did i mention that i love oneliners...
echo '<pre>';
print_r($keys);
print_r($values);
echo '</pre>';

?>

---------------------------------------------------------

my 2,
dan

On Sep 6, 2006, at 1:24 PM, Lindal, Mark wrote:

> Jonathan et al,
>
> A correction to your finding recID from the data returned in FMNew()
>
>  foreach($searchResult['data'] as $key => $value);
>  $recordDetails=explode('.',$key);
>  $currentRecord=$recordDetails[0];
>
> Take  the { } out and end your foreach with a ';'
>
> Otherwise you are telling it to loop through all the data.
> In this case only one record is found - so it will only loop once  
> and there
> are cases when you want the recid for a series of record.
>
> But in the case of only one record - this is better coding.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.iviking.org/pipermail/fx.php_list/attachments/20060906/f733208b/attachment.html


More information about the FX.php_List mailing list