php中如何自动知道数组的字段名称?

发布于 2024-12-28 06:03:18 字数 1960 浏览 1 评论 0原文

我只是有一个小问题,我找不到任何答案,因为我不知道如何在谷歌上明确地表达它。

所以我从查询中返回了这个数组:

Array
(
    [0] => Array
        (
            [aad3177ec0cbced7a15fae003bdd0ff3] => Array
                (
                    [rowid] => aad3177ec0cbced7a15fae003bdd0ff3
                    [id] => 21
                    [qty] => 1
                    [price] => 176
                    [name] => KRUPS YY1305 
                    [image] => utilities/images/products/p2070396dt.jpg
                    [description] => Menu simplifié pour une convivialité optimale.   
                    [subtotal] => 176
                )
        )
)

我想获取所有信息 rowid、id、qty ...,通过 foreach 循环将其存储在另一个变量中,而不知道第二个数组的名称:“aad3177ec0cbced7a15fae003bdd0ff3”。我只想在用户注销时从存储在数据库中的购物车中检索文章。

那么我该怎么说:

echo $myArray['0'][theNumberGoesHereAutomatically]['price'];

理想情况下,我的代码看起来像这样:

foreach ($data2 as $cart){
            $data = array(
                   'rowid'      => $cart_unserialized[$token]['aad3177ec0cbced7a15fae003bdd0ff3']['rowid'],
                   'id'         => $cart_unserialized[$token]['aad3177ec0cbced7a15fae003bdd0ff3']['id'],
                   'qty'        => $cart_unserialized[$token]['aad3177ec0cbced7a15fae003bdd0ff3']['qty'],
                   'price'      => $cart_unserialized[$token]['aad3177ec0cbced7a15fae003bdd0ff3']['price'],
                   'name'       => $cart_unserialized[$token]['aad3177ec0cbced7a15fae003bdd0ff3']['name'],
                   'image'      => $cart_unserialized[$token]['aad3177ec0cbced7a15fae003bdd0ff3']['image'],
                   'description'=> $cart_unserialized[$token]['aad3177ec0cbced7a15fae003bdd0ff3']['description'],
                );
            $this->cart->insert($data2);
            $token++;
        };

在不知道 aad3177ec0cbced7a15fae003bdd0ff3 的情况下。

有什么想法吗?谢谢 :)

I just have a little question, I can't find any answer since I don't know exactly how to formulate it on Google.

So I have this array returned from a query:

Array
(
    [0] => Array
        (
            [aad3177ec0cbced7a15fae003bdd0ff3] => Array
                (
                    [rowid] => aad3177ec0cbced7a15fae003bdd0ff3
                    [id] => 21
                    [qty] => 1
                    [price] => 176
                    [name] => KRUPS YY1305 
                    [image] => utilities/images/products/p2070396dt.jpg
                    [description] => Menu simplifié pour une convivialité optimale.   
                    [subtotal] => 176
                )
        )
)

And I want to get all the information rowid, id, qty ... to store it through a foreach loop in another variable without knowing the name of the second array : 'aad3177ec0cbced7a15fae003bdd0ff3'. I just want to retrieve articles from a cart stored in a database when user logged out.

So how can I say something like :

echo $myArray['0'][theNumberGoesHereAutomatically]['price'];

Ideally, my code would look like that :

foreach ($data2 as $cart){
            $data = array(
                   'rowid'      => $cart_unserialized[$token]['aad3177ec0cbced7a15fae003bdd0ff3']['rowid'],
                   'id'         => $cart_unserialized[$token]['aad3177ec0cbced7a15fae003bdd0ff3']['id'],
                   'qty'        => $cart_unserialized[$token]['aad3177ec0cbced7a15fae003bdd0ff3']['qty'],
                   'price'      => $cart_unserialized[$token]['aad3177ec0cbced7a15fae003bdd0ff3']['price'],
                   'name'       => $cart_unserialized[$token]['aad3177ec0cbced7a15fae003bdd0ff3']['name'],
                   'image'      => $cart_unserialized[$token]['aad3177ec0cbced7a15fae003bdd0ff3']['image'],
                   'description'=> $cart_unserialized[$token]['aad3177ec0cbced7a15fae003bdd0ff3']['description'],
                );
            $this->cart->insert($data2);
            $token++;
        };

without knowing the aad3177ec0cbced7a15fae003bdd0ff3.

Any ideas? Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

哑剧 2025-01-04 06:03:18

您可以通过以下方式轻松完成此操作:

foreach ($data2 as $varname => $cart){

$varname 将包含 aad3177ec0cbced7a15fae003bdd0ff3

You can do this easily with:

foreach ($data2 as $varname => $cart){

$varname will contain aad3177ec0cbced7a15fae003bdd0ff3.

絕版丫頭 2025-01-04 06:03:18
foreach($array as $key => $value)
{
  echo $value['price'];
}
foreach($array as $key => $value)
{
  echo $value['price'];
}
忘东忘西忘不掉你 2025-01-04 06:03:18

您可以使用 key() 函数获取密钥

You can get the key with key() function

甜宝宝 2025-01-04 06:03:18

使用 current 函数:

foreach ($data2 as $token=>$cartUnserialized){
    $data = current($cartUnserialized);
    // .. proccess your data ..
};

Use current function:

foreach ($data2 as $token=>$cartUnserialized){
    $data = current($cartUnserialized);
    // .. proccess your data ..
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文