0

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

1 Answer 1

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

try this

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Only $key is first echo $key . '-' . $attribute;
sure i add this only to the end to demonstrate it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.