Got some code that loops through an existing array, builds a new array as it goes and strips out some sort-related data. when the loop is finished, I want to sort the resultant array on the sort-related data that resides in $dibble[‘sort’] … I’m struggling to understand how to do it. Here’s the loop that build and strips:
foreach($initial as $item)
{
$dibble['alink'][]=$item->link;
$dibble['aname'][]=$item->name;
$dibble['aid'][]=$item->ID;
# strip out block text ... if there is any
$c_start=strpos($item->text,"[menusort]")+10;
if ($c_start > 10)
{
$c_end = strpos($item->text,"[/menusort]");
$c_len=$c_end - $c_start;
$dibble['sort'][]=substr($item->text, $c_start,$c_len);
}
else
{
$dibble['sort'][]='99999';
}
}
You can create a new array item with associative keys on that array item storing your information. To do that, you could move the assignments to the end of the loop, so that you can place the sort key in another variable, and then add the array item at the end of things.
foreach($initial as $item) {
# strip out block text ... if there is any
$sort = '99999';
$c_start=strpos($item->text, "[menusort]") + 10;
if ($c_start > 10) {
$c_end = strpos($item->text, "[/menusort]");
$c_len = $c_end - $c_start;
$sort = substr($item->text, $c_start, $c_len);
}
$dibble[] = array(
'alink' => $item->link,
'aname' => $item->name,
'aid' => $item->ID,
'sort' => $sort
);
}
I’m not sure I can say it any differently than paul wilkins code above … each occurrence of $dibble holds an array containing four data items … I simply want to get a ‘foreach’ to loop over each item and get access to any/all of the data items.