Solved

Replace item in array (php)

Posted on 2009-07-10
2
273 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 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 110

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

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

617 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