数组推送问题

发布于 2024-10-31 00:47:34 字数 1064 浏览 0 评论 0原文

我在将对象推入数组时遇到问题。

这是我的对象

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅黛梨妆こ 2024-11-07 00:47:34

我不知道你的确切需求,但值得尝试一个简单的数组转换。

$value = (array) $Product;

你的 cvrrent 方法的问题似乎是零评估为假,我认为严格的比较应该解决这个问题。

    $value = array();

    while (next($Product) !== false) {
        $constant = key($Product);  
        array_push($value, $constant.", ".$Product->$constant);         
        echo $constant."<br>";
    }

无论如何,另一个答案中的 foreach 可能是一个更好的主意,但如果您出于某种原因更喜欢 while 循环,则需要注意与零的比较。

I don't know your exact needs, but its worth trying a simple cast to array.

$value = (array) $Product;

The problem with your cvrrent approach seems to be the zero evaluating to false, i think a strict compare should fix that.

    $value = array();

    while (next($Product) !== false) {
        $constant = key($Product);  
        array_push($value, $constant.", ".$Product->$constant);         
        echo $constant."<br>";
    }

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.

最美不过初阳 2024-11-07 00:47:34

在这种情况下,使用 foreach 循环可能是一个更好的主意:

$value = array();
foreach($obj as $key => $val)
{
  array_push($value, sprintf("%s, %s", $key, $val));
}

Using a foreach loop might be a better idea, in this case:

$value = array();
foreach($obj as $key => $val)
{
  array_push($value, sprintf("%s, %s", $key, $val));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文