PHP将数组转换为字符串

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

Just a foreach loop should do it:

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

Here is the code,

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

Its simple foreach loop to traverse through your array.

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

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>';
}
<?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
", $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

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 />
;
}
echo $result;

Think it will do the job.

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'] . "
";
});

But as you can see the foreach is even simpler.

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