-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

Tom
  • 1,436
  • 24
  • 50
  • 4
    `foreach` is too fancy…? – deceze Aug 21 '17 at 12:52
  • 1
    In before _"No of course I know about foreach, but my actual scenario is more complex"_. – CodeCaster Aug 21 '17 at 12:58
  • No need for a down-vote here. OP stated problem and tried code. Title is slightly misleading though. – Progrock Aug 21 '17 at 13:02
  • 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](https://stackoverflow.com/questions/9816889/how-to-echo-an-array-in-php) or [that one](https://stackoverflow.com/questions/5672796/display-array-values-in-php). So while I'm not saying this *should* be downvoted, I think downvoting it isn't wrong either. – domsson Aug 21 '17 at 13:06
  • Possible duplicate of [Display array values in PHP](https://stackoverflow.com/questions/5672796/display-array-values-in-php) – domsson Aug 21 '17 at 13:07
  • 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. – Progrock Aug 21 '17 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. – deceze Aug 21 '17 at 13:14
  • For some reason I'd never considered a foreach loop. I was massively over complicating this. – Tom Aug 22 '17 at 09:51

7 Answers7

1

Just a foreach loop should do it:

foreach ($info as $set => $range) {
    echo $set . ' ' . $range['start'] . ' - ' . $range['end'] . '<br />';
}
OK sure
  • 2,617
  • 16
  • 29
  • Thanks for some reason I'd not thought to use foreach. I think I was overcomplicating this. – Tom Aug 22 '17 at 09:51
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
Progrock
  • 7,373
  • 1
  • 19
  • 25
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.

Rahul
  • 18,271
  • 7
  • 41
  • 60
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
Jigar Shah
  • 6,143
  • 2
  • 28
  • 41
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>';
}
Sarkouille
  • 1,275
  • 9
  • 16
  • 1
    @domsson we need a foreach for the answers on this one. – OK sure Aug 21 '17 at 13:02
  • @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. – Sarkouille Aug 21 '17 at 13:02
  • 1
    Oh come on @ksjohn uses commas for multiple echo params instead of concatenating strings. Totally different! – Progrock Aug 21 '17 at 13:15
  • `.` 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. – Sarkouille Aug 21 '17 at 13:31
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.

Jean-Luc Aubert
  • 620
  • 5
  • 19
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";
}
Progrock
  • 7,373
  • 1
  • 19
  • 25