Page 1 of 1
Increase Array Dimension Dynamically
Posted: Tue May 29, 2007 1:43 am
by keenlearner
Is there a way that we can increase the array dimension dynamically (during the runtime) ?
like
Code: Select all
$a = array();
// May be using for loop
for($i=0; $i < 5; $i++)
$a[0];
$a[0][0];
$a[0][0][0];
$a[0][0][0][0][0];
But not sure how can I accomplished that, or maybe is there other way which has the same concept as this dynamic array dimension resizing ? Thanks
Posted: Tue May 29, 2007 1:47 am
by Chris Corbyn
Yes, just declare array() again for each dimension:
Code: Select all
$a = array();
$a[0] = array();
$a[0][0] = array();
$a[0][0][0] = array();
$a[0][0][0][0] = array();
$a[0][0][0][0][0] = array();
Posted: Tue May 29, 2007 3:02 am
by keenlearner
Thanks for the reply, but during the runtime how can I add another square bracket with the index zero to the array variable ?
Let say from one dimension to two dimension
The thing is when I am writing PHP program, I can declare array with any dimension size I want, but it's not this. The array should resize itself DURING the PROGRAM EXECUTION DYNAMICALLY.
Such as this is the pseudo code
if (I_WANT_5_DIMENSION_ARRAY)
Make array of 5 dimension;
else if (I_WANT_6_DIMENSION_ARRAY)
Make array of 6 dimension
and so on.
Thanks
Posted: Tue May 29, 2007 3:07 am
by Oren
Hmm... Just like
d11wtq showed you?

Posted: Tue May 29, 2007 3:32 am
by keenlearner
Still not solved, lets make it a little simpler,
I have array of
Can you make a code where it will increase one more dimension of array $a[0] to $a[0][0] ? Thanks...
Posted: Tue May 29, 2007 3:39 am
by djot
Posted: Tue May 29, 2007 4:02 am
by Chris Corbyn
Just think about it. When you declared $a to be an array, how did you do it?
Right?
So if $a[0] is just a variable like $a, how would you declare that to be an array (aka, the second dimension)?
Just the same right?
That's all you do. Just keep declaring the array elements to be arrays themselves.
Posted: Tue May 29, 2007 4:41 am
by keenlearner
Thanks for helping me, I know how to declare multi dimension array but, I want it to be declared during RUNTIME (when the program is executing, not when I am typing the PHP code). I found the solution that someone gave me and it's exactly what I want as below.
Code: Select all
<?php
$depth = 5;
eval('$array = '.str_repeat('array(', $depth).str_repeat(')', $depth).';');
print_r($array);
?>
Output
Code: Select all
Array
(
[0] => Array
(
[0] => Array
(
[0] => Array
(
[0] => Array
(
)
)
)
)
)
Posted: Tue May 29, 2007 5:00 am
by onion2k
That's some pretty unreadable code.
Posted: Tue May 29, 2007 5:43 am
by stereofrog
Looks like you need something like
Code: Select all
function make_n_ary($depth) {
return ($depth < 2) ?
array() :
array(make_n_ary($depth - 1));
}
$a = make_n_ary(10);
print_r($a);
I'm very curious why someone would need this.
Posted: Tue May 29, 2007 7:02 am
by keenlearner
I am curious too

, I am so blur today, I forgot that there is not a need to increase the array dimension, because the array dimension will be automatically increased when I insert array into the array. It takes a bit of writing but here is exactly what I am trying to do , maybe you all can help me to improve, I am trying to make an algebra calculator , such as if I had an equation
it should give the result of "r" as 9, I have done this by inputing each character into $segment array in this way;
Code: Select all
$segment = array();
for($i=0; $i < strlen($str); $i++)
$segment[$i] = $str[$i];
Then I moved all the numbers at the left hand side to the right hand side which is after the equal operator and also change the number sign from positive to negative and vice versa. The number that has been moved to the right hand side is stored by appending into the $segment array.
The equation will look like this now which is stored in the $segment array.
4 + r - 5 = 8 - 4 + 5;
Code: Select all
$rhs = '';
for($j=$equalSignIndex+1; $i< sizeof($segment); $i++){
$rhs =. $segment[$i]; // Get the right hand side numbers which is 8- 4 + 5
}
return eval("return $rhs;"); // gives the result of 9
The above one might have better way of doing it. But what I am having a problem which is the parentheses, such as below
((2 + 1) * t ) + 5 = 8;
I was thinking to store the character in parentheses into $segment array in multi dimension like
Code: Select all
$segment[0] = '((2 + 1) * t )';
$segment[0][0] = '(2 + 1)';
$segment[0][1] = '*';
$segment[0][2] = 't';
$segment[1] = '+' // We have come out from parentheses so we want to store in new index
$segment[2] = '5';
$segment[3] = '=';
$segment[4] = '8';
The use of multi dimension array for me is to let me be able to keep track of the position inside the parentheses.So that I know the very first thing to do is to evaluate those from the innermost parentheses which has the biggest array dimension such as $segment[0][0] which gives result of 3 and stored it again in $segment[0][0]. And the rest of evaluation will goes slightly different from the previous equation to get the result of t. My explanation might be hard to be understood and also might have silly way of doing it, but hope you can understand and help to improve, thank you.
Posted: Tue May 29, 2007 8:05 am
by jayshields
keenlearner wrote:
Code: Select all
$segment[0] = '((2 + 1) * t )';
$segment[0][0] = '(2 + 1)';
$segment[0][1] = '*';
$segment[0][2] = 't';
$segment[1] = '+' // We have come out from parentheses so we want to store in new index
$segment[2] = '5';
$segment[3] = '=';
$segment[4] = '8';
You can't do that. You are assigning a string AND an array to $segment[0]. You could achieve something similar to this with an associative array. I don't know if you are even taking the right approach to making this application.