Skip to content

Commit

Permalink
Added array_dot helper function to array_helper
Browse files Browse the repository at this point in the history
  • Loading branch information
David McReynolds committed Oct 17, 2014
1 parent c92ca7f commit 1baaab6
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions fuel/modules/fuel/helpers/MY_array_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,5 +232,42 @@ function csv_to_array($filename = '', $delimiter = ',', $header_row = 0, $lengt
return $data;
}

// --------------------------------------------------------------------

/**
* Return the value from an associative array or an object.
* credit: borrowed from Vanilla forums GetValueR function
*
* @access public
* @param string $key The key or property name of the value.
* @param mixed $array The array or object to search.
* @param mixed $default The value to return if the key does not exist.
* @return mixed The value from the array or object.
*/
function array_dot($key, $array, $default = FALSE)
{
$path = explode('.', $key);

$value = $array;
for ($i = 0; $i < count($path); ++$i)
{
$sub_key = $path[$i];

if (is_array($value) AND isset($value[$sub_key]))
{
$value = $value[$sub_key];
}
elseif (is_object($value) AND isset($value->$sub_key))
{
$value = $value->$sub_key;
}
else
{
return $default;
}
}
return $value;
}

/* End of file MY_array_helper.php */
/* Location: ./modules/fuel/helpers/MY_array_helper.php */

0 comments on commit 1baaab6

Please sign in to comment.