logo       
Google Custom Search
    AddThis Social Bookmark Button

[ILUG-Webdev] PHP array problem: msg#00001

Subject: [ILUG-Webdev] PHP array problem
Before I have to go re-writing a load of someone elses code maybe someone
has an easy solution to the following problem.

I have an associative array something like the following:

$menu[] = Array('name' => 'Personnel', 'id' => 102);
$menu[] = Array('name' => 'Publications', 'id' => 104);
$menu[] = Array('name' => 'Events', 'id' => 105);

This used to be static. It is now edittable via a browser, and in between
edits the array is stored in a textfile via serialize() and sourced from
there for use in other places. Because of the way serialize() works $menu
comes back out as:

$menu[0] = Array('name' => 'Personnel', 'id' => 102);
$menu[1] = Array('name' => 'Publications', 'id' => 104);
$menu[2] = Array('name' => 'Events', 'id' => 105);

ie. it now has numbered values. So if a user adds a menuitem to the end,
then deletes one preceding it, the array gets saved with missing values:

$menu[0] = Array('name' => 'Personnel', 'id' => 102);
$menu[2] = Array('name' => 'Events', 'id' => 105);
$menu[3] = Array('name' => 'News', 'id' => 110);

This would be OK, except that there is old code that iterates through
$menu using:

$b = count($menu);
for ($i=0; $<$b; $i++) { ... }

instead of using

foreach($menu as $whatever) { ... }

My question is this. Rather than re-writing a load of old code to use
foreach() instead of a for loop, is there any way of "fixing" the array up
before it is serialize()ed - basically re-number the array correctly?

PHP is still not my forte, so please forgive me if I've missed something
obvious.

Regards,
        Lee
-- 
ILUG Web Development
http://mail.linux.ie/mailman/listinfo/webdev/



<Prev in Thread] Current Thread [Next in Thread>