[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How can I update a single object in an array?

Posted on 2012-03-20
3
Medium Priority
?
200 Views
Last Modified: 2012-03-20
How can I update a single user, like adding firstname, lastname, etc?

foreach ($users['User'] as $my_object)
{
    $email = (string)$my_object->email;
    $my_associative_array[$email] = $my_object;
	
}

Open in new window

0
Comment
Question by:glynco
[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
3 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37742562
Please post the class definition used when the script instantiated $my_object.  I will show you how to add properties and setters.
0
 

Author Comment

by:glynco
ID: 37742692
<?php

class User {
	public $email;
	public $firstname;
	public $lastname;
	
	public function __construct($email, $firstname, $lastname)  
    {  
        $this->email = $email;
	$this->firstname = $firstname;
	$this->lastname = $lastname;
    } 


	public function setEmail($value)
	{
		$this->email=$value;
	}

	public function getEmail($value)
	{
		return $this->email;
	}

	public function setFirstname($value)
	{
		$this->firstname=$value;
	}

	public function getFirstname($value)
	{
		return $this->firstname;
	}

	public function setLastname($value)
	{
		this->lastname=$value;
	}

	public function getLastname($value)
	{
		return $this->lastname;
	}

}
?>

Open in new window

0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 37744316
That cannot possibly be the class definition.  It contains a parse error on line 38.

But that aside, here is the way you do it.  Take note of a few code changes.  See
http://www.laprbass.com/RAY_temp_glynco.php
<?php // RAY_temp_glynco.php
error_reporting(E_ALL);
echo "<pre>";

// CLASS DEFINITION
class User
{
    public $email;
    public $firstname;
    public $lastname;

    public function __construct($email, $firstname, $lastname)
    {
        $this->email = $email;
        $this->firstname = $firstname;
        $this->lastname = $lastname;
    }

    public function setEmail($value)
    {
        $this->email=$value;
    }

    public function getEmail()
    {
        return $this->email;
    }

    public function setFirstname($value)
    {
        $this->firstname=$value;
    }

    public function getFirstname()
    {
        return $this->firstname;
    }

    public function setLastname($value)
    {
        $this->lastname=$value;
    }

    public function getLastname()
    {
        return $this->lastname;
    }
}


// MAKE AN ARRAY OF OBJECTS
$arr[] = new User('Ray.Paseur@Gmail.com', 'Ray', 'Paseur');
$arr[] = new User('NoReply@Gmail.com', 'No', 'Reply');

// SHOW THE ARRAY
var_dump($arr);

// UPDATE ONE OF THE OBJECTS
foreach ($arr as $key => $obj)
{
    // FIND THE OBJECT WE WANT TO UPDATE
    if ( (string)$obj->getLastName() == 'Reply' )
    {
        // MODIFY THE OBJECT
        $obj->setFirstName('Bill');
        $obj->setLastName('Gates');

        // REPLACE THIS ELEMENT OF THE ARRAY
        $arr[$key] = $obj;
    }
}

var_dump($arr);

Open in new window

0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

656 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