donut/array_dig

recursively fetch nested array keys

0.1.1 2014-05-28 17:10 UTC

README

recursively fetch nested array keys, deeply, safely, most definitely.

API

array_dig( mixed $arr, mixed $key1[, mixed $key2[, ..., mixed $key3]] )

Specify one or more keys to recursively dig in an array.

<?php

$arr = array(
  "a" => array(
    "b" => "c"
  )
);

array_dig($arr, "a", "b"); // "c"

array_dig( mixed $arr, Array $keys )

Specify an array of keys to recursively dig in an array

<?php

$arr = array(
  "a" => array(
    "b" => "c"
  )
);

$dig = array("a", "b")

array_dig($arr, $dig); // "c"

Installation

array_dig lives on packagist.org

$ composer require donut/array_dig

Usage

<?php require "vendor/autoload.php";

\Donut\Util\array_dig($arr, $keys);

An added benefit of using array_dig is that you can immediately access arrays returned by functions.

<?php

function donut() {
  return array("maple", "jelly", "glazed");
}

// PHP <= 5.3
donut()[1];

// Parse error: syntax error, unexpected '['

array_dig to the rescue

<?php

array_dig(donut(), 1); // "jelly"

Attribution

License

BSD 3-clause