开发者

php string name as variable in array

开发者 https://www.devze.com 2023-02-04 02:42 出处:网络
how take string from array define as new array, how to code in php $column = array(\"id\",\"name\",\"value\");

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开发者_StackOverflow 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.


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;
  }
}


I can't see why you'd want to model your data like this, you're asking for a world of hurt in terms of debugging. There are "variable variables" you could use to define this, or build global variables dynamically using $GLOBALS:

$somevar = "hello"
$$somevar[0] = "first index";  // creates $hello[0]

$GLOBALS[$somevar][0] = "first index"; // creates $hello[0] in global scope


try

$array = array();
foreach ($results as $r){
   foreach ($column as $col ){
       $array[$col][] = $r[$col];
   }
}

extract ($array);

or you can simply do this

$id = array();
$name = array();
$value = array();

foreach ( $results as $r ){
    $id[] = $r['id']; // or $r[0];
    $name[] = $r['name'];
    $value[] = $r['value'];
}

Hope this is what you asked


This is pretty dangerous, as it may overwrite variables you consider safe in your code. What you're looking for is extract:

$result = array();

while ($row = mysql_fetch_array($result)) 
  foreach ($column as $i => $c) 
    $result[$c][] = $row[$i];

extract($result);

So, if $result was array( 'a' => array(1,2), 'b' => array(3,4)), the last line defines variables $a = array(1,2) and $b = array(3,4).


You cannot use variable variables right on for this, and you shouldn't anyway. But this is how you could do it:

foreach (mysql_fetch_something() as $row) {
    foreach ($row as $key=>$value) {
        $results[$key][] = $value;
    }
}

extract($results);

Ideally you would skip the extract, and use $results['id'][1] etc. But if you only extract() the nested array in subfunctions, then the local variable scope pollution is acceptable.


There is no need for arrays or using $_GLOBALS, i believe the best way to create variables named based on another variable value is using curly brackets:

$variable_name="value";

${$variable_name}="3";

echo $value;

//Outputs 3

If you are more specific on what is the array you receive i can give a more complete solution, although i must warn you that i have never had to use such method and it's probably a sign of a bad idea.

If you want to learn more about this, here is a useful link: http://www.reddit.com/r/programming/comments/dst56/today_i_learned_about_php_variable_variables/

0

精彩评论

暂无评论...
验证码 换一张
取 消