Re: How to use a variable variable with an array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



maybe something like this if you are willing to get rid of your weird array
dimension delimiters (you can use a dot for that for example, see below)

$a = array(
    "a" => array(
        "b" => array(
            "c" => array(
                "d" => array(
                    "e" => array(
                        "f" => 1
                    )
                )
            )
        )
    )
);

function coolFunction( array $array, $search, $separator = '.' )
{
    if ( empty( $array ) ) {
        return false;
    }

    if ( strlen( $search ) === 0 ) {
        return false;
    }

    $dimensions = explode( $separator, $search, 2 );
    $dimension = array_shift( $dimensions );

    if ( isset( $array[$dimension] ) ) {
        // no more dimensions to search
        if ( empty( $dimensions ) ) {
            return $array[$dimension];
        }

        // more dimensions to search but we can only go further if
        // the current dimension value is also an array
        if ( is_array( $array[$dimension] ) ) {
            return coolFunction( $array[$dimension], $dimensions[0] );
        } else {
            return false;
        }
    } else {
        return false;
    }
}

var_dump( coolFunction( $a, "a.b" ) );
var_dump( coolFunction( $a, "a.b.d.e.c" ) );
var_dump( coolFunction( $a, "a.b.c.d.e.f" ) );
var_dump( coolFunction( $a, "a.b.c.d.e.f.g" ) );

On Wed, Dec 14, 2011 at 3:27 AM, Nils Leideck <nils.leideck@xxxxxxxxxx>wrote:

> Hi Al,
>
> many thanks for your feedback. Unfortunately I don’t know the deepness of
> the array so I can’t use the nested foreach() idea :-( Let me try to
> simplify my question (which also helps myself to clarify my process ;-)
>
> I have a variable where the value is a string that is exactly the path to
> the array value:
>
> [code]
>    $arrayPath =
> "['user_interface’][‘design']['my_colors']['item_number_one’]”;
> [/code]
>
> And I have an array that has all the details:
>
> [code]
>    $myArray = coolFunction(‘getArray’);
> [/code]
>
> The question is, how can I use these both informations to return a value
> from the $myArray?
>
> Examples I tried unsuccessfully:
>
> [code]
>    [...]
>    echo $myArray$arrayPath;
>    echo $myArray{$arrayPath};
>    echo $myArray${arrayPath};
>    echo $myArray${$arrayPath};
>    echo $myArray.$arrayPath;
>    echo $myArray.$arrayPath;
>    [...]
> [/code]
>    etc...
>
> your feedback is much much much appreciated!!!
>
> Cheers, Nils
> --
> http://webint.cryptonode.de / a Fractal project
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux