473,379 Members | 1,278 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

extract() to object variables

bilibytes
128 100+
Hi everyone,

I am wondering if it is possible to extract the key values of an array into object variables.

lets say i have the following $_REQUEST array:

Expand|Select|Wrap|Line Numbers
  1. $var_array = array("color" => "blue",
  2.                    "size"  => "medium",
  3.                    "shape" => "sphere");
  4. extract($var_array, EXTR_IF_EXISTS);
  5.  
and the the following object:

Expand|Select|Wrap|Line Numbers
  1. Class Listener
  2. {
  3.     public $color;
  4.         public $size;
  5.  
  6.     public function __construct(){
  7.         if(isset($_REQUEST)){
  8.  
  9.             extract($_REQUEST,  EXTR_IF_EXISTS);
  10.             return true;
  11.         }
  12.         else{
  13.  
  14.             return false;
  15.  
  16.         }
  17.     }
  18.  
  19. }

how could i use extract, to store the Key=>values into the object variables:

Expand|Select|Wrap|Line Numbers
  1. echo $this->color //blue
  2. echo $this->size //medium

thankyou
Nov 6 '08 #1
3 6443
Atli
5,058 Expert 4TB
Hi.

You can simply loop through the array and add each array element as a property.

For example:
Expand|Select|Wrap|Line Numbers
  1. class foo
  2. {
  3.     public function __construct($inArray)
  4.     {
  5.         foreach($inArray as $_key => $_value)
  6.         {
  7.             $this->$_key = $_value;
  8.         }
  9.     }
  10. }
  11. $arr = array('color' => 'blue', 'size' => 'medium');
  12. $inst = new foo($arr);
  13. print_r($inst);
  14.  
Which should give you:
Expand|Select|Wrap|Line Numbers
  1. foo Object
  2. (
  3.     [color] => blue
  4.     [size] => medium
  5. )
  6.  
Nov 6 '08 #2
bilibytes
128 100+
Thankyou,

the problem is that i cannot use the IF_EXISTS ability from the extract() function.

if i have a request[key] that is not expected, i would store it, which is a thing i would like to avoid...

thanks any way.
Nov 6 '08 #3
Atli
5,058 Expert 4TB
Then why don't you just pick out the elements you want and leave the rest?

Check out the array_intersect_key function.
It should do the trick.
Nov 6 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Logical | last post by:
I wanted to do: include('page.htm?id=12&foo=bar'); But since I can't (and don't want to make another seperate HTTP request with include('http://...')); I was wondering if there's a function...
11
by: Ren | last post by:
Suppose I have a file containing several lines similar to this: :10000000E7280530AC00A530AD00AD0B0528AC0BE2 The data I want to extract are 8 hexadecimal strings, the first of which is E728,...
45
by: Curt Geske | last post by:
I'm suprised no one suggested a union! #include <stdio.h> union _x { long lng; char byt; } X; void main( void )
4
by: Chris | last post by:
I am calling a COM object that returns a type Object and it contains two strings but I can't get them out. Here is the command: object ParamNameObj= localxPCCOMOBJ.xPCTarget.GetParamName (1); ...
9
by: chrisspencer02 | last post by:
I am looking for a method to extract the links embedded within the Javascript in a web page: an ActiveX component, or example code in C++/Pascal/etc. I am looking for a general solution, not one...
2
by: kimi | last post by:
Hi ppl, I am new to PHP. I would need some information on the following: 1. a) I wanted to know from where the data is extracted and stroed in the global assocoative arrays ( specifically...
22
Geeza1973
by: Geeza1973 | last post by:
Below is the code I have: <?php include("db.php"); mysql_connect ($host, $user, $pass); mysql_select_db ($database); $result = mysql_query ("SELECT * FROM `highscore` ORDER BY score DESC LIMIT...
1
by: =?Utf-8?B?SkI=?= | last post by:
Hello My pgm1 (User Interface Level) passes an empty ArrayList to pgm2 (Business Logic Level). pgm2 then calls pgm3 (Data Access Level) to populate the ArrayList. Question1: When pgm2 gets...
2
by: HTCAthenaGuy | last post by:
Hey ive got a simple problem here im using Forum.Document.Links to extract all links from a webpage loaded into a webbrowser control . Some of the links contain url variables like the subscribe...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.