2013-04-04 94 views
1

我有一個對象值從對象值到一個數組

foreach($this->promo_cart->contents() as $produks){ 
    foreach($this->login_sess->contents() as $val): 
     $idex = array_unique(explode('_', $produks['prod_name'])); 
     if($val['id']==$idex[2]) { 
      echo $val[id]; //result is 314314315 
     } 
    endforeach; 
} 

如何使從結果數組,像

array 
(
    [0] => 314 
    [1] => 314 
    [2] => 315 
) 

回答

1

您可以添加元素,像這樣的數組:

$foo = array(); // start with empty array 
$foo[] = 'bar'; // added element to array => array([0] => "bar") 

所以你的情況,你可以這樣做:

$my_array = array(); 

foreach($this->promo_cart->contents() as $produks) 
{ 
    foreach($this->login_sess->contents() as $val) 
    { 
    $idex = array_unique(explode('_', $produks['prod_name'])); 

    if($val['id']==$idex[2]) { 
     $my_array[] = $val[id]; 
    } 
    } 
} 

$my_array將包含:

array 
(
    [0] => 314 
    [1] => 314 
    [2] => 315 
) 
+0

嘿感謝.. +1 – ranggadablues 2013-04-04 06:46:56

+0

不客氣。 – Mischa 2013-04-04 06:47:32

1

創建新的數組和所需的值與[] 推送到陣列試試這個

$tempArray=array(); //<---create new array 
foreach($this->promo_cart->contents() as $produks){ 
    foreach($this->login_sess->contents() as $val): 
    $idex = array_unique(explode('_', $produks['prod_name'])); 
    if($val['id']==$idex[2]) { 
     $tempArray[] = $val[id]; //<----push value to array. 
    } 
    endforeach; 
    } 
    print_r($tempArray); 
相關問題