回显数组的值?
我想回显从搜索函数返回的所有数组的值。每个数组都包含一个从我的数据库收集的 $category
。到目前为止,我编写的将这些作为其原始值进行回显的代码(例如,以它们在我的数据库中的相同形式)是:
$rows = search($rows);
if (count($rows) > 0) {
foreach($rows as $row => $texts) {
foreach ($texts as $idea) {
echo $idea;
}
}
}
但是,此代码回显的唯一内容是一个长字符串我的数据库中存在的所有信息。
我调用的函数的结果如下所示:
function search($query) {
$query = mysql_real_escape_string(preg_replace("[^A-Za-zÅÄÖåäö0-9 -_.]", "", $query));
$sql = "SELECT * FROM `text` WHERE categories LIKE '%$query%'";
$result = mysql_query($sql);
$rows = array();
while ($row = mysql_fetch_assoc($result)) {
$rows['text'] = $row;
}
mysql_free_result($result);
return $rows;
}
如何让它回显应该是数组值的实际文本?
I want to echo the values of all arrays that has been returned from a search function. Each array contains one $category
, that have been gathered from my DB. The code that I've written so far to echo
these as their original value (e.g. in the same form they lay in my DB.) is:
$rows = search($rows);
if (count($rows) > 0) {
foreach($rows as $row => $texts) {
foreach ($texts as $idea) {
echo $idea;
}
}
}
However, the only thing this code echoes is a long string of all the info that exists in my DB.
The function, which result I'm calling looks like this:
function search($query) {
$query = mysql_real_escape_string(preg_replace("[^A-Za-zÅÄÖåäö0-9 -_.]", "", $query));
$sql = "SELECT * FROM `text` WHERE categories LIKE '%$query%'";
$result = mysql_query($sql);
$rows = array();
while ($row = mysql_fetch_assoc($result)) {
$rows['text'] = $row;
}
mysql_free_result($result);
return $rows;
}
How can I make it echo the actual text that should be the value of the array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
搜索函数中的这一行:
echo $rows['categories'] = $row;
有问题。对于while
循环中的每一次传递,您都使用相同的键存储所有行。效果只是成功存储返回查询的最后一行。你应该改变这个...
到这个...
然后当你访问返回值时,你可以像下面这样处理它...
This line:
echo $rows['categories'] = $row;
in your search function is problematic. For every pass in yourwhile
loop, you are storing all rows with the same key. The effect is only successfully storing the last row from your returned query.You should change this...
to this...
Then when you are accessing the returned value, you could handle it like the following...
问题是您有一个多维数组,即数组的每个元素都是另一个数组。
而不是
尝试
print_r
:这从技术上讲,它将按照您的要求进行操作,但更重要的是,它将帮助您了解子数组的结构,因此您可以打印所需的特定索引,而不是将整个数组转储到屏幕上。
The problem is that you have a multi-dimensional array, that is each element of your array is another array.
Instead of
try
print_r
:This will technically do what you ask, but more importantly, it will help you understand the structure of your sub-arrays, so you can print the specific indices you want instead of dumping the entire array to the screen.
var_dump($rows)
是什么样的?听起来像是一个多维数组。您可能需要有两个(或更多)循环:What does a
var_dump($rows)
look like? Sounds like it's a multidimensional array. You may need to have two (or more) loops:我认为这应该可行:
如果这将再次输出数组序列,请尝试查看其中的内容:
I think this should work:
If this will output a sequence of Array's again, try to see what in it: