Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

Ok. I need to get keys, i think they they are called keys.

When i var_dump object i get

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" }

Code itself

$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/>';  
}

For now i am getting back

2
Old wood table
25
65
50
60
wood

Result i need

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

I tried to use key(array) and next(array)

foreach($productsArray as $attribute){

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

but the results was

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

It seems like it's skipping id key. Any ideas? Thanks

share|improve this question

1 Answer 1

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

try this

share|improve this answer
    
Thanks. Only $key is first echo $key . '-' . $attribute; –  grab Feb 26 '14 at 19:33
    
sure i add this only to the end to demonstrate it –  Stricted Feb 26 '14 at 19:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.