[英]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.