Solved

How can I update a single object in an array?

Posted on 2012-03-20
3
192 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
  • 2
3 Comments
 
LVL 109

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 109

Accepted Solution

by:
Ray Paseur earned 500 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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

773 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