note 49004 added to control-structures.foreach
| From: | richard dot quadling at bandvulc dot co dot uk | Date: | Fri, 14 Jan 2005 10:37:31 +0000 |
| Subject: | note 49004 added to control-structures.foreach | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-83258@lists.php.net to get a copy of this message | ||
Using heavily nested arrays and needing to go through them and change values in the main array can
be a bit daunting. Sometimes. Maybe you have code like this ...
<?php
foreach($aAVWP['Accounts'] as $sAccount => $aAccountData)
{
if (isset($aAccountData['Selected']) && ($aAccountData['Selected'] ===
True))
{
foreach($aAccountData['Vehicles'] as $sVehicle => $aVehicleData)
{
foreach($aVehicleData['WheelPositions'] as $sWheelPosition => $aWheelPositionData)
{
if ($aWheelPositionData['LastVisited'] == time())
{
$aAVWP['Accounts'] [$sAccount] ['Vehicles'] [$sVehicles]
['WheelPositions'] [$sWheelPosition] ['Updated'] = True;
}
}
}
}
}
?>
The foreach is not too bad, but the assignment is a LOT to get right.
<?php
foreach($aAVWP['Accounts'] as $sAccount => &$aAccountData)
{
if (isset($aAccountData['Selected']) && ($aAccountData['Selected'] ===
True))
{
foreach($aAccountData['Vehicles'] as $sVehicle => &$aVehicleData)
{
foreach($aVehicleData['WheelPositions'] as $sWheelPosition =>
&$aWheelPositionData)
{
if ($aWheelPositionData['LastVisited'] == time())
{
$aWheelPositionData['Updated'] = True;
}
}
}
}
}
?>
Does the same thing.
A smaller proof ...
<?php
$aArray = array
(
'a' => array
(
'A' => 1,
'B' => 2,
),
'b' => array
(
'A' => 1,
'B' => 2,
),
);
foreach($aArray as $sKey => $aNextLevel)
{
$aNextLevel['A'] = $sKey;
}
echo '<pre>Unchanged as a copy is made : ' . var_export($aArray, True) .
'</pre>';
foreach($aArray as $sKey => &$aNextLevel)
{
$aNextLevel['A'] = $sKey;
}
echo '<pre>Changed as a reference is used : ' . var_export($aArray, True) .
'</pre>';
?>
produces output of ...
Unchanged as a copy is made : array (
'a' =>
array (
'A' => 1,
'B' => 2,
),
'b' =>
array (
'A' => 1,
'B' => 2,
),
)
Changed as a reference is used : array (
'a' =>
array (
'A' => 'a',
'B' => 2,
),
'b' =>
array (
'A' => 'b',
'B' => 2,
),
)
Hopefully useful to you.
Richard Quadling.
----
Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+49004
Delete -- http://master.php.net/manage/user-notes.php?action=delete+49004&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49004&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+49004&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+49004&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+49004&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+49004&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+49004&report=yes
Search -- http://master.php.net/manage/user-notes.php