Increase Array Dimension Dynamically

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
keenlearner
Forum Commoner
Posts: 50
Joined: Sun Dec 03, 2006 7:19 am

Increase Array Dimension Dynamically

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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();
keenlearner
Forum Commoner
Posts: 50
Joined: Sun Dec 03, 2006 7:19 am

Post 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

Code: Select all

$a[0]; 
$a[0][0];
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
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Hmm... Just like d11wtq showed you? :?
keenlearner
Forum Commoner
Posts: 50
Joined: Sun Dec 03, 2006 7:19 am

Post by keenlearner »

Still not solved, lets make it a little simpler,
I have array of

Code: Select all

$a[0] = array();
Can you make a code where it will increase one more dimension of array $a[0] to $a[0][0] ? Thanks...
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

all said ...

Code: Select all

$a[0][0] = ARRAY();
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Just think about it. When you declared $a to be an array, how did you do it?

Code: Select all

$a = array();
Right? :)

So if $a[0] is just a variable like $a, how would you declare that to be an array (aka, the second dimension)?

Code: Select all

$a[0] = array();
Just the same right? :)

That's all you do. Just keep declaring the array elements to be arrays themselves.
keenlearner
Forum Commoner
Posts: 50
Joined: Sun Dec 03, 2006 7:19 am

Post 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
                                (
                                )
                        )
                )
        )
)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

That's some pretty unreadable code.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post 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.
keenlearner
Forum Commoner
Posts: 50
Joined: Sun Dec 03, 2006 7:19 am

Post by keenlearner »

I am curious too :D , 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

Code: Select all

$str = '4 + r - 5 = 8';
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.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post 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.
Post Reply