[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

PHP Array

Hello,

a question about PHP. I have a function:

    public function addField($field, $value)

    {

        $this->fields["$field"] = $value;

    }

Open in new window


How I can read out the fields. Like I have one field with the name "name". how I can put the value into a new variable $newName?

Ocrana
0
Ocrana
Asked:
Ocrana
  • 2
1 Solution
 
nrbreenCommented:
do you mean you want to set one specific variable $newName from the one array value fields["name"]

like so:

$newName=$this->fields["name"];

Open in new window


or do you want to place *all* the array values into separate variables,

foreach ($this->fields as $key=>$value)
   ${"new$key"}=$value;

Open in new window




0
 
Ray PaseurCommented:
Object oriented programing in PHP is documented (like most everything else about PHP) in the PHP.net web site.
http://us2.php.net/manual/en/language.oop5.php

But that said, I get the sense that you have some programming experience, but you are new to PHP.   If that is the case, drop everything and buy this book right now.  It will teach the syntax and concepts you need to use PHP effectively.  Very readable with great explanations and examples.  Now in its fourth printing, it has been a part of my professional library since edition one.
http://www.sitepoint.com/books/phpmysql4/

See the action of this script here on my server
http://www.laprbass.com/RAY_temp_ocrana.php
<?php // RAY_temp_ocrana.php
error_reporting(E_ALL);
echo "<pre>";

// CREATE A CLASS
Class Thing
{
    protected $fields;

    public function __construct()
    {
        $this->fields = array();
    }

    public function addField($field, $value)
    {
        $this->fields[$field] = $value;
    }

    public function getField($field)
    {
        return $this->fields[$field];
    }
}

// CREATE AN OBJECT -- AN INSTANCE OF THE CLASS
$x = new Thing;

// ADD FIELDS TO THE OBJECT
$x->addField('Dave', 'Baldwin');
$x->addField('Ray',  'Paseur');

// SHOW THE OBJECT
var_dump($x);

// GET ONE OF THE FIELDS
$y = $x->getfield('Ray');
var_dump($y);

Open in new window

0
 
Ray PaseurCommented:
Ocrana: The behavior of the solution you accepted is dependent on information that you did not include in this question.  It might work or it might fail.  That is why after reading that comment I recommended that you read the online man page about OOP5.  In case you want to find out when the solution will work and when it will fail (not as a parse error, but as a run-time catastrophe) you might want to post a new question.  Suggest you do that before you rely on the answer.

And get that SitePoint book -- it will really be valuable to you.

Best regards, ~Ray
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now