如何从查询结果集中组合多个列值以创建格式化句子?
如何更改此 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
)
How do I change this php:
<?php
$result = $sth->fetchAll();
print_r($result); //or var_dump($result); for more info
foreach($result as $row){
print_r($row);
}
?>
That outputs, to give me for example:
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
How do I do that above using the outputted array print_r
results?
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
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您想将关联数组转换为句子,例如:
$array = array('noun'=>'spot', 'verb'=>'run');
您可以简单地从各个部分构建句子:
这是最简单、最有效的方法。然而,它也有一些缺点;
另一种方法是在字符串中使用占位符,并替换值:
这种方法允许将模板字符串单独定义到程序中,例如在数据库或配置文件中,也更易于阅读和维护。另一方面;
听起来对于你的问题,无论哪种方式都可以。
(如果您使用第二种方法,请务必将数据库对象配置为仅返回关联(字符串)元素,而不是数字元素;否则它将不必要地搜索 {0}、{1} 等)
It sounds like you want to convert an associative array into a sentence, eg:
$array = array('noun'=>'spot', 'verb'=>'run');
You could simply build the sentence from parts:
This is the simplest and most efficient method. However, it has some drawbacks;
Another approach is to use placeholders in a string, and substitute in the values:
This approach allows the template string to be defined separately to the program, for instance in a database or configuration file, it's also easier to read and maintain. On the other hand;
It sounds like for your question, either way would be fine.
(If you use the second approach, be sure to configure the database object to just return associative (string) elements, not numeric elements; otherwise it will be needlessly searching for {0}, {1}, etc)