添加到具有键值对多维数组 [英] add to multidimensional array with key value pair

查看:156
本文介绍了添加到具有键值对多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林返回类似于这个想法阵列

Im returning a array that resembles this idea

Array
(
    [0] => Array
        (
            [GRP] => "Group1"
            [ITM] => "Item1"
        )
    [1] => Array
        (
            [GRP] => "Group1"
            [ITM] => "Item2"
        )
    [2] => Array
        (
            [GRP] => "Group1"
            [ITM] => "Item1"
        )
    [3] => Array
        (
            [GRP] => "Group2"
            [ITM] => "Item1"
        )
    [4] => Array
        (
            [GRP] => "Group2"
            [ITM] => "Item2"
        )
)

我希望能够通过数组搜索和总结的项目金额
对于上面的例子的结果应该是这样

I want to be able to search through the array and sum the amount of items For the above example the result should be something like

Array
(
    ["Group1"] => Array
        (
            ["Item1"] => 2
            ["Item2"] => 1
        )
    ["Group2"] => Array
        (
            ["Item1"] => 1
            ["Item2"] => 1
        )
)

但我不知道如何处理这一点。

But I have no idea how to approach this.

我猜的搜索棘手的我。
我虽然使用成才这样的

I guess the searching is tricky for me. I though of using someting like this

foreach($array as $row){
    $grp = $row["GRP"];
    $itm = $row["ITM"];

    $grpFound = array_search($grp, $newArray);
    if($grpFound){
        //GRP found, now search for ITM
        $itmFound = array_search($grp, $newArray[$grp]);
        if($itmFound){
            // increase the key value of the item
            $newArray[$grp][$itm] += 1
        }else{
            //Add new item to array group with a item value of 1
            $newArray[$grp] = [$itm => 1]
        }
    }else{
        // ADD new group to array
        $newArray[] = $grp;
    }
}

这是我在我的头,我知道它的种类伪杂交code,但是如果在这里正确的轨道上的IM,请告诉我。

This is what I have in my head, I know its kind of psuedo-ish code, but please tell me if im on the right track here.

推荐答案

您是pretty密切 - 只需要一个循环中的几个条件语句

You're pretty close - just need a few conditionals within a loop.

工作原型:

$aEntry = array();
$aEntry[ 'GRP' ] = 'Group1';
$aEntry[ 'ITM' ] = 'Item1';

$aEntry2 = array();
$aEntry2[ 'GRP' ] = 'Group1';
$aEntry2[ 'ITM' ] = 'Item2';

$aEntry3 = array();
$aEntry3[ 'GRP' ] = 'Group1';
$aEntry3[ 'ITM' ] = 'Item1';

$aEntry4 = array();
$aEntry4[ 'GRP' ] = 'Group2';
$aEntry4[ 'ITM' ] = 'Item1';

$aEntry5 = array();
$aEntry5[ 'GRP' ] = 'Group2';
$aEntry5[ 'ITM' ] = 'Item2';

$aEntry6 = array();
$aEntry6[ 'GRP' ] = 'Group2';
$aEntry6[ 'ITM' ] = 'Item1';

$aEntry7 = array();
$aEntry7[ 'GRP' ] = 'Group3';
$aEntry7[ 'ITM' ] = 'Item1';

$aData = array();
$aData[] = $aEntry;
$aData[] = $aEntry2;
$aData[] = $aEntry3;
$aData[] = $aEntry4;
$aData[] = $aEntry5;
$aData[] = $aEntry6;
$aData[] = $aEntry7;

$aFormatted = array();
$iCountData = count( $aData );
for( $i = 0; $i < $iCountData; ++$i )
{
    $vKeyToAppendTo = '';
    foreach( $aFormatted as $sKey => $aValues )
    {
        if( ( $aValues[ 0 ][ 'GRP' ] == $aData[ $i ][ 'GRP' ] ) )
        {
            // Set the array key to append to.
            $vKeyToAppendTo = $sKey;
            break;
        }
    }
    // If we have a key match append.
    if( !empty( $vKeyToAppendTo ) )
    {
        $aFormatted[ $vKeyToAppendTo ][] = $aData[ $i ];
    }
    // Otherwise create the new lat/lng entry group.
    else
    {
        $aFormatted[ $aData[ $i ][ 'GRP' ] ][] = $aData[ $i ];
    }
}
var_dump( $aFormatted );

这篇关于添加到具有键值对多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆