File: src/Helpers/deep_value.php

Recommend this page to a friend!
  Classes of AccountKiller  >  Fuse  >  src/Helpers/deep_value.php  >  Download  
File: src/Helpers/deep_value.php
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: Fuse
Fuzzy search of arrays using the Bitap algorithm
Author: By
Last change: Re-establish support for PHP 5.6
Date: 2 years ago
Size: 1,195 bytes
 

Contents

Class file image Download
<?php namespace Fuse\Helpers;

function
deep_value($assoc, $path, &$list = [])
{
    if (!
$path) {
       
// If there's no path left, we've gotten to the object we care about.
       
$list[] = $assoc;
    } else {
       
$dotIndex = mb_strpos($path, '.');
       
$firstSegment = $path;
       
$remaining = null;

        if (
$dotIndex !== false) {
           
$firstSegment = mb_substr($path, 0, $dotIndex);
           
$remaining = mb_substr($path, $dotIndex + 1);
        }

       
$value = isset($assoc[$firstSegment])
            ?
$assoc[$firstSegment]
            :
null;

        if (!
is_null($value)) {
            if (!
$remaining && (is_string($value) || is_int($value) || is_float($value))) {
               
$list[] = (string) $value;
            } elseif (
is_list($value)) {
               
// Search each item in the array.
               
for ($i = 0, $len = sizeof($value); $i < $len; $i++) {
                   
deep_value($value[$i], $remaining, $list);
                }
            } elseif (
$remaining) {
               
// An associative array. Recurse further.
               
deep_value($value, $remaining, $list);
            }
        }
    }

    return
$list;
}


For more information send a message to info at phpclasses dot org.