Jump to content

Archived

This topic is now archived and is closed to further replies.

emehrkay

i need help making an array out of an object

Recommended Posts

i have this object
[code]
Array
(
    [0] => stdClass Object
        (
            [first_name] => Jennifer
            [last_name] => Masi
            [person_id] => 1309
        )

    [1] => stdClass Object
        (
            [first_name] => Roy
            [last_name] => Mc
            [person_id] => 2469
        )

)
[/code]

now i want loop through that and put the person_id as the key and first & last names as the value. i tried array_push, but i dont think you can define what the key will be with that function. is there any way for me to do this?

my current line of thinking ($this->_data_contact is the object):
[code]
$con_count = count($this->_data_contact);
        
        $con_arr   = array($arr_val);
        for($i = 0; $i < $con_count; $i++){
            array_push ($con_arr, $this->_data_contact[$i]->person_id."=>".$this->_data_contact[$i]->first_name." ".$this->_data_contact[$i]->last_name);
        }
[/code]

that only creates
[0]=>"id=>first last"
[1]=>"id=>first last"

Share this post


Link to post
Share on other sites
Why can't you just set the keys normally?
[code]
for($i = 0; $i < $con_count; $i++)
      $con_arr[$this->_data_contact[$i]->person_id] = $this->_data_contact[$i]->first_name." ".$this->_data_contact[$i]->last_name;
[/code]

By the way, it may be force of habit for some, but you don't need braces around a single-line for/if/while.

Share this post


Link to post
Share on other sites
thanks ober you always come through in the clutch, like kobe or mj.

yeah i think the brackets is a habit, and i like to see my code done that way. oh, most importantly, its a standard at my job - lol

Share this post


Link to post
Share on other sites
Glad to help :)

I know some people are forced to do it, but I know there are a lot of people that don't know you can skip them.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.