0

So my end result will be this (the end result will have 48 entries):

$theArray=array(
$theArray1,
$theArray2,
$theArray3,
$theArray4,
$theArray5,
$theArray6
);

I have tried a few things but I think this is the closest, but I'm still not there yet, any help appreciated.

$i = 0;
while ($i <= 48){
  $theArray[]=${"theArray".$i.","}
  $i++
 }
$theArray[]=${"theArray".$i};
5
  • 3
    Uh, what is the question? Commented May 17, 2016 at 9:23
  • 1
    Why don't you have $theArray1, $theArray2, etc defined as array values in the first place, so no numbers or hacky conversions necessary? Where are these values coming from? Commented May 17, 2016 at 9:23
  • Are you looking for this, Is your desire output like: https://3v4l.org/5khJe Commented May 17, 2016 at 9:44
  • Yes, but rather the entries in the arrays rather than a string Commented May 17, 2016 at 11:02
  • Perhaps I should add $theArray1, $theArray2.. etc are arrays Commented May 17, 2016 at 11:24

3 Answers 3

2

You missed ; at the end of line:

$i = 0;
while ($i <= 48){
  $theArray[]=${"theArray".$i.","}; // missed ; here
  $i++; // missed ; here
 }
$theArray[]=${"theArray".$i};
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry that was in haste, but it still doesn't work how I intended it.
0

You have to use the function compact for create an array containing variables. Try to read the documentation http://php.net/manual/en/function.compact.php

And here are some example http://www.w3schools.com/php/func_array_compact.asp

1 Comment

Yes perhaps this is the answer, but I'm struggling to incorporate it into a loop, I'll keep trying though.
0

Keep it simple as below:

<?php
$i = 0;
$theArray = array();
while ($i <= 48){
  array_push($theArray, '$theArray'.$i);
  $i++;
 }
echo '$arr = array('.implode(',', $theArray).');';
?>

Just run this on your end. Cheers!

1 Comment

Thanks I think this is nearly it, but rather than it output each array as a string I need it to be the actual array. The above outputs the following: Array ( [0] => $theArray0 [1] => $theArray1 [2] => $theArray2 [3] => $theArray3 ) rather than the values within $theArray0 etc..

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.