在 php 中添加关联数组值?

发布于 2024-10-10 05:33:44 字数 3042 浏览 0 评论 0原文

我有一个具有以下属性的数组:

Array
(
    [0] => Array
        (
            [project] => test proposal
            [type] => pending
            [0] => 10,000
            [1] => 10,000
            [2] => 5,000
            [3] => 0
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
        )

    [1] => Array
        (
            [project] => test 3
            [type] => won
            [0] => 0
            [1] => 0
            [2] => 20,000
            [3] => 20,000
            [4] => 10,000
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
        )

    [2] => Array
        (
            [project] => Test 3
            [type] => pending
            [0] => 8,333
            [1] => 8,333
            [2] => 8,333
            [3] => 0
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
        )

) 

我想将最后一个项目推入数组,该数组组合了所有其他项目的值,项目和类型可以为空。所以结果将是:

Array
(
    [0] => Array
        (
            [project] => test proposal
            [type] => pending
            [0] => 10,000
            [1] => 10,000
            [2] => 5,000
            [3] => 0
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
        )

    [1] => Array
        (
            [project] => test 3
            [type] => won
            [0] => 0
            [1] => 0
            [2] => 20,000
            [3] => 20,000
            [4] => 10,000
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
        )

    [2] => Array
        (
            [project] => Test 3
            [type] => pending
            [0] => 8,333
            [1] => 8,333
            [2] => 8,333
            [3] => 0
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
        )

     [3] => Array
        (
            [project] => 
            [type] => 
            [0] => 18,333
            [1] => 18,333
            [2] => 33,333
            [3] => 20,000
            [4] => 10,000
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
        )
)

I have an array with the following properties:

Array
(
    [0] => Array
        (
            [project] => test proposal
            [type] => pending
            [0] => 10,000
            [1] => 10,000
            [2] => 5,000
            [3] => 0
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
        )

    [1] => Array
        (
            [project] => test 3
            [type] => won
            [0] => 0
            [1] => 0
            [2] => 20,000
            [3] => 20,000
            [4] => 10,000
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
        )

    [2] => Array
        (
            [project] => Test 3
            [type] => pending
            [0] => 8,333
            [1] => 8,333
            [2] => 8,333
            [3] => 0
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
        )

) 

I'd like to push a last item to the array that combines the values of all of the others, project and type can be blank. So the outcome would be:

Array
(
    [0] => Array
        (
            [project] => test proposal
            [type] => pending
            [0] => 10,000
            [1] => 10,000
            [2] => 5,000
            [3] => 0
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
        )

    [1] => Array
        (
            [project] => test 3
            [type] => won
            [0] => 0
            [1] => 0
            [2] => 20,000
            [3] => 20,000
            [4] => 10,000
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
        )

    [2] => Array
        (
            [project] => Test 3
            [type] => pending
            [0] => 8,333
            [1] => 8,333
            [2] => 8,333
            [3] => 0
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
        )

     [3] => Array
        (
            [project] => 
            [type] => 
            [0] => 18,333
            [1] => 18,333
            [2] => 33,333
            [3] => 20,000
            [4] => 10,000
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
        )
)

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

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

发布评论

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

评论(3

风吹雨成花 2024-10-17 05:33:44

也许像这样?:

$temp=array('project'=>'','type'=>'');
foreach($array as $project=> $data){
    foreach($data as $node=>$value){
        if(is_int($node) && is_int($value)){
            @$temp[$node]+=$value;
        }
    }
}
$array[]=$temp;

like this perhaps?:

$temp=array('project'=>'','type'=>'');
foreach($array as $project=> $data){
    foreach($data as $node=>$value){
        if(is_int($node) && is_int($value)){
            @$temp[$node]+=$value;
        }
    }
}
$array[]=$temp;
冷了相思 2024-10-17 05:33:44
foreach($array as $arr) {
    foreach($arr as $k => $v) {
        if($v== 'project' || $v == 'type') continue;
        $newArr[$k] = $newArr[$k] + $v;
    }
}
$array[] = $newArr;
foreach($array as $arr) {
    foreach($arr as $k => $v) {
        if($v== 'project' || $v == 'type') continue;
        $newArr[$k] = $newArr[$k] + $v;
    }
}
$array[] = $newArr;
绿光 2024-10-17 05:33:44
foreach ($data as $sub) {
    foreach ($sub as $key => $value) {
        if (is_numeric($key)) $sum[$key] += $value;
    }
}
$data[] = $sum;
foreach ($data as $sub) {
    foreach ($sub as $key => $value) {
        if (is_numeric($key)) $sum[$key] += $value;
    }
}
$data[] = $sum;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文