1

Recently I've been given an interview where many question were ask, but there was one question I could not answer which was the following:

Printing a multi dimensional array using one foreach loop only.

$cars = array (  
    array("Volvo",22,18), 
    array("BMW",15,13),
    array("Saab",5,2),
    array("Land Rover",17,15) 
);

Interviewer asked me to print the above array using only one foreach loop. Can anybody give me answer for this?

4
  • Have you tried to print? Commented Feb 17, 2016 at 9:32
  • Give us a sample of the multi dimensional array. Commented Feb 17, 2016 at 9:32
  • This will be interesting.. Commented Feb 17, 2016 at 9:32
  • hey, i have added the array Commented Feb 17, 2016 at 9:34

6 Answers 6

2

You can use recursive function for this. Only one foreach loop calls recursively.

$cars = array(array("Volvo",22,18),array("BMW",15,13),array("Saab",5,2),array("Land Rover",17,15));

function displayArr($arr) {
    foreach($arr as $key => $value) {
        if(is_array($value)) {
            displayArr($value);
            echo "<br>";
        }
        else {
            echo $value . " . ";
        }
    }
}

displayArr($cars);
Sign up to request clarification or add additional context in comments.

Comments

1

Here you go

$cars = array (  array("Volvo",22,18), array("BMW",15,13),array("Saab",5,2), array("Land Rover",17,15) );

foreach($cars as $car) {
    print("Name: ".$car[0]."\n");
    print("Number 1: ".$car[1]."\n");
    print("Number 2: ".$car[2]."\n");
}

Obviously could could make checks to see if the other keys in the inner arrays are set. But seems your array will always contains all the keys.

EDIT:

Based on the comment you left you want to be dynamic as possible so do to that you need to use recursion or the goto control structure.

$cars = array (  array("Volvo",22,18), array("BMW",15,13),array("Saab",5,2), array("Land Rover",17,15) );

foreach($cars as $car) {
    $count = 0;

    A:
    if(isset($car[$count])) {
        print($car[$count] . "\n");
        $count +=1;
        goto A;
    }
}

5 Comments

how can you give indexing to array, if there is thousands of values in array then what, should i write it thousand time to print all values.
Nope you don't have too am just doing stuff based on the array you provided. Let me update the answer.
Wow, thats the first time I have seen a goto since the 80's. Almost tempted to give you an UV for digging it out of the back of the tool bag, dusting it off and giving it a day in the sunshine
@RiggsFolly and I wasn't even born then. I guess its the fact I work with seniors around me hehe xD. Not really. It was the first thing I come up and that I was sure would work with out testing it.
@Andre Ferraz thank you for answer and yes you are right i wanted the dynamic foreach loop.
0

Here is a method that does not use even one foreach loop

function walk2($item, $key)
{
    echo $key . ' - ' . $item.PHP_EOL;
}

function walk1($car, $key)
{
    array_walk($car, 'walk2');
    echo '------'.PHP_EOL;
}

array_walk($cars, 'walk1');

Gives the output :

0 - Volvo
1 - 22
2 - 18
------
0 - BMW
1 - 15
2 - 13
------
0 - Saab
1 - 5
2 - 2
------
0 - Land Rover
1 - 17
2 - 15
------

Although possibly the question was supposed to make you ask logical questions about the make up of the array so they could test your analytic skill as much as your ability to jump through coding hoops.

Also, even though you are not using a foreach, or any other loop you are making use of the 'C' code that was written for these functions which of course is making use of some sort of loop.

Comments

0

This can give you the result.

<?php
$cars = array (  array("Volvo",22,18), array("BMW",15,13),array("Saab",5,2), array("Land Rover",17,15) );

array_map('fun', $cars);
function fun($cars){
    foreach($cars as $car){
        echo $car."<br />";
    }
}
?>

output

Volvo 22 18 BMW 15 13 Saab 5 2 Land Rover 17 15

use array_map to traverse through the child arrays .

Comments

0
foreach($cars as $key=>$car){
    echo $car[0];
    echo $car[1];
    echo $car[2];
}

Comments

0
$cars = array (
    array("Volvo",22,18), 
    array("BMW",15,13),
    array("Saab",5,2),
    array("Land Rover",17,15) 
);

foreach($cars as $car){
    echo "<pre>";
    print_r($car);
    echo "</pre>";
}

1 Comment

This is extremely vague, could you provide more information?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.