1

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 /> ");
    }
}
3
  • php.net/manual/en/function.implode.php ? Commented Mar 12, 2013 at 18:28
  • 1
    Fix your code so it can handle the deeper levels of your array, or add code to filter out those sub-arrays or otherwise convert them to printable strings. Commented Mar 12, 2013 at 18:32
  • @MarcB, any workaround/tutorial to achieve that would be appreciated... Commented Mar 12, 2013 at 18:50

4 Answers 4

2

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 /> ");
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

@JhilkeDai Please update your question code then under another header, because the above should have worked for you.
@JhilkeDai Well, I don't know what to tell you. I just did a test with that code and it works fine. What php version are you running on?
@Daedalus 5.4.6 the Array in other places like. after Dishes and Gel went away. There were 3 Array after Thin, now it shows two. I too really don't know why it is like that
@Daedalus I edited the question again, the answer is working perfectly.
0

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 /> ");
        }
    }
}

Comments

0
function yes_it_is($val) {
    if (is_array($val)) {
       print_r($val);
       //OR echo "<pre>"; print_r($val):
    } else {
       echo $val;
    }
}

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.