Solved

PHP Array

Posted on 2011-09-23
3
244 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 109

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 109

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

807 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