Welcome Guest, Not a member yet? Register   Sign In
Dynamic variable from object
#1

[eluser]skribe[/eluser]
Hi folks:

I'm having some difficulty in working how to achieve this. I have a (join) table that holds contact information.

person
contact_type
contact_details

Different people have different contact types and I want to be able to separate them out so I can do the update form of my CRUD. Is there a way for my to dynamically create variables from an object?

Code:
For instance:
foreach($contact as $item):
    $ . $item->type . = . $item->details;
endforeach;


If not is there an alternative method?

TIA

skribe
#2

[eluser]Jaketoolson[/eluser]
if I understand this correctly, you are referring to 'Variable Variables'?

This is how I used to do it:
Code:
$options['stn_id'] = 77;        
$options['date'] = date('Y-m');
$options['name'] = 'Joseph';
    
foreach ($options as $k => $v)
{
    $$k = $v;
}

This would result with the following variables being created:
Code:
$stn_id = 77;
$date = '2011-02';
$name = 'Joseph';

But see the link at the top for better methods.
#3

[eluser]skribe[/eluser]
Thanks for the help. Apologies if I'm repeating myself and the help/code that you've supplied fixes this. I'm being dim (as usual =).

This is what I'm trying to extract from:

Code:
Array ( [0] => stdClass Object ( [person_id] => 2 [contact_id] => 1 [details] => [email protected] [con_id] => 1 [type] => Email ) [1] => stdClass Object ( [person_id] => 2 [contact_id] => 2 [details] => 12345678 [con_id] => 2 [type] => Primary Phone ) )

I want to be able to extract the type/con_id and assign the relevant details to it.




Theme © iAndrew 2016 - Forum software by © MyBB