2014-02-26 128 views
0

好的。 我需要得到鑰匙,我認爲他們被稱爲鑰匙。獲取數組密鑰對象PHP

當我的var_dump反對我得到

object(stdClass)#6 (7) { ["id"]=> string(1) "2" 
["title"]=> string(14) "Old wood table" 
["price"]=> string(2) "25" 
["size_w"]=> string(2) "65" 
["size_h"]=> string(2) "50" 
["size_l"]=> string(2) "60" 
["material"]=> string(4) "wood" } 

代碼本身

$catalog = new Catalog('furniture',2); 
if(!$catalog->exists()){ 
    die('Product dose not exist'); 
}else{ 
    $product = $catalog->data(); 
} 
echo $product->title; 
var_dump($product); 
$productsArray = get_object_vars($product); 

foreach($productsArray as $attribute){ 
    echo $attribute . '<br/>'; 
} 

對於現在我又回到

2 
Old wood table 
25 
65 
50 
60 
wood 

結果,我需要

id-2 
title-foo 
price-25 
size_w-65 
size_h-50 
size_l-60 
material-wood 

我試圖用鑰匙(陣列)和未來(陣列)

foreach($productsArray as $attribute){ 

    echo key($productsArray) . '- ' . $attribute . '<br/>'; 
    next($productsArray); 
} 

但結果是

title- 2 
price- Old wood table 
size_w- 25 
size_h- 65 
size_l- 50 
material- 60 
- wood 

好像它跳過ID鍵。 任何想法?由於

回答

2
foreach($productsArray as $key => $attribute){ 
    echo $attribute . '-' . $key . '<br/>'; 
} 

試試這個

+0

感謝。只有$ key是第一個'echo $ key。 ' - '。 $ attribute;' – grab

+0

當然,我只是把它添加到最後來演示它 – Stricted