0

I'm currently trying to make a loop that creates arrays, but I need to set the name of the array dynamic, so that all arrays have a name like $array0, $array1.

I am fetching rows from a MySQL table that all have an ID, that ID is stored in $rowData[0], so I want make arrays that have a name with this ID.

I currently have this:

if(mysql_num_rows($result)!=0) {
while($rowData = mysql_fetch_array($result)) {
    echo '<pre>';-
    $array . $rowData[0] = new ArrayObject($rowData);
    print_r($array . $rowData[0]);
    echo "<h1>" . $array . $rowData[0] . [1] . "<h1>";
    echo '</pre>';
  }
}


Thanks in advance.

1
  • Just an FYI: mysql_ functions are deprecated and have been removed in PHP 7. Use mysqli_or PDO functions instead. Commented Feb 19, 2017 at 18:29

1 Answer 1

1

That negates the value of using arrays, just do:

$array[$rowData[0]] = new ArrayObject($rowData);

Then you access $array[1] etc...

What you are attempting is variable variables, but rarely if ever are they better than using an array. If $rowData[0] is 1 then this would create $array1:

${'array' . $rowData[0]} = new ArrayObject($rowData);

NOTE for mysql_*() functions:

This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:

  • mysqli_query()
  • PDO::query()
Sign up to request clarification or add additional context in comments.

Comments

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.