数组推送问题
我在将对象推入数组时遇到问题。
这是我的对象
Products Object
(
[id] =>
[title] => Titel
[articlenumber] => Artikelnummer
[price] => Prijs
[sale_price] => Sale Prijs
[description] => Tekst
[views] => 1
[brand] => Merk
[soled] => 0
[start_date] => 2011-04-21
[end_date] => 2011-04-28
[active] => 2
[sale_text] => Sale Tekst
)
这是我的数组我尝试将所有内容推送到数组
Array
(
[0] => title, Titel
[1] => articlenumber, Artikelnummer
[2] => price, Prijs
[3] => sale_price, Sale Prijs
[4] => description, Tekst
[5] => views, 1
[6] => brand, Merk
)
正如你所看到的,当他到达“soled”项时我的代码停止了,它这样做是因为该值是0。当我将此值放入其他值时如果工作正常的话。
这是我使用的代码。
$value = array();
while (next($Product)) {
$constant = key($Product);
array_push($value, $constant.", ".$Product->$constant);
echo $constant."<br>";
}
I'm having a problem with pushing an object into an array.
Here's my object
Products Object
(
[id] =>
[title] => Titel
[articlenumber] => Artikelnummer
[price] => Prijs
[sale_price] => Sale Prijs
[description] => Tekst
[views] => 1
[brand] => Merk
[soled] => 0
[start_date] => 2011-04-21
[end_date] => 2011-04-28
[active] => 2
[sale_text] => Sale Tekst
)
And here is my array I tryed to push everything to an array
Array
(
[0] => title, Titel
[1] => articlenumber, Artikelnummer
[2] => price, Prijs
[3] => sale_price, Sale Prijs
[4] => description, Tekst
[5] => views, 1
[6] => brand, Merk
)
As you can see my code stops when he comes to the item "soled", it does it because the value is 0. When I put this value to something else if works fine.
Here is the code im using.
$value = array();
while (next($Product)) {
$constant = key($Product);
array_push($value, $constant.", ".$Product->$constant);
echo $constant."<br>";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道你的确切需求,但值得尝试一个简单的数组转换。
你的 cvrrent 方法的问题似乎是零评估为假,我认为严格的比较应该解决这个问题。
无论如何,另一个答案中的 foreach 可能是一个更好的主意,但如果您出于某种原因更喜欢 while 循环,则需要注意与零的比较。
I don't know your exact needs, but its worth trying a simple cast to array.
The problem with your cvrrent approach seems to be the zero evaluating to false, i think a strict compare should fix that.
The
foreach
in the other answer is probably a better idea anyhow, but if you prefer the while loop for whatever reason you need to watch out for the comparison on that zero.在这种情况下,使用 foreach 循环可能是一个更好的主意:
Using a foreach loop might be a better idea, in this case: