Welcome Guest, Not a member yet? Register   Sign In
Array Helper
#1

[eluser]TheFuzzy0ne[/eluser]
Hi everyone. Here's my array helper, I thought I should post it in case anyone else finds it useful. It just needs to be copied into a file named "array_helper.php", and placed in "application/helpers". It will play nice with the Array helper that already exists.

I was looking for a way to get the first key from an associative array, and figured I should cater for other common array activities. The content has been heavily inspired from the guys who commented on this page: http://www.php.net/array_keys

One advantage of the helper file is that it can help keep your code understandable, maintainable, and self-documenting. Comments and feedback welcome.

Code:
<?php

/**
*    Array First Key
*
*    Returns the first key in the array.
*
*    @param    array
*    @return   string
*/
function array_first_key($array)
{
    return array_shift(array_keys($array));
}

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

/**
*    Array Last Key
*
*    Returns the last key in the array.
*
*    @param    array
*    @return   string
*/
function array_last_key($array)
{
    return array_pop(array_keys($array));
}

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

/**
*    Array First Value
*
*    Returns the first value in the array.
*
*    @param    array        
*    @return   mixed
*/
function array_first_value($array)
{
    return array_shift($array);
}

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

# Alias for the function above.
function array_first_val($array) {
    return array_first_value($array);
}

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

/**
*    Array Last Value
*
*    Returns the last value in the array.
*
*    @param    array
*    @return   mixed
*/
function array_last_value($array)
{
    return array_pop($array);
}

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

# Alias for the function above.
function array_last_val($array)
{
    return array_last_value($array);
}

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


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

/**
*    Array Value Intersect Keys
*
*    Returns the keys of elements in the haystack that match the needle(s)
*
*    @param    array
*    @param    array
*    @return   array
*/
function array_value_intersect_keys($haystack, $needles){
    $intersected = array_intersect($haystack, $needles);
    return array_keys($intersected);
}

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

/**
*    Array Remove Assoc Key
*
*    Removes the part of the array that matches the specified key.
*
*    @param    array
*    @param    string
*    @return   array
*/
function array_remove_assoc_key($array=array(), $key='')
{
    if(array_key_exists($key, $array)) {
        unset($array[$key]);
    }
    return $array;
}

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

/**
*    Array Remove Assoc Keys
*
*    Removes the parts of the array that matches the specified keys.
*
*    @param    array
*    @param    array
*    @return   array
*/
function array_remove_assoc_keys($array=array(), $keys=array())
{
    foreach($keys as $key) {
        $array = array_remove_assoc_key($array, $key);
    }        
    return $array;
}

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

/**
*    Array key
*
*    Extracts the key of an associative array from the position you specify.
*
*    @param    array
*    @param    int
*    @return   string
*/
function array_key($arr, $pos) {
    if (!empty($arr)) {
        if ($pos===null)
            $pos = 0;
      
        $all_keys = array_keys($arr);
        unset($arr);
      
        $key = $all_keys[$pos];
        unset($all_keys);
      
        if (isset($key))
            return $key;
        else {
            unset($key);
            return null;
        }
    }
}
#2

[eluser]xwero[/eluser]
I would remove the array_first_value and the array_last_value functions as they only make your application slower.
#3

[eluser]Yash[/eluser]
I can try it. Looks nice to me.
#4

[eluser]xwero[/eluser]
Your helper inspired this post
#5

[eluser]TheFuzzy0ne[/eluser]
Any idea why we can't pass parameters in by reference? I'm not sure if this is a bug or not, I can't imagine why it shouldn't work as expected.
#6

[eluser]xwero[/eluser]
You can, the problem was that i used this test code
Code:
$test = 'array_shift';
$test(array(1,2));
I should have known it doesn't work as
Code:
array_shift(array(1,2));
raises the error
Quote:Fatal error: Only variables can be passed by reference
I will correct it on my blog later, thank you for pointing out my stupidity Wink
#7

[eluser]TheFuzzy0ne[/eluser]
Whoops. I thought it was strange that it wouldn't work, so I tested it with a string, but I made exactly the same mistake... Only I got this message:

Quote:Fatal error: Cannot pass parameter 1 by reference

So it wasn't quite so obvious to me either. Well done for spotting it! Smile




Theme © iAndrew 2016 - Forum software by © MyBB