PHP Classes

File: examples/ex1.php

Recommend this page to a friend!
  Classes of Jorge Castro   PHP Array One   examples/ex1.php   Download  
File: examples/ex1.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP Array One
Perform several types of operations on arrays
Author: By
Last change:
Date: 1 year ago
Size: 1,072 bytes
 

Contents

Class file image Download
<?php

use eftec\ArrayOne;

include
__DIR__.'/../vendor/autoload.php';

$array=[1,2,3
   
,'products'=>[
        [
'name'=>'cocacola1','price'=>500,'quantity'=>200],
        [
'name'=>'cocacola2','price'=>600,'quantity'=>300],
        [
'name'=>'cocacola3','price'=>700,'quantity'=>400],
    ]
];

$r=ArrayOne::set($array)
    ->
nav('products')
    ->
setCol('subtotal',function($col,$index) { return $col['price']*$col['quantity'];})
    ->
filter(function($col,$index){ return $col['price']>=600; })
    ->
removeCol(['price'])
    ->
col('name')
    ->
last()
    ->
current();

var_dump($r);
var_dump(ArrayOne::$error);

$r=ArrayOne::set($array)
    ->
nav('products')
    ->
reduce(function($row,$index,$previous) {
        return [
'price'=>$previous['price']+$row['price'],
           
'quantity'=>$previous['quantity']+$row['quantity'],'counter'=>@$previous['counter']+1];
    })
    ->
all();
var_dump('--------');
var_dump($r);

$r=ArrayOne::set($array)
    ->
nav('products')
    ->
reduce(['price'=>'sum','quantity'=>'sum'])
    ->
all();
var_dump('--------');
var_dump($r);