2012-12-11 112 views
0

如何將屬性添加到數組對象變量? 比方說,我有可變對象是這樣的:將屬性變量添加到對象數組php

Array 
(
    [0] => stdClass Object 
     (
      [category] => Nuklir 
      [id] => 715 
      [title] => Testing Article Nuklir 
      [alias] => testing-article-nuklir 
      [title_alias] => 
      [introtext] => <p><span style="color: #666666; font-family: 'Lucida Grande', Arial, sans-serif; font-size: 13px; line-height: 19px;" mce_style="color: #666666; font-family: 'Lucida Grande', Arial, sans-serif; font-size: 13px; line-height: 19px;">Vivamus libero eros, dictum id aliquam in, tristique id sapien. Maecenas rhoncus malesuada aliquet. Morbi vulputate vulputate mauris quis condimentum. Ut ut ligula et mauris accumsan tristique ut nec lacus.</span></p><p><span style="color: #666666; font-family: 'Lucida Grande', Arial, sans-serif; font-size: 13px; line-height: 19px;" mce_style="color: #666666; font-family: 'Lucida Grande', Arial, sans-serif; font-size: 13px; line-height: 19px;">Vivamus libero eros, dictum id aliquam in, tristique id sapien. Maecenas rhoncus malesuada aliquet. Morbi vulputate vulputate mauris quis condimentum. Ut ut ligula et mauris accumsan tristique ut nec lacus.</span></p><p><span style="color: #666666; font-family: 'Lucida Grande', Arial, sans-serif; font-size: 13px; line-height: 19px;" mce_style="color: #666666; font-family: 'Lucida Grande', Arial, sans-serif; font-size: 13px; line-height: 19px;">Vivamus libero eros, dictum id aliquam in, tristique id sapien. Maecenas rhoncus malesuada aliquet. Morbi vulputate vulputate mauris quis condimentum. Ut ut ligula et mauris accumsan tristique ut nec lacus.</span><br /></p> 
      [fulltext] => 
      [sectionid] => 7 
      [state] => 1 
      [catid] => 25 
      [created] => 2012-12-10 07:51:03 
      [created_by] => 110 
      [created_by_alias] => 
      [modified] => 0000-00-00 00:00:00 
      [modified_by] => 0 
      [checked_out] => 0 
      [checked_out_time] => 0000-00-00 00:00:00 
      [publish_up] => 2012-12-10 07:51:03 
      [publish_down] => 0000-00-00 00:00:00 
      [hits] => 0 
      [images] => 
      [urls] => 
      [ordering] => 1 
      [metakey] => 
      [metadesc] => 
      [access] => 0 
      [slug] => 715:testing-article-nuklir 
      [catslug] => 25:nuklir 
      [readmore] => 0 
      [author] => migrasi 
      [usertype] => Super Administrator 
      [groups] => Public 
      [author_email] => [email protected] 
     ) 

) 

我想在這裏什麼,是添加新的特性如。 [site_id] => 1 [author_email]下方。可能嗎?

+0

對象屬性沒有特別排序(儘管數組鍵是...),但是你有一個索引爲[[0]''so'$ yourvar [0] - > site_id = 1;' –

回答

2

這是你所擁有的基本(與對象的數組的索引0)...

$array = array(new stdclass); 
print_r($array); 

這給你

Array (
    [0] => stdClass Object 
     (
     ) 

) 

現在你想添加一些屬性添加到對象...

$array[0]->site_id = 1; 
print_r($array); 

這會給你

Array 
(
    [0] => stdClass Object 
     (
      [site_id] => 1 
     ) 

) 
+0

謝謝,它的作品! –

+0

@MuthuKumaran請給我指出任何提供信息如何設置爲正確答案的文檔。我只是看到選項「是這個職位對你有用:是|否」 –

+0

@MuthuKumaran好吧,請不理我以前的評論,我明白了,thx :) –