从组数组中读取

发布于 2024-12-26 14:16:30 字数 836 浏览 2 评论 0 原文

+---------------+-----------------+
| Type          |  Price          |
+---------------+-----------------+
| Music         |  19.99          |
| Music         |   3.99          |
| Music         |  21.55          |
| Toy           |  89.95          |
| Toy           |   3.99          |
| Phones        |  99.99          |
| Phones        |  89.99          |
+---------------+-----------------+

我使用此代码将上述数据显示如下:

// Get the sum
$group = array();
foreach($rows as $r){
  $group[$r->type] = $group[$r->Type] + $r->Price;
}

// Display
foreach($group as $type=>$price){
  echo "Type:".$type." Price: ".$price;
}

结果:

Music  | 45.53
Toy    | 93.94
Phones | 189.98

我的问题是如何从第二条记录开始显示数组 ($group) 中的数据,以及如何仅显示其中的一条记录数组。

+---------------+-----------------+
| Type          |  Price          |
+---------------+-----------------+
| Music         |  19.99          |
| Music         |   3.99          |
| Music         |  21.55          |
| Toy           |  89.95          |
| Toy           |   3.99          |
| Phones        |  99.99          |
| Phones        |  89.99          |
+---------------+-----------------+

I have the above data displayed as below using this code:

// Get the sum
$group = array();
foreach($rows as $r){
  $group[$r->type] = $group[$r->Type] + $r->Price;
}

// Display
foreach($group as $type=>$price){
  echo "Type:".$type." Price: ".$price;
}

Result:

Music  | 45.53
Toy    | 93.94
Phones | 189.98

My problem is how to display the data from the array ($group) starting from the second record and also how to display just one record from the array.

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

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

发布评论

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

评论(3

遇见了你 2025-01-02 14:16:30

尝试使用 array_slice() ;

//this will create new array without first element
$new_group = array_slice($group, 1);

现在像以前一样迭代 $new_group

Try using array_slice();

//this will create new array without first element
$new_group = array_slice($group, 1);

And now iterate over $new_group as before.

睡美人的小仙女 2025-01-02 14:16:30

如果内存不是问题(数组不是很大),请使用 array_slice 作为 Peter Krejci 建议。但是,如果这是一个问题,您将希望通过简单地跳过第一个元素来避免重复所有数据:

$first = true;
foreach ($group as $type=>$price) {
    if ($first) {
        $first = false;
        continue;
    }
    echo "Type:", $type, " Price: ", $price;
}

我使用此方法而不是直接数字索引,因为不能保证索引是无间隙的。人们也可以使用内部指针函数,例如 next 但那些在这个迭代器接口时代已经失宠了。

还有如何仅显示数组中的一条记录

这取决于您希望通过哪些条件访问它。如果您想按类型执行此操作,您可以简单地:

echo $group['Music'];

If memory is not a concern (the array is not very large), use array_slice as suggested by Peter Krejci. However, if it is a concern, you'll want to avoid duplicating all that data by simply skipping the first element:

$first = true;
foreach ($group as $type=>$price) {
    if ($first) {
        $first = false;
        continue;
    }
    echo "Type:", $type, " Price: ", $price;
}

I'm using this method rather than direct numerical index as there's no guarantee that the indexes are gapless. One could alternatively use the internal pointer functions, e.g., next but those have fallen out of favor in this age of the Iterator interface.

also how to display just one record from the array

That depends which criteria you wish to access it by. If you want to do it by type, you can simply:

echo $group['Music'];
长伴 2025-01-02 14:16:30

尝试array_shift($group),然后尝试foreach()

来自 array_shift() 文档

array_shift() 将数组的第一个值移开并返回它,
将数组缩短一个元素并将所有内容向下移动。全部
数字数组键将被修改为从零开始计数
而文字键不会被触及。

Try array_shift($group), and then the foreach().

From the array_shift() documentation:

array_shift() shifts the first value of the array off and returns it,
shortening the array by one element and moving everything down. All
numerical array keys will be modified to start counting from zero
while literal keys won't be touched.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文