PHP 根据键显示数组中对象的值
PHP Display Values from Object within an Array Based on Key
我有以下数组,其中包含对象和数组。我如何根据它们的键仅获取特定值(对于每个对象)?
我已经测试过并且数组正在显示(见下文)但我无法根据需要隔离“名称”值。
我尝试了以下代码来获取名称值:
case 'field_prgm_housing' :
$node = 'field_color';
$tids = field_get_items('node', $node, $key, $node->language);
$terms = taxonomy_term_load_multiple(array(), array('tid' => $tids));
$nameonly = $terms->[0]->name[0];
return = $nameonly;
break;
Colors (Array, 2 elements)
12 (Object) stdClass
tid (String, 2 characters ) 12
vid (String, 1 characters ) 3
name (String, 9 characters ) Blue
description (String, 0 characters )
format (String, 13 characters ) filtered_html
weight (String, 1 characters ) 0
vocabulary_machine_name (String, 15 characters ) colors
rdf_mapping (Array, 5 elements)
path (Array, 1 element)
13 (Object) stdClass
tid (String, 2 characters ) 13
vid (String, 1 characters ) 3
name (String, 8 characters ) Green
description (String, 0 characters )
format (String, 13 characters ) filtered_html
weight (String, 1 characters ) 0
vocabulary_machine_name (String, 15 characters ) colors
rdf_mapping (Array, 5 elements)
path (Array, 1 element)
Try this
$node = 'field_color';
$tids = field_get_items('node', $node, $key, $node->language);
$terms = taxonomy_term_load_multiple(array(), array('tid' => $tids));
//loop all the values and get the require value
$name = array();
foreach($terms as $term){
$name[] = $term->name;
}
return $name;
$terms->[0]
是什么意思?
$terms
是一个数组,因此您必须通过指定要获取的索引来访问它,例如:$terms[12], $terms[13]
...
该数组的元素是对象,因此当您获得一个时,您必须使用“->”运算符来获取它的字段(或方法)。
所以它必须是这样的:
$nameonly = $terms[12]->name;
$nameonly = $terms[13]->name;
或者您可以按照@kranthi 的建议遍历所有数组项。
我有以下数组,其中包含对象和数组。我如何根据它们的键仅获取特定值(对于每个对象)? 我已经测试过并且数组正在显示(见下文)但我无法根据需要隔离“名称”值。
我尝试了以下代码来获取名称值:
case 'field_prgm_housing' :
$node = 'field_color';
$tids = field_get_items('node', $node, $key, $node->language);
$terms = taxonomy_term_load_multiple(array(), array('tid' => $tids));
$nameonly = $terms->[0]->name[0];
return = $nameonly;
break;
Colors (Array, 2 elements)
12 (Object) stdClass
tid (String, 2 characters ) 12
vid (String, 1 characters ) 3
name (String, 9 characters ) Blue
description (String, 0 characters )
format (String, 13 characters ) filtered_html
weight (String, 1 characters ) 0
vocabulary_machine_name (String, 15 characters ) colors
rdf_mapping (Array, 5 elements)
path (Array, 1 element)
13 (Object) stdClass
tid (String, 2 characters ) 13
vid (String, 1 characters ) 3
name (String, 8 characters ) Green
description (String, 0 characters )
format (String, 13 characters ) filtered_html
weight (String, 1 characters ) 0
vocabulary_machine_name (String, 15 characters ) colors
rdf_mapping (Array, 5 elements)
path (Array, 1 element)
Try this
$node = 'field_color';
$tids = field_get_items('node', $node, $key, $node->language);
$terms = taxonomy_term_load_multiple(array(), array('tid' => $tids));
//loop all the values and get the require value
$name = array();
foreach($terms as $term){
$name[] = $term->name;
}
return $name;
$terms->[0]
是什么意思?
$terms
是一个数组,因此您必须通过指定要获取的索引来访问它,例如:$terms[12], $terms[13]
...
该数组的元素是对象,因此当您获得一个时,您必须使用“->”运算符来获取它的字段(或方法)。
所以它必须是这样的:
$nameonly = $terms[12]->name;
$nameonly = $terms[13]->name;
或者您可以按照@kranthi 的建议遍历所有数组项。