dongpiao9078 2014-02-26 19:22
浏览 15
已采纳

获取数组键对象PHP

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条回答 默认 最新

  • doubingling4706 2014-02-26 19:25
    关注
    foreach($productsArray as $key => $attribute){
       echo  $attribute . '-' . $key . '<br/>';  
    }
    

    try this

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看