Forum Moderators: coopster

Message Too Old, No Replies

removing array elements

         

sssweb

2:48 pm on Jul 28, 2006 (gmt 0)

10+ Year Member



How do I remove individual array elements in a 'for' loop? Example:

$array = array(1, 2, 3, 4, 5);
for($i = 0; $i < count($array); $i++)
{
if ( $array[$i] == 3 )
{
** I want to delete this array element **
}
}

The resulting array should = (1, 2, 4, 5)
Note I want to remove the element completely, not just set it to no value, as in (1, 2, '', 4, 5).

coopster

3:01 pm on Jul 28, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



unset() it.
$array = array(1, 2, 3, 4, 5); 
print '<pre>'; print_r($array); print '</pre>';
for($i = 0; $i < count($array); $i++) {
if ($array[$i] == 3 ) {
unset [php.net]($array[$i]);
}
}
print '<pre>'; print_r($array); print '</pre>';
exit;

sssweb

5:08 pm on Jul 28, 2006 (gmt 0)

10+ Year Member



Thnx, Coopster -- works like a charm.

morales2k

5:18 pm on Aug 17, 2006 (gmt 0)

10+ Year Member



I have a similar situation. But somehow this example above won't do it.

I am making a small form that signs users of my site up for a current "event". Then if he is already signed up he should be shown a remove button to be able to remove himself if he no longer wishes to be signed up for that event.

The signups work nicely, but I am unaware of how the coding goes for me to CHECK if certain user ID is in my array of dot separated values.

My database for the events has a column called event_signup and the values for each event are simply the user id's separated by dots. (1.15.20.3)

I am using PHP-Fusion CMS so the database functions may seem strange...
---------------------------------------------------------------------
//this will find me my current signups.
$result3 = dbquery("SELECT event_signup FROM events WHERE
event_id='".$_POST['event_id']."'");
$data3 = dbarray($result3);
// Check to see if its the first
if ( $data3['event_signup'] == "" ){
// We dont need the . as seperators cause theres only 1 id
$signup2 = $_POST['event_signup'];
}else{
// user_ids, is now an array like user_ids[0]=55 ; user_ids[1]=21.
$user_ids = explode(".",$data3['event_signup']);
// Tells how many user ids are signed up
$num_ids = count($user_ids);
//add the new id to the end of the current ones
$user_ids[$num_ids+1] = $_POST['event_signup'];
// Put all our ids back to gether
$signup2 = implode(".", $user_ids);
}
//UPDATE the signups in my database.
$result3 = dbquery("UPDATE events SET event_signup='$signup2'
WHERE event_id='".$_POST['event_id']."'");
-----------------------------------------------------------------

Now... HOW do I get that array, separate everything from it, CHECK if the current user is there, (the user clicking on remove button or something) and unset() ONLY that user from the whole array. Then implode and UPDATE database once more with all other users intact.

I had help with the current coding above, so I am no expert I know. But I am learning this stuff fast and I must say that I like it hehe for a hobby its awesome. Eventually could be that this becomes some par time job for me hehe.

Ah, thanks for any help you guys can provide on this.

supermoi

3:33 am on Aug 18, 2006 (gmt 0)

10+ Year Member



To check if a user is in your array, you can use in_array() [php.net]
To get the key under which the user's id is, use array_search() [php.net]
You can then unset the element using the key returned by array_search().