PHP Classes

File: examples/kernel/core/tcollection/tcollection.sum_avg.php

Recommend this page to a friend!
  Packages of Christos Drogidis   Ascoos OS   examples/kernel/core/tcollection/tcollection.sum_avg.php   Download  
File: examples/kernel/core/tcollection/tcollection.sum_avg.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Ascoos OS
A PHP Web 5.0 Kernel for decentralized web and IoT
Author: By
Last change:
Date: 2 months ago
Size: 4,639 bytes
 

Contents

Class file image Download
<?php
/*
dobu {
    file:id(`example-00000095`) {
        ascoos {
            logo {`
                  __ _ ___ ___ ___ ___ ___ ___ ___
                 / _` |/ / / __/ _ \ / _ \ / / / _ \ / /
                | (_| |\ \| (_| (_) | (_) |\ \ | (_) |\ \
                 \__,_|/__/ \___\___/ \___/ /__/ \___/ /__/
            `},
            name {`ASCOOS OS`},
            version {`1.0.0`}
        },
        example {
            class {`TCollection`},
            methods {`sumBy(), avgBy(), add(), Free()`},
            source {`examples/kernel/core/tcollection/tcollection.sum_avg.php`},
            category:langs {
                en {`Objects Collection`},
                el {`??????? ????????????`}
            },
            summary:langs {
                en {`Demonstrates summing and averaging numeric properties`},
                el {`??????????? ???????? ??? ???? ??? ??????????? ?????????`}
            },
            desc:langs {
                en {`This example demonstrates how to use the sumBy() and avgBy() methods of TCollection to aggregate numeric values from a list of objects.
                    Non?numeric or null values are automatically ignored, ensuring accurate calculations.
                    The example shows how to populate a collection, compute the total and average of a specific property, and finally release memory resources.`},
                el {`?? ?????????? ???? ??????????? ?? ????? ??? ??????? sumBy() ??? avgBy() ??? TCollection ??? ?? ??????????? ??????????? ?????
                    ??? ??? ????? ????????????.
                    ?? ??????????? ? null ????? ?????????? ????????, ?????????????? ??????? ????????????.
                    ?? ?????????? ??????? ??? ????????????? ??? ???????, ??? ???????????? ?? ???????? ??? ? ????? ???? ???? ????????????? ?????????
                    ??? ????? ??? ??????? ? ?????????? ??? ??????.`}
            },
            author {`Drogidis Christos`},
            sincePHP {`8.4.0`}
        }
    }
}
*/
declare(strict_types=1);

use
Ascoos\OS\Kernel\Core\Collections\TCollection;

$startTime = microtime(true);
$startMem = memory_get_usage();

// ????????????????????????????????????????????????
// <EN> Creating a collection with numerical data
// <EL> ?????????? ???????? ?? ?????????? ????????
// ????????????????????????????????????????????????
$collection = new TCollection();

$obj1 = new stdClass(); $obj1->score = 85;
$obj2 = new stdClass(); $obj2->score = 92;
$obj3 = new stdClass(); $obj3->score = 78;
$obj4 = new stdClass(); $obj4->score = null; // non-numeric ? ignored

$collection->add($obj1);
$collection->add($obj2);
$collection->add($obj3);
$collection->add($obj4);

// ????????????????????????????????????????????????
// <EN> Sum and average score
// <EL> ???????? ??? ????? ???? ????
// ????????????????????????????????????????????????
$totalScore = $collection->sumBy('score');
$averageScore = $collection->avgBy('score');

echo
"<h1>TCollection::sumBy() and avgBy()</h1>";
echo
"<pre>";
echo
"Score total : {$totalScore}\n";
echo
"Average score : " . ($averageScore !== null ? number_format($averageScore, 2) : 'No valid') . "\n";
echo
"</pre>";

// ????????????????????????????????????????????????
// <EN> Memory cleaning
// <EL> ?????????? ??????
// ????????????????????????????????????????????????
$collection->Free();

echo
"Example completed.\n";

// ==================== STATISTICS ====================

print_stats($startTime, $startMem);
?>