Jump to content

Array - Adding and Removing


tarun

Recommended Posts

Okay Heres My Code

$friendList;
$friendList["Lisa"] = "offline";
$friendList["Jack"] = "online";
$friendList["Ryan"] = "offline";
$friendList["Rachel"] = "offline";
$friendList["Grace"] = "online";

foreach( $friendList as $key => $value){
echo "Friend: $key, Status: $value";
}

 

How Would I Add And Remove Friends?

 

Thnx,

Tarun

Link to comment
https://forums.phpfreaks.com/topic/37341-array-adding-and-removing/
Share on other sites

To remove:

 

unset($friendList["namegoeshere"]);

 

To add:

$friendlist["namegoeshere"] = "status";

 

To remove dynamically:

if(array_key_exists($name_to_remove, $friendList))

{

unset($friendList[$name_to_remove]);

}

 

To add dynamically:

$friendList[$name_to_remove] = "status";

 

You might also want to change $friendList; to $friendList = array();

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.