我需要一些foreach循環的幫助。說我有這種方法:php foreach循環,無法將值分配給適當的變量名稱?
public function showPaymentDetails() {
$data = array("name" => $this->name,
"lastname" => $this->lastname,
"email" => $this->email,
"company" => $this->company,
"event_name" => $this->event_name,
"event_price" => $this->event_price,
"uniqid" => $this->uniqid,
"mobile" => $this->mobile,
"travel" => $this->travel,
"job" => $this->job,
"how_hear" => $this->how_hear,
"geusts" => $this->guests,
"slider_network" => $this->slider_network,
"slider_invest" => $this->slider_invest,
"slider_investors" => $this->slider_investors,
"gender" => $this->gender
);
return $data;
}
現在我想分配一個數組的每個值到一個變量。這是我做的:
$i = 0;
foreach($data as $row[$i]) {
$row[$i];
$i++;
}
echo $row[0];
echo $row[1];
echo $row[2];
正如你所看到的,我將值賦給$row[]
。這真的很糟糕。如何將這些值分配給$name
,$lastname
等變量名稱。
謝謝!
也許你應該說明你想要做什麼.. 。聽起來不正確。 – djot
'extract($ data);'? http://php.net/extract你爲什麼要這樣做? –
爲什麼將它們分配給變量?你已經有了一個帶有命名鍵的數組。這比單獨的變量更有效。 – JamesG