Solved

PHP Array

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

773 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