Solved

How can I update a single object in an array?

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
change name file before record in server 5 25
How to validate 2 forms at once 8 27
Phone Dialer 5 36
using hash in login 34 13
Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

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