多维数组中的array_slice?

发布于 2024-09-25 12:24:04 字数 1448 浏览 0 评论 0原文

我在 php 中有一个像这样的数组:

Array
(
    [0] => Array
        (
              [915] => 1
              [1295] => 1
              [1090] => 1
              [1315] => 0.93759357774
              [128] => 0.93759357774
              [88] => 0.731522789561
              [1297] => 0.731522789561
              [1269] => 0.525492880722
              [1298] => 0.525492880722
              [121] => 0.519133966069
         )
   [1] => Array
       (
              [585] => 1
              [1145] => 1
              [1209] => 1
              [375] => 1
              [1144] => 1
              [913] => 1
              [1130] => 0.996351158355
              [215] => 0.937096401456
              [1296] => 0.879373313559
              [30] => 0.866473953643
              [780] => 0.866473953643
              [1305] => 0.866473953643
              [1293] => 0.866473953643
       )

) 

如何获取每个数组的子数组的第 1-5 行,如下所示:

结果:

Array
(
    [0] => Array
        (
              [915] => 1
              [1295] => 1
              [1090] => 1
              [1315] => 0.93759357774
              [128] => 0.93759357774
         )
   [1] => Array
       (
              [585] => 1
              [1145] => 1
              [1209] => 1
              [375] => 1
              [1144] => 1
       )

)

I have an array in php like this :

Array
(
    [0] => Array
        (
              [915] => 1
              [1295] => 1
              [1090] => 1
              [1315] => 0.93759357774
              [128] => 0.93759357774
              [88] => 0.731522789561
              [1297] => 0.731522789561
              [1269] => 0.525492880722
              [1298] => 0.525492880722
              [121] => 0.519133966069
         )
   [1] => Array
       (
              [585] => 1
              [1145] => 1
              [1209] => 1
              [375] => 1
              [1144] => 1
              [913] => 1
              [1130] => 0.996351158355
              [215] => 0.937096401456
              [1296] => 0.879373313559
              [30] => 0.866473953643
              [780] => 0.866473953643
              [1305] => 0.866473953643
              [1293] => 0.866473953643
       )

) 

How do I get the 1st-5th rows of sub-array for each array, like this :

Result :

Array
(
    [0] => Array
        (
              [915] => 1
              [1295] => 1
              [1090] => 1
              [1315] => 0.93759357774
              [128] => 0.93759357774
         )
   [1] => Array
       (
              [585] => 1
              [1145] => 1
              [1209] => 1
              [375] => 1
              [1144] => 1
       )

)

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

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

发布评论

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

评论(3

递刀给你 2024-10-02 12:24:04
$multid_array = array(/* Your Multidimensional array from above*/);

$sliced_array = array();  //setup the array you want with the sliced values.

//loop though each sub array and slice off the first 5 to a new multidimensional array
foreach ($multid_array as $sub_array) {
    $sliced_array[] = array_slice($sub_array, 0, 5);
}

然后 $sliced_array 将包含您想要的输出。

$multid_array = array(/* Your Multidimensional array from above*/);

$sliced_array = array();  //setup the array you want with the sliced values.

//loop though each sub array and slice off the first 5 to a new multidimensional array
foreach ($multid_array as $sub_array) {
    $sliced_array[] = array_slice($sub_array, 0, 5);
}

The $sliced_array will then contain the output you wanted.

醉酒的小男人 2024-10-02 12:24:04
  • 迭代数组。
  • 通过参考读取值。
  • 删除从偏移量5开始的键值直到
    结束。您不需要收集返回值,因为我们正在使用对原始数组的引用。

foreach($mainArray as $key => &$value) {
  array_splice($value,5);
}

工作 ideone 链接

  • Iterate over the array.
  • Read the value by reference.
  • Delete key-values from offset 5 till
    the end. You need not collect the return value because we are using the reference to the original array.

.

foreach($mainArray as $key => &$value) {
  array_splice($value,5);
}

Working ideone link

☆獨立☆ 2024-10-02 12:24:04

您可能想研究一下 php 函数 array_splice。

http://no.php.net/manual/en/function.array -slice.php

You might want to look into the php function array_splice.

http://no.php.net/manual/en/function.array-slice.php

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