Solved

Replace item in array (php)

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
This article discusses four methods for overlaying images in a container on a web page
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.

757 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

21 Experts available now in Live!

Get 1:1 Help Now