プロが教える店舗&オフィスのセキュリティ対策術

・Array
(
  [0] => Array
    (
      [test1] => 11
      [test2] => 22
      [test3] => 33
      [test4] => 44
    )

)

それぞれのフィールドを変数として代入したいのです。

$test1=11
$test2=22
$test3=33
$test4=44

ご教示頂ければ幸いです。よろしくお願い致します。

A 回答 (1件)

$arr = array(


  'test1'=>11
 ,'test2'=>22
 ,'test3'=>33
);

foreach($arr as $key => $value)
{
 ${$key} = $value;
}

echo $test1;
echo $test2;
echo $test3;

不具合の元になりやすいので普通はやりませんけどね。

この回答への補足

すみません。解決しました。
どうもありがとうございました。

while($row = pg_fetch_assoc($res) )
{
$data[] = $row;
foreach($row as $key => $value)
{
${$key} = $value;
}
}

補足日時:2007/11/26 13:47
    • good
    • 0
この回答へのお礼

これは、いつもご回答感謝いたします。

while($row = pg_fetch_assoc($res) )
{
$data[] = $row;
}

以前、ご教示頂いたDBから、SELECT文で取得した各フィールドの値を変数入力したいと思っているのです。

$arr = array(
  'test1'=>11
 ,'test2'=>22
 ,'test3'=>33
);

SELECT文で抜き出した結果が記載したArrayとなるので、値は可変なのです。なにか良い方法はないでしょうか。

お礼日時:2007/11/22 18:38

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!