【问题标题】:For each loop prints array as well as array values [duplicate]对于每个循环打印数组以及数组值[重复]
【发布时间】:2020-06-15 06:23:39
【问题描述】:

我在 wordpress 网站中有一个 for each 循环,用于输出用于结构化数据的内容。这大部分工作正常,但我看到正在输出的实际数组以及其中的值。

所以我有以下内容:

"@context" : "http://schema.org",
"@type": ["ItemList", "events"],
"name": "<?php the_title(); ?>",
"url": "<?php echo $current_url ?>",
"itemListElement": [
$args = array( 
        'numberposts'       => -1, 
        'post_type'         => 'events', 
        'post_status'       => 'publish',
        'orderby'           => 'date', 
        'order'             => 'DESC', 
        'meta_query' => array(
            array(
                'key' => 'event_end_date',
                'compare' => '>=',
                'value' => $today
            )
        ),
        'meta_key' => 'event_start_date'
    );
    $myposts = get_posts($args);
    foreach ($myposts as $mypost) { 
        $args[] = 
        '{
            "@type": "ListItem",
            "item": {
                "@type": "Event",
                "name": "' . get_the_title($mypost->ID) . '",
                "location": "' . get_field( 'event_address', $mypost->ID) . '",
                "startDate": "' . get_field( 'event_start_date', $mypost->ID ) . '",
                "endDate": "' . get_field( 'event_end_date', $mypost->ID ) . '",
                "description": "' . get_the_excerpt($mypost->ID) . '"
            }
        }';
    }   
    echo implode( ', ', $args ); 

这个输出:

"@context" : "http://schema.org",
"@type": ["ItemList", "events"],
"name": "Forthcoming Shows",
"url": "http://localhost/forthcoming-shows/",
"itemListElement": [
-1, events, publish, date, DESC, Array, event_start_date, {  // this should be just the curly brace
    "@type": "ListItem",  
    "item": {  
        "@type": "Event",  
        "name": "name here", 
        "location": "this is the address",  
        "startDate": "25/06/2020",
        "endDate": "25/06/2020",
        "description": "some description"
    }
}, {
    "@type": "Event",  
        "name": "Another event", 
        "location": "an address",  
        "startDate": "25/06/2020",
        "endDate": "25/06/2020",
        "description": "some description"
    }
}]

为什么我会在结果上方打印数组? 我还收到一条错误消息“注意:第 214 行中的数组到字符串的转换。它指的是 echo implode 行。不完全确定这是否相关。

【问题讨论】:

  • 永远不要手动制作你的 json 字符串。使用json_encode() 或遭受头痛和脱发。
  • 构建你完整的多维数据结构,然后在你完成修改后对其进行一次json_encode。再简单不过了。你走在一条肮脏的道路上。 :)
  • 谢谢@mickmackusa,我将不得不调查一下。希望它对这种结构化数据的问题有很大帮助。不过,我仍然很想知道这里发生了什么

标签: php arrays wordpress foreach


【解决方案1】:

数组的第一个键是:

$args = array( 
        'numberposts'       => -1, 
        'post_type'         => 'events', 
        'post_status'       => 'publish',
        'orderby'           => 'date', 
        'order'             => 'DESC', 
        'meta_query' => array(
            array(
                'key' => 'event_end_date',
                'compare' => '>=',
                'value' => $today
            )
        ),
        'meta_key' => 'event_start_date'
    );

因此,当您进行内爆时,“meta_query”键作为数组。 这就是为什么当你打印它时它会显示一个数组。如果您想将该数组转换为字符串,您可以对其进行 json_encode(在 implode 之前):

        'meta_query' => json_encode(array(
            array(
                'key' => 'event_end_date',
                'compare' => '>=',
                'value' => $today
            )
        )),

这取决于你想用那个数组做什么。你必须自己解决嵌套问题。


编辑:(基于 cmets)

您在 meta_query 上有一个关联数组的数组。所以,这将打印一个对象数组。如果要删除数组的两层,可以这样做:

$args = array( 
        'numberposts'       => -1, 
        'post_type'         => 'events', 
        'post_status'       => 'publish',
        'orderby'           => 'date', 
        'order'             => 'DESC', 
        'meta_query' => json_encode(
            array(
                'key' => 'event_end_date',
                'compare' => '>=',
                'value' => $today
            )
        ),
        'meta_key' => 'event_start_date'
    );

这将打印一个 json 对象。如果您希望打印一个对象的对象,那么您必须将数组包含在关联数组中:

$args = array( 
        'numberposts'       => -1, 
        'post_type'         => 'events', 
        'post_status'       => 'publish',
        'orderby'           => 'date', 
        'order'             => 'DESC', 
        'meta_query' => json_encode(array(
            'mykey' => array(
                'key' => 'event_end_date',
                'compare' => '>=',
                'value' => $today
            )
        )),
        'meta_key' => 'event_start_date'
    );

但这一切都取决于你想用那个数组做什么......

【讨论】:

  • 这很好,因为它解决了错误消息“注意:数组到字符串的转换...在第 214 行”。缺点是它仍然打印数组。虽然现在它将这部分打印为 json。我可以理解。 -1, events, publish, date, DESC, Array, event_start_date, { 变成了 -1, events, publish, date, DESC, [{"key":"event_end_date","compare":"&gt;=","value":"20200615"}], event_start_date, {,而我希望/期待的只是 {
猜你喜欢
  • 2014-08-05
  • 1970-01-01
  • 1970-01-01
  • 2015-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多