如何更改此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
)
答案 0 :(得分:1)
听起来你想将一个关联数组转换成一个句子,例如:
$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}等)