[英]Display specific values of array comma separated
我一直在尝试显示由逗号分隔的数组的特定值(最后一个除外)。
这是我的数组:
"pure_taxonomies": {
"domaine": [
{
"term_id": 36,
"name": "Domaine TEST",
"slug": "domaine-test",
},
{
"term_id": 40,
"name": "DomaineTEST2",
"slug": "domainetest2",
}
],
我想显示用逗号分隔的每个 NAME。 我尝试过的:
$response = wp_remote_get( add_query_arg( array(), 'http://website.com/wp-json/wp/v2/api' ) );
$postid = url_to_postid( $url );
if( !is_wp_error( $response ) && $response['response']['code'] == 200 ) {
$remote_posts = json_decode( $response['body'] );
foreach( $remote_posts as $remote_post ) {?>
<?php
foreach( $remote_post->pure_taxonomies->domaine as $pure_taxonomies_domaine ) {
$domaines = $pure_taxonomies_domaine->name;
echo $domaines .', ';
}
}
}
我想要的是 Domaine TEST, DomaineTEST2
谢谢
您的输入对象是一个 JSON。
assoc
标志(第二个参数)设置为true 。 这将为您提供一个关联数组。names
。请尝试以下操作:
if( !is_wp_error( $response ) && $response['response']['code'] == 200 ) {
// convert into multi-dimensional associative array
$remote_posts = json_decode( $response['body'], true );
foreach( $remote_posts as $remote_post ) {
// Use array_column function to get all the names
$names = array_column($remote_post['pure_taxonomies']['domaine'], 'name');
// use implode to convert into comma separated string
echo implode(', ', $names);
}
}
请检查以下代码:
// ...
$domaine_names = array();
foreach( $remote_post->pure_taxonomies->domaine as $pure_taxonomies_domaine ) {
$domaine_names[] = $pure_taxonomies_domaine->name;
}
echo implode(', ', $domaine_names);
// ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.