0

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

1 Answer 1

3

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks victor, I figured out another way of doing what I wanted to do as original idea was far too complicated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.