Solved

PHP Array

Posted on 2011-09-23
3
241 Views
Last Modified: 2012-05-12
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
Comment
Question by:Ocrana
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
nrbreen earned 500 total points
ID: 36591485
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36592556
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36596233
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

Highfive Gives IT Their Time Back

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

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

708 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

17 Experts available now in Live!

Get 1:1 Help Now