如何更改此 php:
<?php
$result = $sth->fetchAll();
print_r($result); //or var_dump($result); for more info
foreach($result as $row){
print_r($row);
}
?>
输出,给我举个例子:
Joseph Dickinson wants an Xbox 360 for 150
I need a one quick!
John Doe offered it for 149.99
Jane Doe offered it for 154.99
如何使用输出的数组
print_r
结果执行上述操作?
Array
(
[0] => Array
(
[uFName] => Joseph
[0] => Joseph
[uLName] => Dickinson
[1] => Dickinson
[listTitle] => Need Xbox 360
[2] => Need Xbox 360
[listPropPrice] => 150
[3] => 150
[listCmt] => I need one quick!
[4] => I need one quick!
[listDt] => 2011-09-15
[5] => 2011-09-15
[mFName] => John
[6] => John
[mLName] => Doe
[7] => Doe
[moAmt] => 149.99
[8] => 149.99
[moDtOff] => 2011-09-15
[9] => 2011-09-15
)
[1] => Array
(
[uFName] => Joseph
[0] => Joseph
[uLName] => Dickinson
[1] => Dickinson
[listTitle] => Need Xbox 360
[2] => Need Xbox 360
[listPropPrice] => 150
[3] => 150
[listCmt] => I need one quick!
[4] => I need one quick!
[listDt] => 2011-09-15
[5] => 2011-09-15
[mFName] => Jane
[6] => Jane
[mLName] => Doe
[7] => Doe
[moAmt] => 154.99
[8] => 154.99
[moDtOff] => 2011-09-15
[9] => 2011-09-15
)
[2] => Array
(
[uFName] => Joseph
[0] => Joseph
[uLName] => Dickinson
[1] => Dickinson
[listTitle] => Need Xbox 360
[2] => Need Xbox 360
[listPropPrice] => 150
[3] => 150
[listCmt] => I need one quick!
[4] => I need one quick!
[listDt] => 2011-09-15
[5] => 2011-09-15
[mFName] => Diana
[6] => Diana
[mLName] => Matthews
[7] => Matthews
[moAmt] => 160.00
[8] => 160.00
[moDtOff] => 2011-09-15
[9] => 2011-09-15
)
[3] => Array
(
[uFName] => Joseph
[0] => Joseph
[uLName] => Dickinson
[1] => Dickinson
[listTitle] => Need Xbox 360
[2] => Need Xbox 360
[listPropPrice] => 150
[3] => 150
[listCmt] => I need one quick!
[4] => I need one quick!
[listDt] => 2011-09-15
[5] => 2011-09-15
[mFName] => Amanda
[6] => Amanda
[mLName] => Koste
[7] => Koste
[moAmt] => 174.99
[8] => 174.99
[moDtOff] => 2011-09-15
[9] => 2011-09-15
)
[4] => Array
(
[uFName] => Warren
[0] => Warren
[uLName] => Kennan
[1] => Kennan
[listTitle] => Need New Sofa
[2] => Need New Sofa
[listPropPrice] => 1000
[3] => 1000
[listCmt] => Need one quick
[4] => Need one quick
[listDt] => 2011-09-15
[5] => 2011-09-15
[mFName] => Diana
[6] => Diana
[mLName] => Matthews
[7] => Matthews
[moAmt] => 495.99
[8] => 495.99
[moDtOff] => 2011-09-15
[9] => 2011-09-15
)
[5] => Array
(
[uFName] => Warren
[0] => Warren
[uLName] => Kennan
[1] => Kennan
[listTitle] => Need New Sofa
[2] => Need New Sofa
[listPropPrice] => 1000
[3] => 1000
[listCmt] => Need one quick
[4] => Need one quick
[listDt] => 2011-09-15
[5] => 2011-09-15
[mFName] => Amanda
[6] => Amanda
[mLName] => Koste
[7] => Koste
[moAmt] => 489.99
[8] => 489.99
[moDtOff] => 2011-09-15
[9] => 2011-09-15
)
)
Array
(
[uFName] => Joseph
[0] => Joseph
[uLName] => Dickinson
[1] => Dickinson
[listTitle] => Need Xbox 360
[2] => Need Xbox 360
[listPropPrice] => 150
[3] => 150
[listCmt] => I need one quick!
[4] => I need one quick!
[listDt] => 2011-09-15
[5] => 2011-09-15
[mFName] => John
[6] => John
[mLName] => Doe
[7] => Doe
[moAmt] => 149.99
[8] => 149.99
[moDtOff] => 2011-09-15
[9] => 2011-09-15
)
Array
(
[uFName] => Joseph
[0] => Joseph
[uLName] => Dickinson
[1] => Dickinson
[listTitle] => Need Xbox 360
[2] => Need Xbox 360
[listPropPrice] => 150
[3] => 150
[listCmt] => I need one quick!
[4] => I need one quick!
[listDt] => 2011-09-15
[5] => 2011-09-15
[mFName] => Jane
[6] => Jane
[mLName] => Doe
[7] => Doe
[moAmt] => 154.99
[8] => 154.99
[moDtOff] => 2011-09-15
[9] => 2011-09-15
)
Array
(
[uFName] => Joseph
[0] => Joseph
[uLName] => Dickinson
[1] => Dickinson
[listTitle] => Need Xbox 360
[2] => Need Xbox 360
[listPropPrice] => 150
[3] => 150
[listCmt] => I need one quick!
[4] => I need one quick!
[listDt] => 2011-09-15
[5] => 2011-09-15
[mFName] => Diana
[6] => Diana
[mLName] => Matthews
[7] => Matthews
[moAmt] => 160.00
[8] => 160.00
[moDtOff] => 2011-09-15
[9] => 2011-09-15
)
Array
(
[uFName] => Joseph
[0] => Joseph
[uLName] => Dickinson
[1] => Dickinson
[listTitle] => Need Xbox 360
[2] => Need Xbox 360
[listPropPrice] => 150
[3] => 150
[listCmt] => I need one quick!
[4] => I need one quick!
[listDt] => 2011-09-15
[5] => 2011-09-15
[mFName] => Amanda
[6] => Amanda
[mLName] => Koste
[7] => Koste
[moAmt] => 174.99
[8] => 174.99
[moDtOff] => 2011-09-15
[9] => 2011-09-15
)
Array
(
[uFName] => Warren
[0] => Warren
[uLName] => Kennan
[1] => Kennan
[listTitle] => Need New Sofa
[2] => Need New Sofa
[listPropPrice] => 1000
[3] => 1000
[listCmt] => Need one quick
[4] => Need one quick
[listDt] => 2011-09-15
[5] => 2011-09-15
[mFName] => Diana
[6] => Diana
[mLName] => Matthews
[7] => Matthews
[moAmt] => 495.99
[8] => 495.99
[moDtOff] => 2011-09-15
[9] => 2011-09-15
)
Array
(
[uFName] => Warren
[0] => Warren
[uLName] => Kennan
[1] => Kennan
[listTitle] => Need New Sofa
[2] => Need New Sofa
[listPropPrice] => 1000
[3] => 1000
[listCmt] => Need one quick
[4] => Need one quick
[listDt] => 2011-09-15
[5] => 2011-09-15
[mFName] => Amanda
[6] => Amanda
[mLName] => Koste
[7] => Koste
[moAmt] => 489.99
[8] => 489.99
[moDtOff] => 2011-09-15
[9] => 2011-09-15
)
听起来你想将关联数组转换为句子,例如:
$array = array('noun'=>'spot', 'verb'=>'run');
您可以简单地从各个部分构建句子:
$sentence = "See ".$array['noun'].".\n".
"See ".$array['noun']." ".$array['verb'].".\n".
$array['verb']." ".$array['noun'].", ".$array['verb']."!";
这是最简单、最有效的方法。然而,它也有一些缺点;
另一种方法是在字符串中使用占位符,并替换值:
$sentence_template = "See {noun}.\nSee {noun} {verb}. {verb} {noun}, {verb}!";
$from = array();
foreach(array_keys($array) as $label)
$from[] = '{'.$label.'}';
$to = array_values($array);
$sentence = str_replace($from, $to, $sentence_template);
这种方法允许将模板字符串单独定义到程序中,例如在数据库或配置文件中,也更易于阅读和维护。另一方面;
听起来对于你的问题,无论哪种方式都可以。
(如果您使用第二种方法,请务必将数据库对象配置为仅返回关联(字符串)元素,而不是数字元素;否则它将不必要地搜索 {0}、{1} 等)