0

I have an array:

$instructions = array (
array("step_no"=>"1","description"=>"Ensure that you have sufficient balance"),
array("step_no"=>"2","description"=>"Approve the request sent to your phone")
);

What I want is to loop through this array, which I have done, but I am now confused because I don't know how to get the output I desire.

foreach ($array as $key => $value) { 
//echo $key . "\n"; 
foreach ($value as $sub_key => $sub_val) {      
    if (is_array($sub_val)) { 
        //echo $sub_key . " : \n"; 
        foreach ($sub_val as $k => $v) { 
            echo "\t" .$k . " = " . $v . "\n"; 
        } 
    } else { 
        echo $sub_key . " = " . $sub_val . "\n"; 
    } 
  } 
}

The above code loops through the array, but this line of code:

echo $sub_key . " = " . $sub_val . "\n";

gives me:

step_no = 1 description = Ensure that you have sufficient balance step_no = 2 description = Approve the request sent to your phone

when I change it to:

echo $sub_val . "\n"; 

it gives me:

1 Ensure that you have sufficient balance 2 Approve the request sent to your phone

But I what I truly want is:

1. Ensure that you have sufficient balance 
2. Approve the request sent to your phone

Is this possible at all? Thanks.

3 Answers 3

1
$instructions = array (
    array("step_no"=>"1","description"=>"Ensure that you have sufficient balance"),
    array("step_no"=>"2","description"=>"Approve the request sent to your phone")
);

foreach($instructions as $instruction) {
    echo $instruction['step_no'] . '. ' . $instruction['description'] . "\n";
}

If it's HTML you may want to use <ol> and <li>.

Sign up to request clarification or add additional context in comments.

1 Comment

this was fast, woow, very fewer lines too. Suited my needs perfectly. Thanks.
1

It smells like you are not running this script in command line but in browser. If so, then \n makes no visual effect (unless within <pre> block) and you u must use HTML tag <br /> instead. Also, drop concatenation madness and use variable substitution:

echo "{$sub_key}. = {$sub_val}<br/>";

1 Comment

Ok. Thanks for the advice.
1

You can simple achieve this way

<?php
    $instructions = array (
                           array("step_no"=>"1","description"=>"Ensure that you have sufficient balance"),
                           array("step_no"=>"2","description"=>"Approve the request sent to your phone")
);

   foreach($instructions as $instruction){

       echo $instruction['step_no'].'. '.$instruction['description'].PHP_EOL;
   }
?>

Alway keep it simple.

1 Comment

I'm a huge fan of keeping codes simple, I'm not very good at all with loops and arrays, that's why I browsed a lot of sites just to come up with that code, it seemed to work, that's why I decided to use it with trying to make further adjustments to it to avoid spoiling it.

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.