如何访问 php 中给定数组结构的数组键值?
How to access array key values of given array structure in php?
我有一个数组,即将在 php 中使用 print_r($variable)
。我想修改这个数组以另一种形式出现。我怎样才能做到这一点。
Array
(
[body] => Array
(
[label] => Body
[widget] => Array
(
[type] => text_textarea_with_summary
[settings] => Array
(
[rows] => 20
[summary_rows] => 5
)
[weight] => 31
[module] => text
)
[settings] => Array
(
[display_summary] => 1
[text_processing] => 1
[user_register_form] =>
)
[display] => Array
(
[default] => Array
(
[label] => hidden
[type] => text_default
[settings] => Array
(
)
[module] => text
[weight] => 0
)
[teaser] => Array
(
[label] => hidden
[type] => text_summary_or_trimmed
[settings] => Array
(
[trim_length] => 600
)
[module] => text
[weight] => 0
)
)
[required] =>
[description] =>
[id] => 6
[field_id] => 2
[field_name] => body
[entity_type] => node
[bundle] => article
[deleted] => 0
[default_value] =>
)
[field_tags] => Array
(
[label] => Tags
[description] => Enter a comma-separated list of words to describe your content.
[widget] => Array
(
[type] => taxonomy_autocomplete
[weight] => -4
[settings] => Array
(
[size] => 60
[autocomplete_path] => taxonomy/autocomplete
)
[module] => taxonomy
)
[display] => Array
(
[default] => Array
(
[type] => taxonomy_term_reference_link
[weight] => 10
[label] => above
[settings] => Array
(
)
[module] => taxonomy
)
[teaser] => Array
(
[type] => taxonomy_term_reference_link
[weight] => 10
[label] => above
[settings] => Array
(
)
[module] => taxonomy
)
)
[settings] => Array
(
[user_register_form] =>
)
[required] =>
[id] => 7
[field_id] => 3
[field_name] => field_tags
[entity_type] => node
[bundle] => article
[deleted] => 0
[default_value] =>
)
[field_image] => Array
(
[label] => Image
[description] => Upload an image to go with this article.
[required] =>
[settings] => Array
(
[file_directory] => field/image
[file_extensions] => png gif jpg jpeg
[max_filesize] =>
[max_resolution] =>
[min_resolution] =>
[alt_field] => 1
[title_field] =>
[default_image] => 0
[user_register_form] =>
)
[widget] => Array
(
[type] => image_image
[settings] => Array
(
[progress_indicator] => throbber
[preview_image_style] => thumbnail
)
[weight] => -1
[module] => image
)
[display] => Array
(
[default] => Array
(
[label] => hidden
[type] => image
[settings] => Array
(
[image_style] => large
[image_link] =>
)
[weight] => -1
[module] => image
)
[teaser] => Array
(
[label] => hidden
[type] => image
[settings] => Array
(
[image_style] => medium
[image_link] => content
)
[weight] => -1
[module] => image
)
)
[id] => 8
[field_id] => 4
[field_name] => field_image
[entity_type] => node
[bundle] => article
[deleted] => 0
)
)
我希望它成为:
Array
(
[body]=>Body
[field_tags]=>Tags
[field_image]=>Image
)
如何使用 foreach 循环实现此目的。
这个数组来自下面的代码。
$serialize = field_info_instances('node',$form_state['values']['displays']['selected']);
foreach ($serialize as $key => $value) {
print_r($serialize);die;
}
重写数组保存键,但将值更改为标签项
$newarray = array_map(function($i) { return $i['label']; }, $array);
您要查找的是 key ()
,因为它 returns 来自数组索引的键值。请仔细阅读此功能 over here
这里是这个页面的例子:
<?php
$array = array(
'fruit1' => 'apple',
'fruit2' => 'orange',
'fruit3' => 'grape',
'fruit4' => 'apple',
'fruit5' => 'apple');
// this cycle echoes all associative array
// key where value equals "apple"
while ($fruit_name = current($array)) {
if ($fruit_name == 'apple') {
echo key($array).'<br />';
}
next($array);
}
?>
我有一个数组,即将在 php 中使用 print_r($variable)
。我想修改这个数组以另一种形式出现。我怎样才能做到这一点。
Array
(
[body] => Array
(
[label] => Body
[widget] => Array
(
[type] => text_textarea_with_summary
[settings] => Array
(
[rows] => 20
[summary_rows] => 5
)
[weight] => 31
[module] => text
)
[settings] => Array
(
[display_summary] => 1
[text_processing] => 1
[user_register_form] =>
)
[display] => Array
(
[default] => Array
(
[label] => hidden
[type] => text_default
[settings] => Array
(
)
[module] => text
[weight] => 0
)
[teaser] => Array
(
[label] => hidden
[type] => text_summary_or_trimmed
[settings] => Array
(
[trim_length] => 600
)
[module] => text
[weight] => 0
)
)
[required] =>
[description] =>
[id] => 6
[field_id] => 2
[field_name] => body
[entity_type] => node
[bundle] => article
[deleted] => 0
[default_value] =>
)
[field_tags] => Array
(
[label] => Tags
[description] => Enter a comma-separated list of words to describe your content.
[widget] => Array
(
[type] => taxonomy_autocomplete
[weight] => -4
[settings] => Array
(
[size] => 60
[autocomplete_path] => taxonomy/autocomplete
)
[module] => taxonomy
)
[display] => Array
(
[default] => Array
(
[type] => taxonomy_term_reference_link
[weight] => 10
[label] => above
[settings] => Array
(
)
[module] => taxonomy
)
[teaser] => Array
(
[type] => taxonomy_term_reference_link
[weight] => 10
[label] => above
[settings] => Array
(
)
[module] => taxonomy
)
)
[settings] => Array
(
[user_register_form] =>
)
[required] =>
[id] => 7
[field_id] => 3
[field_name] => field_tags
[entity_type] => node
[bundle] => article
[deleted] => 0
[default_value] =>
)
[field_image] => Array
(
[label] => Image
[description] => Upload an image to go with this article.
[required] =>
[settings] => Array
(
[file_directory] => field/image
[file_extensions] => png gif jpg jpeg
[max_filesize] =>
[max_resolution] =>
[min_resolution] =>
[alt_field] => 1
[title_field] =>
[default_image] => 0
[user_register_form] =>
)
[widget] => Array
(
[type] => image_image
[settings] => Array
(
[progress_indicator] => throbber
[preview_image_style] => thumbnail
)
[weight] => -1
[module] => image
)
[display] => Array
(
[default] => Array
(
[label] => hidden
[type] => image
[settings] => Array
(
[image_style] => large
[image_link] =>
)
[weight] => -1
[module] => image
)
[teaser] => Array
(
[label] => hidden
[type] => image
[settings] => Array
(
[image_style] => medium
[image_link] => content
)
[weight] => -1
[module] => image
)
)
[id] => 8
[field_id] => 4
[field_name] => field_image
[entity_type] => node
[bundle] => article
[deleted] => 0
)
)
我希望它成为:
Array
(
[body]=>Body
[field_tags]=>Tags
[field_image]=>Image
)
如何使用 foreach 循环实现此目的。 这个数组来自下面的代码。
$serialize = field_info_instances('node',$form_state['values']['displays']['selected']);
foreach ($serialize as $key => $value) {
print_r($serialize);die;
}
重写数组保存键,但将值更改为标签项
$newarray = array_map(function($i) { return $i['label']; }, $array);
您要查找的是 key ()
,因为它 returns 来自数组索引的键值。请仔细阅读此功能 over here
这里是这个页面的例子:
<?php
$array = array(
'fruit1' => 'apple',
'fruit2' => 'orange',
'fruit3' => 'grape',
'fruit4' => 'apple',
'fruit5' => 'apple');
// this cycle echoes all associative array
// key where value equals "apple"
while ($fruit_name = current($array)) {
if ($fruit_name == 'apple') {
echo key($array).'<br />';
}
next($array);
}
?>