ryan
ryan

Reputation: 3

Add value to the end of a multidimensional array

I am trying add value to a mutlidimensional array but I am slightly confused as how to achieve this.

I am not sure quite how to explain what I want so I will demonstrate it below:

<?php 
     $value = 'text';
     $array = array();

I want the length of the array to be based upon the value of $int e.g.

     $int = 3;
     $array[][][] = $value;

     $int = 4;
     $array[][][][] = $value;
?>

Is this possible??

Thanks

Upvotes: 0

Views: 903

Answers (1)

Victor Nicollet
Victor Nicollet

Reputation: 24577

There's no such thing as the "end" of a recursive array. Right now, what your algorithm looks like it's trying to do is creating a new cell in a new row in a new column in a etc. which is a fairly unusual operation. Is this what you really intended?

Anyway, you can do the following:

$int = 4;
while ($int-- > 1) $value = array($value);
$array[] = $value;

Upvotes: 3

Related Questions