Search code examples
phparraysstringvariable-variables

php string name as variable in array


how take string from array define as new array, how to code in php

$column = array("id","name","value");

let say found 3 row from mysql

want result to be like this

$id[0] = "1";
$id[1] = "6";
$id[2] = "10";

$name[0] = "a";
$name[1] = "b";
$name[2] = "c";

$value[0] = "bat";
$value[1] = "rat";
$value[2] = "cat";

I want to take string from $column array define as new array.

how to code it? or if my question is stupid , my please to have suggestion.

thank you.


Solution

  • Answer I made on your previous question:

    $result = mysql_query($sql);
    $num = mysql_num_rows($result);
    $i = 0;
    
    if ($num > 0) {
      while ($row = mysql_fetch_assoc($result)) {
        foreach($row as $column_name => $column_value) {
          $temp_array[$column_name][$i] = $column_value;
        }
        $i++;
      }
    
      foreach ($temp_array as $name => $answer) {
        $$name = $answer;
      }
    }