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

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

报告相同问题?

悬赏问题

  • ¥30 backtrader对于期货交易的现金和资产计算的问题
  • ¥15 求C# .net4.8小报表工具
  • ¥15 安装虚拟机时出现问题
  • ¥15 Selenium+docker Chrome不能运行
  • ¥15 mac电脑,安装charles后无法正常抓包
  • ¥18 visio打开文件一直显示文件未找到
  • ¥15 请教一下,openwrt如何让同一usb储存设备拔插后设备符号不变?
  • ¥50 使用quartz框架进行分布式任务定时调度,启动了两个实例,但是只有一个实例参与调度,另外一个实例没有参与调度,不知道是为什么?请各位帮助看一下原因!!
  • ¥50 怎么获取Ace Editor中的python代码后怎么调用Skulpt执行代码
  • ¥30 fpga基于dds生成幅值相位频率和波形可调的容易信号发生器。