Jump to content

Recommended Posts

Hi

I need help with dynamically building dimensions of array. $arr can have 1 to n dimensions and value of each element can be anything.

Something like this:

$result = array();

// $arr can have dimensions 1 to n. Each entry is dimensions label
$arr = array("dim1.1","dim2.1","dim3.1");

$strDim = "['" . implode("']['", $arr) . "']";
eval('$result' . $strDim . " = 10;");

print_r($result);

 

Array

(

    [dim1.1] => Array

        (

            [dim2.1] => Array

                (

                    [dim3.1] => 10

                )

 

        )

 

)

 

This solution with eval is working but, i rather avoid it. Only other thing i can think of is recursive function call.

Link to comment
https://forums.phpfreaks.com/topic/151290-array/
Share on other sites

I'm not sure what you're trying to accomplish, you just want a certain depth array with the final element a non-array, and all of the other arrays wrapping the final element? I could give you a recursive solution that results in the same array being built, but I'm not sure it would accomplish your goal. Could you explain what you are trying to accomplish?

 

For instance, this results in the same array, but isn't what you want, is it?

 

<?php
function buildArray($dimensions, &$root = array()) {
$curDimension = array_shift($dimensions);

if(!empty($dimensions)) {
	$root[$curDimension] = array();
	$root[$curDimension] = buildArray($dimensions, $root[$curDimension]);
}
else {
	$root = $curDimension;
}

return $root;
}


// $arr can have dimensions 1 to n. Each entry is dimensions label
$dimensions = array('dim1.1', 'dim2.1', 'dim3.1', '10');
$result = buildArray($dimensions);
print_r($result);
?>

Link to comment
https://forums.phpfreaks.com/topic/151290-array/#findComment-794788
Share on other sites

The whole idea was to add entry to array, but dimensions, where to add, is in another array (don't ask why  :) )

I've came up with a similar solution with recursive array. Can anyone think of simpler solution?

I was trying with http://php.net/language.variables.variable, but unsuccessfully.

Link to comment
https://forums.phpfreaks.com/topic/151290-array/#findComment-794949
Share on other sites

I'm almost certain there's a better solution for your data structure than a thing like this. If you would explain what you're trying to accomplish we could probably help. If you're going to use eval() like that, at least encapsulate it in a function you can hide deep in your source tree in a file full of comments and apologies. :)

Link to comment
https://forums.phpfreaks.com/topic/151290-array/#findComment-795478
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.