sneakyimp;10934722 wrote:For your example array, you could do this:
for ($item in $foo) {
echo $item->Line .'<br>';
}
Thanks for your reply. I'm not sure I explained it very well. I actually need session variables, and of course, the problem comes when i have two of the same name.
I've come up with the following which does work, although I'm not sure its the most efficient, or cleanest way to go:
$foo = array (
(object) array("Label" => "Street", "Line" => "Stanley Road"),
(object) array("Label" => "Town", "Line" => "Manchester"),
(object) array("Label" => "Number of Rules", "Line" => "2"),
(object) array("Label" => "Rule", "Line" => "X014"),
(object) array("Label" => "Rule Text", "Line" => "Rule text for X014"),
(object) array("Label" => "Rule", "Line" => "X015"),
(object) array("Label" => "Rule Text", "Line" => "Rule text for X015")
);
foreach($foo as $bar){
if ($bar->Label != 'Rule' AND $bar->Label != 'Rule Text') {
$_SESSION[$bar->Label] = $bar->Line;
} else {
switch ($bar->Label){
case 'Rule':
$suffix ++;
$rules[$bar->Label.$suffix] = $bar->Line;
break;
case 'Rule Text':
$rules[$bar->Label.$suffix] = $bar->Line;
break;
}
}
}
foreach ($rules as $key => $value){ $_SESSION[$key] = $value;}
which gives the following:
var_dump ($_SESSION);
array(7) { ["Street"]=> string(12) "Stanley Road"["Town"]=> string(10) "Manchester" ["Number of Rules"]=> string(1) "2" ["Rule1"]=> string(4) "X014" ["Rule Text1"]=> string(18) "Rule text for X014"["Rule2"]=> string(4) "X015" ["Rule Text2"]=> string(18) "Rule text for X015" }
which is pretty much what I was after, but open to any cleaner suggestions