Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Replace item in array (php)

Posted on 2009-07-10
2
Medium Priority
?
276 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 35

Accepted Solution

by:
gr8gonzo earned 2000 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 111

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

730 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