Solved

How can I update a single object in an array?

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

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 110

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
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…
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 count occurrences of each item in an array.

688 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