PHP Classes

Array Depth: Traverse arrays in depth to perform useful actions

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 177 All time: 8,750 This week: 455Up
Version License PHP version Categories
array_depth 1.0.1Custom (specified...5PHP 5, Data types
Description 

Author

This class can traverse arrays in depth to perform useful actions. Currently it can:

- Determine the depth of a nested array
- Retrieve all data types used in an array
- Determine whether or not an array is homogeneous
- Compute the signature of an array
- Determine whether or not an array is suitable for the process of selection of rows and columns

Innovation Award
PHP Programming Innovation award nominee
February 2018
Number 3
Arrays are a very popular data type in PHP because they make it easy to associate key names or numbers with the respective values.

They also allow to nest information by storing arrays as key values.

This package can inspect nested arrays and perform several types useful operations like checking if all the stored values is of the same type, compute an unique value from all the array values that can be used as a signature of the array, determine if the array can be used as a table with rows and columns

Manuel Lemos
Picture of zinsou A.A.E.Moïse
  Performance   Level  
Name: zinsou A.A.E.Moïse <contact>
Classes: 50 packages by
Country: Benin Benin
Innovation award
Innovation award
Nominee: 23x

Winner: 2x

Example

<?php
require_once('array_depth.php');

echo
'<pre>';
echo
array_depth(array(array(array(array(1,array()))),array(array())));echo'<br>';
echo
array_depth(array(array(array(array(1,array(),array())))));echo'<br>';
echo
array_depth(array(array(array(array(1,45))))); echo'<br>';
echo
array_depth(array(array(),array(array()),array())); echo'<br>';
echo
array_depth(1);echo'<br>';


var_dump(homogeneous(array(/*1,45,'uuu',*/array(array(array(1)))))); echo'<br>';
var_dump(homogeneous(array(array(),array(array()),array()))); echo'<br>';
var_dump(homogeneous(array(1,2,3))); echo'<br>';
var_dump(homogeneous(array(1,2.5,3))); echo'<br>';
var_dump(homogeneous(array(array(1),array(2.5),array(3)))); echo'<br>';


$type=true;
var_dump(homogeneous(array(array(1.5,array(2.5)),array(2.5,array(2.5)),array(3.5,array(2.5))),$type)); echo'<br>';
echo
$type; echo'<br>';

$type=true;
var_dump(homogeneous(array(array(),2),$type)); echo'<br>';
echo
$type; echo'<br>';

var_dump(typesof(array(array(),array(array()),array()))); echo'<br>';

var_dump(signature(array(array(),array(array()),array()))); echo'<br>';

var_dump(rowsandcol($x=array(array(1,'c'=>2,3),array(1,'c'=>2,3),array(1,'c'=>array(),3)),$xiayo)); echo'<br>';

print_r( $xiayo);



?>


Details

This class can help achieving this goal: - return the given array depth (level of interleaving) - return all data types used in a given array - return whether or not the array is homogeneous - return the signature of a given array - return whether or not an array is suitable for rows and columns selection's process. for example when you need to do some recursive foreach on an array without knowing the array ,it may be useful to know the real depth programmatically. it may be useful to know all data types used in an array or even just in the first element of an array as you may see in the homogeneous method implementation. An array must be homogeneous in many applications: for example : to compute a chi square value for a data's cross table, all values must be integer(may be double number but without floating point/comma) so if there is any double value in this array the result will be corrupted. when you will use array_column on an array for some operations ,it is impossible to be sure that all cells exists so you got some incomplete results for example $array=array(array(1,2,3),array(0,2,3),array(4,5,6),array(2,2)); print_r(array_column($array,2)); //return Array ( [0] => 3 [1] => 3 [2] => 6 ) As you may see the last array has just been ignored .Sometimes it is cool but sometimes you don't want that because in a loop you may obtain ugly warning just like offset {number} doesn't exists...It is in this situation that you may need to know whether or not an array is suitable for rows and columns selection's process and the real keys signature. for a how to use example refer to the file test.php report but via the forum or contact me at leizmo@gmail.com.

  Files folder image Files (4)  
File Role Description
Plain text file array_depth.php Class class source
Accessible without login Plain text file license.txt Lic. license file
Accessible without login Plain text file readme.txt Doc. readme
Accessible without login Plain text file test.php Example example script

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:177
This week:0
All time:8,750
This week:455Up