0

this array is a product of a process which i push to this array and when i use nested foreach and echo it gives me all the values from it.

foreach ($arr as $key) {
        foreach ($key as $keys => $values) {
            echo $values;
        }

My question is how can assign it in variable so that I can distinguish where it belongs

Array
(
    [logs] => Array
        (
            [0] => 2014-09-22 01:24:56
            [1] => 2014-09-22 10:53:35
            [2] => 2014-09-22 07:49:45
            [3] => 2014-09-22 06:49:29
        )

    [fullname] => Array
        (
            [0] => DORIS JOHNSON
            [1] => JOHN DOE
            [2] => JOHN DOE
            [3] => JOHN DOE
        )

    [id] => Array
        (
            [0] => 785739
            [1] => 404150
            [2] => 404150
            [3] => 404150
        )

    [misc] => Array
        (
            [0] => Etc
            [1] => Other
            [2] => Etc
            [3] => Etc
        )

    [status] => Array
        (
            [0] => MARRIED
            [1] => SINGLE
            [2] => SINGLE
            [3] => SINGLE
        )

)

I need to make and use the keys as a variable like if i need to output it.

echo $logs;
echo $fullname;
echo $id;
echo $misc;
echo $status;

expected output:

2014-09-22 01:24:56 | DORIS JOHNSON | 785739 | Etc | MARRIED

2014-09-22 10:53:35 | JOHN DOE | 404150 | Other | SINGLE

and soon...
  • Possible duplicate of [How 'foreach' actually works](http://stackoverflow.com/questions/10057671/how-foreach-actually-works) and [How can use foreach to Loop Through PHP Array](https://stackoverflow.com/questions/3027719/how-can-use-foreach-to-loop-through-php-array). – jww Oct 04 '14 at 04:09
  • 2
    Your array structure is a little odd. Shouldn't you be holding the different elements for each record in one array, with each array containing a new record? It'll be a whole lot easier to work with. –  Oct 04 '14 at 04:15

2 Answers2

1

Simple and nice way to 'rotate' array, without foreach loops

array_unshift($arr, null);
$arr = call_user_func_array('array_map', $arr);   

And than just output it anyway you want.

foreach($arr as $s)
    echo join(' | ', $s) . '<br>';
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Cheery
  • 16,063
  • 42
  • 57
0

You can do it like

foreach ($arr['logs'] as $key => $val) {        
    //make a new index and put related elements into it
    $arr['result'][$key] = array();
    array_push($arr['result'][$key], $arr['logs'][$key], $arr['fullname'][$key], $arr['id'][$key], $arr['misc'][$key]);
}

foreach($arr['result'] as $val) {
    //loop through the new index and echo the result in provided format
    echo implode(" | ",$val)."<br/>";
}
Mithun Satheesh
  • 27,240
  • 14
  • 77
  • 101