Hi
Can someone tell me if there is a function that will help me with the following scenario?

$breakfast[1]="sausages";
$breakfast[2]="eggs";
$breakfast[3]="bacon";
$breakfast[4]="heart_failure";

I need to remove an element, say $breakfast[2] and have the rest of the array 'keys' move down one like this;

$breakfast[1]="sausages";
$breakfast[2]="bacon";
$breakfast[3]="heart_failure";

I can probably build something to do it but although I can't find a pre-built function I'm sure there is one!

Cheers

Nick

    Why are they keys important? They shouldn't be.

    unset($breakfast[2]); will work once you recode to remove the need for your keys.

      Hello Tom,
      The keys are important because they are keeping track of 'pages' in an article that will be committed to database once finalized.
      The index will id the $headline,$subhead and $text arrays. I am trying to build a function to delete a page and then re-index the arrays.

      like delete_page($page_num)

      thanks for the help, if you still think my need for indexes is not right please tell me why and I shall start re-coding (again) 🙂

      Nick

        function delete_page($num) {
        global $breakfast;
        unset($breakfast[$num]);
        // concatenate
        $temp=array();
        $c=1; // Starting index for breakfasts
        foreach ($breakfast as $food) {
        $temp[$c++]=$food;
        }
        $breakfast=$temp;
        }

        That function removes the breakfast, then re-aligns all the indeces so they're contiguous, starting from '$c'.

        I hope that's what u need!

          Write a Reply...