Jump to content

i need help making an array out of an object


emehrkay

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"
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.