简体   繁体   中英

eliminate 'Array' string While printing associative array

I have following PHP code

    <?php
$myRoom = array(
                'Kitchen' => array('Dining Table','4 Chairs','Dishes'),
                'Bathroom' => array('Towel','Medicines','Shower Gel'),
                'Bedroom' => array(
                                'Bed' => array(
                                            'Legs' => 4,
                                            'Bedsheet' => 'woolen',
                                            'Blanket' => array(
                                                               'Winter' => 'Thick',
                                                               'Summer' => 'Thin',
                                                            ),
                                ),
                                  ),
                'Person' => 'Only Me',
                );
my_room($myRoom);

function my_room($params) {
    foreach ($params as $key => $value) {
        yes_it_is(" <br /> ".$key." : ");
        my_room($value);
        yes_it_is($value." <br /> ");
    }
}

function yes_it_is($val) {
   echo $val;    
}

The functions below prints the string, and the output of the code looks like:

Kitchen : 0 : Dining Table

1 : 4 Chairs

2 : Dishes Array

Bathroom : 0 : Towel

1 : Medicines

2 : Shower Gel Array

Bedroom : Bed : Legs : 4

Bedsheet : woolen

Blanket : Winter : Thick

Summer : Thin Array Array Array

Person : Only Me

How can I get rid of the 'Array' string that is printed with other values. Any other workaround would also be appreciated.

Edit:

I want to print only keys and values that was in associative array, not Array as string in output.

Edit2: Question updated as per Daedelus' suggestion

I changed my_room function as below:

function my_room($params) {
foreach ($params as $key => $value) {
            yes_it_is(" <br /> ".$key." : ");
            if (is_array($value))
                my_room($value);
            else
                yes_it_is($value." <br /> ");
    }
}

But still the output is:

Kitchen : 0 : Dining Table

1 : 4 Chairs

2 : Dishes

Bathroom : 0 : Towel

1 : Medicines

2 : Shower Gel

Bedroom : Bed : Legs : 4

Bedsheet : woolen

Blanket : Winter : Thick

Summer : Thin Array Array

Person : Only Me

There are two Array printed after Thin

Edit3: Sorry my mistake, The accepted result works perfectly. While testing I had two functions together name my_room and my_room2 , while testing with both the functions together Array was displayed, but when I commented out my_room2 and only displayed my_room function as in the answer, it works perfectly. Sorry for any confusions caused, I was testing two functions together and the output was weird.

Edit4:

Based on the answers below, I changed it to good looking one line ternary operator like:

function my_room($params) {
foreach ($params as $key => $value) {
            yes_it_is(" <br /> ".$key." : ");
            is_array($value) ? my_room($value) : yes_it_is($value." <br /> ");
    }
}

Change my_room to the following (to check if it's an array)

function my_room($params) {
foreach ($params as $key => $value) {
            yes_it_is(" <br /> ".$key." : ");
            if (is_array($value))
                my_room($value);
            else
                yes_it_is($value." <br /> ");
    }
}

If you just want to ignore arrays passed to the function:

function yes_it_is($val) {
   if (!is_array($val)) {
       echo $val;
   }
}

Edit: Wait, you are concatenating with "<br />" before, so this will not work. Better solution:

function my_room($params) {
    foreach ($params as $key => $value) {
        yes_it_is(" <br /> ".$key." : ");
        if (is_array($value)) {
            my_room($value);
        } else {
            yes_it_is($value." <br /> ");
        }
    }
}
function yes_it_is($val) {
    if (is_array($val)) {
       print_r($val);
       //OR echo "<pre>"; print_r($val):
    } else {
       echo $val;
    }
}

is_array should do the trick:

function my_room($params) {
    foreach ($params as $key => $value) {  
        yes_it_is(" <br /> ".$key." : ");
        if (is_array($value)) {
            my_room($value);
        }
        else {
            yes_it_is($value." <br /> ");
        }
    }
}

What happens otherwise.. you are trying to convert an array to a string, and that comes out as the string Array . If you do a check before hand to make sure it is indeed an array, you won't get that error. I also recommend you turn on error reporting, as well as read the manual for arrays .

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