错误:类 stdClass 的对象无法转换为字符串 -- implode() -- php

发布于 2024-12-09 11:29:38 字数 972 浏览 2 评论 0原文

任何人都可以帮助我理解为什么我收到以下错误:

类 stdClass 的对象无法转换为字符串...(错误指向带有 implode(),< 的行/strong> 见下文)

当我运行以下函数时?

  function selectFullArticle () {

    global $wpdb;

  $id=get_the_ID();


      $webPageArticle = $wpdb->get_results( "SELECT post_content_long FROM $wpdb->posts WHERE ID=$id" );

 $webPageArticle= implode(" ",$webPageArticle);
 return $webPageArticle;

我的目标

是返回一个字符串而不是一个数组。

也许必须以不同的方式处理从 SELECT 返回的数组?

提前致谢,

玛丽娜,


谢谢您的回答。我正在尝试显示从网上下载并将其保存在 WordPress 数据库中的网页,而不是帖子。

两个都 $webPageArticle = $wpdb->get_results( "从 $wpdb->posts WHERE ID=$id 中选择 post_content_long",ARRAY_N); 和 $webPageArticle = $wpdb->get_results( "从 $wpdb->posts WHERE ID=$id 中选择 post_content_long",ARRAY_A);

工作良好,并且 implode() 不再抱怨。但是,我没有得到真正的字符串,因为语句“echo $webPageArticle;”在屏幕上可视化单词“Array”。怎么

来的?

码头

can anybody help me understand why I get the followint error:

Object of class stdClass could not be converted to string in... (the error is pointing to the line with implode(), see below)

when I run the following function?

  function selectFullArticle () {

    global $wpdb;

  $id=get_the_ID();


      $webPageArticle = $wpdb->get_results( "SELECT post_content_long FROM $wpdb->posts WHERE ID=$id" );

 $webPageArticle= implode(" ",$webPageArticle);
 return $webPageArticle;

}

My aim is to return a string and not an array.

Maybe the array returned from a SELECT must be treated in a different way?

Thanks in advance,

Marina


Thanks for your answers. I am trying to display a webpage that I downloaded from the web and saved it in a wordpress database, and not a post.

Both
$webPageArticle = $wpdb->get_results( "SELECT post_content_long FROM $wpdb->posts WHERE ID=$id",ARRAY_N);
and
$webPageArticle = $wpdb->get_results( "SELECT post_content_long FROM $wpdb->posts WHERE ID=$id",ARRAY_A);

work well and implode() does not complain anymore. However, I do not get a real string, because the statement "echo $webPageArticle;" visualizes the word "Array" on the screen. T

how come?

marina

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

迟月 2024-12-16 11:29:38

正如 Codex 中所述,您可以将额外的第二个参数传递给 get_result() 以允许其返回数组而不是对象

 <?php $wpdb->get_results('query',OBJECT_K); ?> 

返回一个可以操作的关联数组。

参考:

OBJECT - result will be output as a numerically indexed array of row objects.
OBJECT_K - result will be output as an associative array of row objects, using first column's values as keys (duplicates will be discarded).
ARRAY_A - result will be output as an numerically indexed array of associative arrays, using column names as keys.
ARRAY_N - result will be output as a numerically indexed array of numerically indexed arrays

As read in Codex, you can pass an additional second parameter to get_result() to allow it to return an array instead of an object

 <?php $wpdb->get_results('query',OBJECT_K); ?> 

returns an associative array you can then manipulate.

Reference:

OBJECT - result will be output as a numerically indexed array of row objects.
OBJECT_K - result will be output as an associative array of row objects, using first column's values as keys (duplicates will be discarded).
ARRAY_A - result will be output as an numerically indexed array of associative arrays, using column names as keys.
ARRAY_N - result will be output as a numerically indexed array of numerically indexed arrays
萤火眠眠 2024-12-16 11:29:38
function selectFullArticle () {
    $id=get_the_ID();
    $webPageArticle = get_post($id, ARRAY_A);
    $webPageArticle= implode(" ", $webPageArticle);
    return $webPageArticle;
}

如果我认为正确并且您只想要帖子内容,请使用以下内容:

function selectFullArticle () {
    $id=get_the_ID();
    $webPageArticle = get_post($id);
    return $webPageArticle->post_content;
}
function selectFullArticle () {
    $id=get_the_ID();
    $webPageArticle = get_post($id, ARRAY_A);
    $webPageArticle= implode(" ", $webPageArticle);
    return $webPageArticle;
}

If I figured it right and you want the post content only, use this:

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