简体   繁体   中英

Print only the first or first few “layers” of an array with php?

I'm creating a drupal node template page and am trying to get a handle on a typically large multi-dimensional array created from a field collection. I am echoing the arrays out within pre tags so that they are formatted but I am still finding them very difficult to read because of their size. For example, here is a portion of the first array item in my $field_collection:

Array
(
[0] => Array
    (
        [2577] => Array
            (
                [#view_mode] => full
                [field_sec1_title] => Array
                    (
                        [#theme] => field
                        [#weight] => 0
                        [#title] => Title
                        [#access] => 1
                        [#label_display] => above
                        [#view_mode] => full
                        [#language] => und
                        [#field_name] => field_sec1_title
                        [#field_type] => text
                        [#field_translatable] => 0
                        [#entity_type] => field_collection_item
                        [#bundle] => field_scholarship
                        [#object] => FieldCollectionItemEntity Object
                            (
                                [fieldInfo:protected] => 
                                [hostEntity:protected] => stdClass Object
                                    (

etc etc.

So would it be possible to print out that array, but to stop printing past a specified point (for example, the array within it that displays the field collection item value), so it looks something like this...?

Array
(
[0] => Array
    (
        [2577] => Array()
        [2578] => Array()
        [2579] => Array()   
     )
)

2 ways, 1. copy the resulting output into something like Notepad++ so you can fold array brackets easily. 2. modify your array prior to output

$outputArray = [];
foreach ($array as $id=>$data){
  $outputArray[$id] = [
     'MemberOfInterest'=>$data['MemberOfInterest'],
     'MemberOfInterest2'=>$data['MemberOfInterest2'],
   ];
}
echo '<pre>'.print_r($outputArray,true).'</pre>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM