emehrkay Posted March 16, 2006 Share Posted March 16, 2006 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" Quote Link to comment https://forums.phpfreaks.com/topic/5085-i-need-help-making-an-array-out-of-an-object/ Share on other sites More sharing options...
ober Posted March 16, 2006 Share Posted March 16, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/5085-i-need-help-making-an-array-out-of-an-object/#findComment-18060 Share on other sites More sharing options...
emehrkay Posted March 16, 2006 Author Share Posted March 16, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/5085-i-need-help-making-an-array-out-of-an-object/#findComment-18090 Share on other sites More sharing options...
ober Posted March 16, 2006 Share Posted March 16, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/5085-i-need-help-making-an-array-out-of-an-object/#findComment-18093 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.