Jump to content

Array - How To Change Values


tarun

Recommended Posts

I Had Help With This Yesturday And Now I Need Some More

This Is My Example Code

$friendList = array();
$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 Change The Values (offline, online etc.)?

 

Thnx,

Tarun

Link to comment
https://forums.phpfreaks.com/topic/37478-array-how-to-change-values/
Share on other sites

And I'm back again to help you. w00t!

 

It's pretty simple actually, just:

 

$friendList["name_here"] = "status_here";

 

Dynamic:

$friendList[$name_to_remove] = $some_status_var;

 

Since you're doing all this array stuff, I suggest you take a look at the PHP Array Docs.

Like this:

<?php


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


$friend = 'Lisa';

if(array_key_exists($friend,$friendList)){
     $friendList[$friend] = 'Online';
}
foreach( $friendList as $key => $value){
echo "Friend: $key, Status: $value <br>";
}
?>

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.