Solved

Replace item in array (php)

Posted on 2009-07-10
2
243 Views
Last Modified: 2012-06-27
Let's say I have an array like this:

array(
name=>'sam',
age=>'22',
weight=>'222'
)

How can I update the name element in the array? to set it to 'John' for example....
0
Comment
Question by:interclubs
2 Comments
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 24827891
<?
// Original data
$myArray = array(
  "name" => "sam",
  "age" => "22",
  "weight" => "222"
);

// Change just the name to "John"
$myArray["name"] = "John";
?>
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24828509
The online manual might be helpful for these sorts of questions.  For example, if you wanted to know if "sam" was in the array, you could use in_array()
http://us.php.net/manual/en/function.in-array.php

if (in_array('sam', $my_array)) echo "We found Sam!";

The complete list of array functions is here:
http://us.php.net/manual/en/ref.array.php

HTH, ~Ray
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now