简体   繁体   English

PHP-按键值分组数组

[英]PHP - Group Array by Key Value

Trying to group an array depending on whether it's index is divisible by there. 尝试根据数组的索引是否可以被数组整除来对数组进行分组。 I've tried various combinations of for loops and foreach loops and if($i % 3 == 0) ... But, it's not outputting the way I need it. 我已经尝试过for循环和foreach循环以及if($ i%3 == 0)的各种组合...但是,它并没有输出我需要的方式。

Here is a greatly simplified array (the original data contains a lot of highly sensitive information...) 这是一个大大简化的数组(原始数据包含许多高度敏感的信息...)

The original array looks similar to this: 原始数组与此类似:

$item[0]['Section 1']['Item 2'] => 1334;
$item[1]['Section 2']['Item 3'] => 15454;
$item[2]['Section 3']['Item 4'] => 1452;
$item[3]['Section 4']['Item 5'] => 1341;
$item[4]['Section 5']['Item 6'] => 1788655;
$item[5]['Section 6']['Item 7'] => 13;
$item[6]['Section 7']['Item 8'] => 142;
$item[7]['Section 8']['Item 9'] => 15678;
$item[8]['Section 9']['Item 10'] => 15542;
$item[9]['Section 10']['Item 11'] => 16578;
$item[10]['Section 11']['Item 12'] => 18452;
$item[11]['Section 12']['Item 13'] => 16565;

I'm trying to group every 3 records, like this: 我正在尝试对每3条记录进行分组,如下所示:

$newitem[0]['Section 1']['Item 2'] => 1334;
$newitem[0]['Section 2']['Item 3'] => 15454;
$newitem[0]['Section 3']['Item 4'] => 1452;
$newitem[1]['Section 4']['Item 5'] => 1341;
$newitem[1]['Section 5']['Item 6'] => 1788655;
$newitem[1]['Section 6']['Item 7'] => 13;
$newitem[2]['Section 7']['Item 8'] => 142;
$newitem[2]['Section 8']['Item 9'] => 15678;
$newitem[2]['Section 9']['Item 10'] => 15542;
$newitem[3]['Section 10']['Item 11'] => 16578;
$newitem[3]['Section 11']['Item 12'] => 18452;
$newitem[3]['Section 12']['Item 13'] => 16565;

Suggestion use array_chunk http://www.php.net/manual/en/function.array-chunk.php 建议使用array_chunk http://www.php.net/manual/zh/function.array-chunk.php

$item = array_chunk($item, 3);

Or 要么

$newItem = array ();
$x = 0;
$t = 0;
foreach ( $item as $value ) {
    if ($x < 3) {
        $newItem [$t] [$x] = $value;
        $x ++;
    } else {
        $t ++;
        $x = 0;
        $newItem [$t] = array ();
    }
}

var_dump ( $newItem );

array_chunk is the one you need it. array_chunk是您需要的那个。 and you should iterate the new array as for two dimentional array as you can see that your new array has same index 0 for 3 elements. 并且您应该像对两个二维数组一样迭代新数组,因为您会看到新数组对3个元素具有相同的索引0。

try: 尝试:

http://www.php.net/manual/en/function.array-chunk.php http://www.php.net/manual/zh/function.array-chunk.php

This should do it: 应该这样做:

$newindex = 0;
$newitem = array();
for($i = 0; $i < count($item); ++$i)
{
    $newindex = (int) $i / 3;
    if(!isset($newitem[ $newindex ]))
        $newitem[ $newindex ] = array();

    $newitem[ $newindex ] = array_merge($newitem[ $newindex ], $item[ $i ]);
}

Link to output (dump of $newitem ) 链接到输出$newitem转储)

The trick is (int) $i / 3 , which takes $i , divides it by 3 , then casts to int which truncates any decimal places. 诀窍是(int) $i / 3 ,它接受$i ,将其除以3 ,然后转换为int并截断任何小数位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM