0

I have an array like tree and i want to get sum of id element any idea how to get??

Thanks

<?php

        $data=[
          0=>[
                'id'=>1,
                'date'=>'3/3/2017',
                'owner'=>'ownerName1',
                'extension'=>'yourExtension1',
                'filename'=>'YourFileName1',
                'childs'=>
                        [
                            0=>[
                                'id'=>3,
                                'date'=>'3/3/2017',
                                'owner'=>'ownerName3',
                                'extension'=>'yourExtension3',
                                'filename'=>'YourFileName3',
                                'childs'=>
                                        [
                                            0=>[
                                                'id'=>5,
                                                'date'=>'3/3/2017',
                                                'owner'=>'ownerName5',
                                                'extension'=>'yourExtension5',
                                                'filename'=>'YourFileName5',
                                                'childs'=>
                                                        [
                                                            0=>[
                                                                'id'=>11,
                                                                'date'=>'3/3/2017',
                                                                'owner'=>'ownerName5',
                                                                'extension'=>'yourExtension5',
                                                                'filename'=>'YourFileName5',
                                                                'childs'=>
                                                                        [
                                                                            0=>[
                                                                                'id'=>13,
                                                                                'date'=>'3/3/2017',
                                                                                'owner'=>'ownerName5',
                                                                                'extension'=>'yourExtension5',
                                                                                'filename'=>'YourFileName5'
                                                                        ],
                                                                             1=>[
                                                                                'id'=>14,
                                                                                'date'=>'3/3/2017',
                                                                                'owner'=>'ownerName6',
                                                                                'extension'=>'yourExtension6',
                                                                                'filename'=>'YourFileName6'
                                                                        ]
                                                                    ]
                                                        ],
                                                             1=>[
                                                                'id'=>12,
                                                                'date'=>'3/3/2017',
                                                                'owner'=>'ownerName6',
                                                                'extension'=>'yourExtension6',
                                                                'filename'=>'YourFileName6'
                                                        ]
                                                    ]
                                        ],
                                             1=>[
                                                'id'=>6,
                                                'date'=>'3/3/2017',
                                                'owner'=>'ownerName6',
                                                'extension'=>'yourExtension6',
                                                'filename'=>'YourFileName6'
                                        ]
                                    ]
                        ],
                             1=>[
                                'id'=>4,
                                'date'=>'3/3/2017',
                                'owner'=>'ownerName4',
                                'extension'=>'yourExtension4',
                                'filename'=>'YourFileName4'
                        ]
                    ]
            ],

         1=>[
                'id'=>2,
                'date'=>'3/3/2017',
                'owner'=>'ownerName2',
                'extension'=>'yourExtension2',
                'filename'=>'YourFileName2',
                'childs'=>
                        [
                            0=>[
                                'id'=>9,
                                'date'=>'3/3/2017',
                                'owner'=>'ownerName7',
                                'extension'=>'yourExtension9',
                                'filename'=>'YourFileName7'
                        ],
                             1=>[
                                'id'=>10,
                                'date'=>'3/3/2017',
                                'owner'=>'ownerName8',
                                'extension'=>'yourExtension10',
                                'filename'=>'YourFileName8'
                        ]
                    ]
            ],


         2=>[
                'id'=>2,
                'date'=>'3/3/2017',
                'owner'=>'ownerName2',
                'extension'=>'yourExtension2',
                'filename'=>'YourFileName2',
                'childs'=>
                        [
                            0=>[
                                'id'=>7,
                                'date'=>'3/3/2017',
                                'owner'=>'ownerName7',
                                'extension'=>'yourExtension7',
                                'filename'=>'YourFileName7'
                        ],
                             1=>[
                                'id'=>8,
                                'date'=>'3/3/2017',
                                'owner'=>'ownerName8',
                                'extension'=>'yourExtension8',
                                'filename'=>'YourFileName8'
                        ]
                    ]
            ]
        ];


 ?>
0

try this code to get sum of all id element.

<?php

$data =[
          0=>[
                'id'=>1,
                'date'=>'3/3/2017',
                'owner'=>'ownerName1',
                'extension'=>'yourExtension1',
                'filename'=>'YourFileName1',
                'childs'=>
                        [
                            0=>[
                                'id'=>3,
                                'date'=>'3/3/2017',
                                'owner'=>'ownerName3',
                                'extension'=>'yourExtension3',
                                'filename'=>'YourFileName3',
                                'childs'=>
                                        [
                                            0=>[
                                                'id'=>5,
                                                'date'=>'3/3/2017',
                                                'owner'=>'ownerName5',
                                                'extension'=>'yourExtension5',
                                                'filename'=>'YourFileName5',
                                                'childs'=>
                                                        [
                                                            0=>[
                                                                'id'=>11,
                                                                'date'=>'3/3/2017',
                                                                'owner'=>'ownerName5',
                                                                'extension'=>'yourExtension5',
                                                                'filename'=>'YourFileName5',
                                                                'childs'=>
                                                                        [
                                                                            0=>[
                                                                                'id'=>13,
                                                                                'date'=>'3/3/2017',
                                                                                'owner'=>'ownerName5',
                                                                                'extension'=>'yourExtension5',
                                                                                'filename'=>'YourFileName5'
                                                                        ],
                                                                             1=>[
                                                                                'id'=>14,
                                                                                'date'=>'3/3/2017',
                                                                                'owner'=>'ownerName6',
                                                                                'extension'=>'yourExtension6',
                                                                                'filename'=>'YourFileName6'
                                                                        ]
                                                                    ]
                                                        ],
                                                             1=>[
                                                                'id'=>12,
                                                                'date'=>'3/3/2017',
                                                                'owner'=>'ownerName6',
                                                                'extension'=>'yourExtension6',
                                                                'filename'=>'YourFileName6'
                                                        ]
                                                    ]
                                        ],
                                             1=>[
                                                'id'=>6,
                                                'date'=>'3/3/2017',
                                                'owner'=>'ownerName6',
                                                'extension'=>'yourExtension6',
                                                'filename'=>'YourFileName6'
                                        ]
                                    ]
                        ],
                             1=>[
                                'id'=>4,
                                'date'=>'3/3/2017',
                                'owner'=>'ownerName4',
                                'extension'=>'yourExtension4',
                                'filename'=>'YourFileName4'
                        ]
                    ]
            ],

         1=>[
                'id'=>2,
                'date'=>'3/3/2017',
                'owner'=>'ownerName2',
                'extension'=>'yourExtension2',
                'filename'=>'YourFileName2',
                'childs'=>
                        [
                            0=>[
                                'id'=>9,
                                'date'=>'3/3/2017',
                                'owner'=>'ownerName7',
                                'extension'=>'yourExtension9',
                                'filename'=>'YourFileName7'
                        ],
                             1=>[
                                'id'=>10,
                                'date'=>'3/3/2017',
                                'owner'=>'ownerName8',
                                'extension'=>'yourExtension10',
                                'filename'=>'YourFileName8'
                        ]
                    ]
            ],


         2=>[
                'id'=>2,
                'date'=>'3/3/2017',
                'owner'=>'ownerName2',
                'extension'=>'yourExtension2',
                'filename'=>'YourFileName2',
                'childs'=>
                        [
                            0=>[
                                'id'=>7,
                                'date'=>'3/3/2017',
                                'owner'=>'ownerName7',
                                'extension'=>'yourExtension7',
                                'filename'=>'YourFileName7'
                        ],
                             1=>[
                                'id'=>8,
                                'date'=>'3/3/2017',
                                'owner'=>'ownerName8',
                                'extension'=>'yourExtension8',
                                'filename'=>'YourFileName8'
                        ]
                    ]
            ]
        ];
echo "<pre>";

//print_r(json_encode($data));

echo "<br/>";

$json_data = json_encode($data);
//preg_match_all('!\d+!', $str, $matches);
preg_match_all('("id":\d+)', $json_data, $matches);
//print_r($matches);

$temp = [];
$temp = $matches[0];

$sum = 0 ;
foreach ($temp as $key => $value) {
    preg_match_all('!\d+!', $value, $matches);
    if (isset($matches[0][0])) {
        $sum = $sum + $matches[0][0];
    }
}

echo "total of all id = ".$sum;


?>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.