Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to: Not use eval to create array? 1

Status
Not open for further replies.

thenewa2x

Programmer
Dec 10, 2002
349
US
Here's my problem: I dont want to use eval. The script works and you can view it on a web-page if you need to.

Code:
<?php

    $array = array(
    
      'LEVEL_1' => array(
    
        'LEVEL_2_1' => array(),
        'LEVEL_2_2' => array(),
        'LEVEL_2_3' => array(),
    
      ),
    
      'LEVEL_2' => array(
    
        'LEVEL_2_1' => array(),
        'LEVEL_2_2' => array(),
        'LEVEL_2_3' => array(),
    
      ),
    
      'LEVEL_3' => array(
    
        'LEVEL_3_1' => array(),
        'LEVEL_3_2' => array(),
        'LEVEL_3_3' => array(),
    
      ),
    
    );
    
    $shortcut =& get_array('/LEVEL_2/LEVEL_2_2/', &$array);
    
    $shortcut['TEST'] = 'Value 2';
    
    print('<pre>');
    print_r($array);
    
    function get_array ( $path, &$array )
    {
      $path      = explode('/', $path);
    
      array_shift($path);
      array_pop($path);
    
      
      $real_path = eval("return(\$array['" . join("']['", $path) . "']);");
    
      return( $real_path );
    }

?>

---------------------------------------
 
How about using the following instead of your eval:
Code:
$real_path = $array[$path[0]][$path[1]];

It worked when I tried it.

Ken
 
Thanks but it works half-way. What if the path is longer?

[tt]/path/to/some/indice/in/the/array/[/tt]

or

[tt]/some/really/long/path/to/the/indice/in/the/array/[/tt]

---------------------------------------
 
On my PHP5 machine, the following code:
Code:
<?php

$array = array(

	'LEVEL_1' => array(

		'LEVEL_2_1' => array(),
		'LEVEL_2_2' => array(),
		'LEVEL_2_3' => array(),

  	),

	'LEVEL_2' => array(

		'LEVEL_2_1' => array(),
		'LEVEL_2_2' => array(),
		'LEVEL_2_3' => array(
			'LEVEL_2_3_1' => array ()
		),

	),

	'LEVEL_3' => array(

		'LEVEL_3_1' => array(),
		'LEVEL_3_2' => array(),
		'LEVEL_3_3' => array(),

	),

);

print '<html><body><pre>' ;

$shortcut =& get_array('/LEVEL_2/LEVEL_2_2/', $array);

$shortcut['TEST1'] = 'Value 1';

$shortcut =& get_array('/LEVEL_2/LEVEL_2_3/LEVEL_2_3_1', $array);
$shortcut['TEST2'] = 'Value 2';

print_r ($array);
print '</pre></body></html>';

function &get_array ( $path, &$array )
{
	$path = explode('/', $path);

	$current = &$array;
	foreach ($path as $path_element)
	{
		if ($path_element != '')
		{
			$current = &$current[$path_element];
		}
	}
      
	return ($current);

}

?>

Returns:

Code:
Array
(
    [LEVEL_1] => Array
        (
            [LEVEL_2_1] => Array
                (
                )

            [LEVEL_2_2] => Array
                (
                )

            [LEVEL_2_3] => Array
                (
                )

        )

    [LEVEL_2] => Array
        (
            [LEVEL_2_1] => Array
                (
                )

            [LEVEL_2_2] => Array
                (
                    [TEST1] => Value 1
                )

            [LEVEL_2_3] => Array
                (
                    [LEVEL_2_3_1] => Array
                        (
                            [TEST2] => Value 2
                        )

                )

        )

    [LEVEL_3] => Array
        (
            [LEVEL_3_1] => Array
                (
                )

            [LEVEL_3_2] => Array
                (
                )

            [LEVEL_3_3] => Array
                (
                )

        )

)

Which is what I think you want.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
It works! My question then becomes, why? If it can find the key in the first level does it recurse until it find a match or the end of the array?

Thanks anyway. Two bad I can't give you two stars.

---------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top