Solved

PHP Array

Posted on 2011-09-23
3
245 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
[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 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 110

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 110

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why isn't my DIV and Form centering? 1 27
Code not executing correctly. 3 43
How do I add date to text file name 15 27
Last 4 numbers of a variable 9 19
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

737 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