I have an array which is coming using print_r($variable)
in php. I want to modify this array to come in some another form. How can I achieve this.
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
)
)
I want it to come as:
Array
(
[body]=>Body
[field_tags]=>Tags
[field_image]=>Image
)
How can I achieve this using foreach loop. This array is coming from below code.
$serialize = field_info_instances('node',$form_state['values']['displays']['selected']);
foreach ($serialize as $key => $value) {
print_r($serialize);die;
}
Rewrite array saving keys but change value to label item
$newarray = array_map(function($i) { return $i['label']; }, $array);
What you're looking for is key ()
as it returns the key value from an array index. Please read up on this function over here
Here an example from this page:
<?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);
}
?>