2012-10-04 31 views
0

附加價值我想在這裏補充一些值:如何在多維數組在PHP

Array 
(
    [0] => stdClass Object 
     (
      [id] => 1 
      [cat_id] => 1 
      [lot_id] => 1 
      [brand] => Dell 
      [model] => D630 
      [unit_aed] => 800 
      [sold] => 0 
      ** i want to append value here like this 
      [username] => name 

     ) 

    [2] => stdClass Object 
     (
      [id] => 4 
      [cat_id] => 0 
      [lot_id] => 0 
      [brand] => dell 
      [model] => e6400 
      [unit_aed] => 0 
      [sold] => 0 
     ) 


) 

怎麼可能呢?我正在嘗試array_push($array, 'username')

它可以用簡單的數組正常工作,但不適用於stdClass對象數組。

+1

Object!= array –

回答

0

添加另一個對象鍵和值:

$array[0]->username = 'name'; 

或者你可以在對象添加username到每個值:

foreach($array as $key => $val){ 
    $array[$key]->username = 'user'; 
} 
+0

謝謝親愛的朋友 – anosim

0
$array_name[0]->username = "name"; 
$array_name[0]->new_value = "whatever"; 
$array_name[0]->another_new_value = "whatever again"; 

等等

+0

感謝Buddyyyy – anosim

0
$myclass = new stdClass(); 
$myclass->id = 4; 
$array[] = $myclass;