Solved

How can I update a single object in an array?

Posted on 2012-03-20
3
190 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 108

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
This article discusses how to create an extensible mechanism for linked drop downs.
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 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…

867 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