-4

This my array:

$info = array( 
    "setA" => array ( "start" => 0, "end" => 0 ),
    "setB" => array ( "start" => 100, "end" => 300 ),
    "setC" => array ( "start" => 0, "end" => 0 ),
    "setD" => array ( "start" => 500, "end" => 1000 ),
    "setE" => array ( "start" => 0, "end" => 0 ),
    "setF" => array ( "start" => 0, "end" => 0 ),
    "setG" => array ( "start" => 0, "end" => 0 )
);

How can I convert this so an echo command produces:

setA 0 - 0
setB 100 - 300
setC 0 - 0
setD 500 - 1000
setE 0 - 0
setF 0 - 0
setG 0 - 0

I've tried various things including the following, but nothing comes close.

array_walk_recursive($info, function($v) use (&$result) {
                    $result[] = $v;
                });
                echo implode('<br>', $result);

Any ideas ? Thanks

8
  • 4
    foreach is too fancy…? Commented Aug 21, 2017 at 12:52
  • 1
    In before "No of course I know about foreach, but my actual scenario is more complex". Commented Aug 21, 2017 at 12:58
  • 1
    @Progrock the answer is obvious (as long as you know the most basic control structures of PHP) and there are many questions that are near-duplicates, for example this one or that one. So while I'm not saying this should be downvoted, I think downvoting it isn't wrong either. Commented Aug 21, 2017 at 13:06
  • 1
    If you are new to Php and arrays the manual (array and book.array) doesn't exactly make it clear that foreach is the defacto common Php way to iterate through array structures. Commented Aug 21, 2017 at 13:09
  • 2
    With all due respect, but if you haven't come across foreach yet you need to study some sort of introduction to PHP, not ask on SO. Commented Aug 21, 2017 at 13:14

7 Answers 7

1

Just a foreach loop should do it:

foreach ($info as $set => $range) {
    echo $set . ' ' . $range['start'] . ' - ' . $range['end'] . '<br />';
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for some reason I'd not thought to use foreach. I think I was overcomplicating this.
1
<?php
$info = array( 
    "setA" => array ( "start" => 0, "end" => 0 ),
    "setB" => array ( "start" => 100, "end" => 300 ),
    "setC" => array ( "start" => 0, "end" => 0 ),
    "setD" => array ( "start" => 500, "end" => 1000 ),
    "setE" => array ( "start" => 0, "end" => 0 ),
    "setF" => array ( "start" => 0, "end" => 0 ),
    "setG" => array ( "start" => 0, "end" => 0 )
);

foreach($info as $key => $value) {
    printf("%s %d - %d\n", $key, $value['start'], $value['end']);
}

Output:

setA 0 - 0
setB 100 - 300
setC 0 - 0
setD 500 - 1000
setE 0 - 0
setF 0 - 0
setG 0 - 0

Comments

0

Here is the code,

foreach($info as $key => $value){
    echo $key." ". $value['start']." - ".$value['end'];
}

Its simple foreach loop to traverse through your array.

2 Comments

This is exactly the same as the first answer.
Correct! But I would know before posting the answer, I would have never posted.
0

That's simple, use a foreach. This control structure is made precisely to iterate on arrays :

foreach ($info as $key => $val) {
    echo $key, ' ', $val['start'], ' - ', $val['end'], '<br>';
}

4 Comments

@domsson we need a foreach for the answers on this one.
@domsson You are indeed wrong, the answer is present 6 times while I'm writing this, and not 4. That is the kind of things that happens when people are allowed to write on the same page at the same time. ;) EDIT : by looking at the other answers, it seems that you aren't against duplicates when it comes to your comments.
Oh come on @ksjohn uses commas for multiple echo params instead of concatenating strings. Totally different!
. is the concatenating operator while , is the argument separating operator. echo can take more than one parameter. You can either concatenate all the pieces in one string, pass each of them as a distinct argument or a little of both. Since the most notable difference in situations where both ways are equivalent is that concatenations are greedier than multiple arguments, I tend to not concatenate.
0

Just loop through your array :

$result = "";
foreach($info as $key => $content){
    $result .= $key . " ";
    foreach($content as $bounce => $value){
        $result .= $value . "-";
    }
    $result = substr($result, 0, strlen($result) - 1) . "<br />\n;
}
echo $result;

Think it will do the job.

3 Comments

Think? Did you try it? No. If you did you'd have stumbled across the syntax error.
Plus one for the most obtuse answer.
Didn't try... but, except syntax error, the algo is correct... and yes, the line after the second loop is incorrect
0

foreach would be easier to use

$info = array( 
    "setA" => array ( "start" => 0, "end" => 0 ),
    "setB" => array ( "start" => 100, "end" => 300 ),
    "setC" => array ( "start" => 0, "end" => 0 ),
    "setD" => array ( "start" => 500, "end" => 1000 ),
    "setE" => array ( "start" => 0, "end" => 0 ),
    "setF" => array ( "start" => 0, "end" => 0 ),
    "setG" => array ( "start" => 0, "end" => 0 )
);

foreach ($info as $key => $value) {
    echo $key.' '.$value['start'].' - '.$value['end'].'</br>';
}

Output:

setA 0 - 0
setB 100 - 300
setC 0 - 0
setD 500 - 1000
setE 0 - 0
setF 0 - 0
setG 0 - 0

3 Comments

Exactly the same as the other three answers.
if you look at timing, it is also same, update answer :)
Strange html breaking element you've got there. And I see no </br>s in your output.
0

Personally I'd iterate through the array with foreach, but you were almost there with your array_walk example (use array_walk instead of array_walk_recursive).

<?php
$result = [];
array_walk($info, function($v, $k) use (&$result) {
    $result[] = $k . ' ' . $v['start'] . ' - ' . $v['end'];
});
echo implode('<br>', $result);

Outputs:

setA 0 - 0<br>setB 100 - 300<br>setC 0 - 0<br>setD 500 - 1000<br>setE 0 - 0<br>setF 0 - 0<br>setG 0 - 0

You could have skipped building an array and then imploding by echoing out within the array_walk callback.

<?php
array_walk($info, function($v, $k) {
    echo $k . ' ' . $v['start'] . ' - ' . $v['end'] . "\n";
});

But as you can see the foreach is even simpler.

foreach($info as $k => $v) {
    echo $k . ' ' . $v['start'] . ' - ' . $v['end'] . "\n";
}

Comments

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.