回显数组的值?

发布于 2024-12-02 06:37:25 字数 814 浏览 0 评论 0原文

我想回显从搜索函数返回的所有数组的值。每个数组都包含一个从我的数据库收集的 $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

唠甜嗑 2024-12-09 06:37:26

搜索函数中的这一行: echo $rows['categories'] = $row; 有问题。对于 while 循环中的每一次传递,您都使用相同的键存储所有行。效果只是成功存储返回查询的最后一行。

你应该改变这个...

$rows = array();

while ($row = mysql_fetch_assoc($result)) {
    echo $rows['categories'] = $row;
}

mysql_free_result($result);

return $rows;

到这个...

$rows = array();

while ($row = mysql_fetch_assoc($result)) {
    $rows[] = $row;
}

return $rows;

然后当你访问返回值时,你可以像下面这样处理它...

foreach ($rows as $key => $array) {
    echo $array['columnName'];
    // or
    foreach ($array as $column => $value) {
        echo $column; // column name
        echo $value; // stored value
    }
}

This line: echo $rows['categories'] = $row; in your search function is problematic. For every pass in your while 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...

$rows = array();

while ($row = mysql_fetch_assoc($result)) {
    echo $rows['categories'] = $row;
}

mysql_free_result($result);

return $rows;

to this...

$rows = array();

while ($row = mysql_fetch_assoc($result)) {
    $rows[] = $row;
}

return $rows;

Then when you are accessing the returned value, you could handle it like the following...

foreach ($rows as $key => $array) {
    echo $array['columnName'];
    // or
    foreach ($array as $column => $value) {
        echo $column; // column name
        echo $value; // stored value
    }
}
活雷疯 2024-12-09 06:37:26

问题是您有一个多维数组,即数组的每个元素都是另一个数组。

而不是

echo $row['categories']; 

尝试 print_r

print_r($row['categories']);

这从技术上讲,它将按照您的要求进行操作,但更重要的是,它将帮助您了解子数组的结构,因此您可以打印所需的特定索引,而不是将整个数组转储到屏幕上。

The problem is that you have a multi-dimensional array, that is each element of your array is another array.

Instead of

echo $row['categories']; 

try print_r:

print_r($row['categories']);

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.

も星光 2024-12-09 06:37:26

var_dump($rows) 是什么样的?听起来像是一个多维数组。您可能需要有两个(或更多)循环:

foreach($rows as $row => $categories) {
   foreach($categories as $category) {
      echo $category;
   }
}

What does a var_dump($rows) look like? Sounds like it's a multidimensional array. You may need to have two (or more) loops:

foreach($rows as $row => $categories) {
   foreach($categories as $category) {
      echo $category;
   }
}
奶气 2024-12-09 06:37:26

我认为这应该可行:

foreach ($rows as $row => $categories) {
    echo $categories;
}

如果这将再次输出数组序列,请尝试查看其中的内容:

foreach ($rows as $row => $categories) {
    print_r($categories);
}

I think this should work:

foreach ($rows as $row => $categories) {
    echo $categories;
}

If this will output a sequence of Array's again, try to see what in it:

foreach ($rows as $row => $categories) {
    print_r($categories);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文