Mina Magdy
Mina Magdy

Reputation: 141

how to echo this multi-dimensional as following

please help me with a code. I have this multi-dimensional array inside it array 52, 55, 54 - array 52 has 2 arrays show the duplicated funcionario_id key and others have one for each because the output has no duplicates for their key.

note: this multi-dimensional is dynamic, this example show 4 arrays but can be more than that.

now I need to show the value of pedido_data_emitir respect the grouping of arrays as following

the array that need to be echo:

$new_array = Array(
[52] => Array
    (
        [0] => stdClass Object
            (
                [pedido_id] => 54
                [cliente_id] => 5
                [funcionario_id] => 52
                [pedido_forma_de_pagto_id] => 2
                [pedido_data_emitir] => 2015-12-16 13:07:19
            )

        [1] => stdClass Object
            (
                [pedido_id] => 52
                [cliente_id] => 5
                [funcionario_id] => 52
                [pedido_forma_de_pagto_id] => 2
                [pedido_data_emitir] => 2015-12-16 13:07:32
            )

    )

[55] => Array
    (
        [0] => stdClass Object
            (
                [pedido_id] => 51
                [cliente_id] => 7
                [funcionario_id] => 55
                [pedido_forma_de_pagto_id] => 2
                [pedido_data_emitir] => 2015-12-16 13:07:28
            )

    )

[54] => Array
    (
        [0] => stdClass Object
            (
                [pedido_id] => 53
                [cliente_id] => 6
                [funcionario_id] => 54
                [pedido_forma_de_pagto_id] => 2
                [pedido_data_emitir] => 2015-12-16 13:07:36
            )

    )

)

what i need is echo like that:

array 52 =>pedido_data_emitir,pedido_data_emitir
<hr/>
array 54 =>pedido_data_emitir
<hr/>
array 55 =>pedido_data_emitir
<hr/>

with normal foreach its get wrong echo

array 52 =>pedido_data_emitir
<hr/>
array 52 =>pedido_data_emitir
<hr/>
array 54 =>pedido_data_emitir
<hr/>
array 55 =>pedido_data_emitir
<hr/>

Upvotes: 1

Views: 47

Answers (4)

Eduardo Escobar
Eduardo Escobar

Reputation: 3389

I don't know if i have understood you well. Anyway, use nested foreach:

$tmp_array = array();

foreach($new_array as $index => $array) {
  foreach($array as $object) {
    array_push($tmp_array, $object->pedido_data_emitir);
  }
  $new_array[$index] = implode(', ', $tmp_array);
  $tmp_array = array();
}

After that, $new_array has what you want

Upvotes: 0

Mina Magdy
Mina Magdy

Reputation: 141

Sean thank you, i just added this code to change the array from stdClass to normal one and its working fine.

$array_modi = json_decode(json_encode($new_array), true);

Upvotes: 0

trincot
trincot

Reputation: 350137

You could do it this way with array_map and implode:

foreach($new_array as $key => $element) {
    echo "array $key => "
         . implode(", ", array_map(
                   function ($o) { return $o->pedido_data_emitir; }, $element))
         . "<hr/>";
}

Output:

array 52 => 2015-12-16 13:07:19, 2015-12-16 13:07:32
<hr/>
array 55 => 2015-12-16 13:07:28
<hr/>
array 54 => 2015-12-16 13:07:36
<hr/>

Upvotes: 2

Sean
Sean

Reputation: 12433

You need to do a nested loop inside your outer loop, ie.

foreach($new_array as $key => $values){

    // will output 'array 52 => '
    echo "array {$key} => ";

   // create var to hold the pedido_data_emitir string
   $pedido_data_emitir = "";

    foreach($values as $value){
        // add each pedido_data_emitir value to the string
        $pedido_data_emitir .= $value['pedido_data_emitir'].", ";
    }

    // remove the trailing ', '
    echo rtrim($pedido_data_emitir,", ");


    echo "<hr/>";
}

Upvotes: 1

Related Questions