How to insert a new associated key within PHP Multidimensional Array

Asked

Viewed 42 times

-1

The array has the following structure below:

    $array = [
        [['total_arrematado' => 27, 'id' => 5],['total_arrematado' => 10, 'id' => 7]],
        [['total_arrematado' => 50, 'id' => 8],['total_arrematado' => 60, 'id' => 15]]
    ];



Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [total_arrematado] => 27
                    [id] => 5
                )

            [1] => Array
                (
                    [total_arrematado] => 10
                    [id] => 7
                )
        )
    [1] => Array
        (
            [0] => Array
                (
                    [total_arrematado] => 50
                    [id] => 8
                )

            [1] => Array
                (
                    [total_arrematado] => 60
                    [id] => 15
                )
        )
)

How it would be possible to create a new key dynamically inside each array to get the following structure:

    Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [total_arrematado] => 27
                    [id] => 5
                    [total_itens] => 100
                )

            [1] => Array
                (
                    [total_arrematado] => 10
                    [id] => 7
                    [total_itens] => 100
                )
        )
    [1] => Array
        (
            [0] => Array
                (
                    [total_arrematado] => 50
                    [id] => 8
                    [total_itens] => 100
                )

            [1] => Array
                (
                    [total_arrematado] => 60
                    [id] => 15
                    [total_itens] => 100
                )
        )
)

I tried with two foreachs, but it doesn’t work:

   foreach ($array as $key => $linha1) {
        foreach ($linha1 as $k => $r) {
            $linha1[$k]['total_itens'] = 100;
        }   
    }

1 answer

0


That’s a array which has two positions and each position of that array contain two more array, then it has to be two for, this solves your problem:

<?php

$array = [
  [['total_arrematado' => 27, 'id' => 5], ['total_arrematado' => 10, 'id' => 7]],
  [['total_arrematado' => 50, 'id' => 8], ['total_arrematado' => 60, 'id' => 15]]
];

for ($i = 0; $i < count($array); $i++) {
  for ($j = 0; $j < count($array[$i]); $j++) {
    $array[$i][$j]['total_itens'] = 100;
  }
}

Exit:

array(2) {
  [0]=>
  array(2) {
    [0]=>
    array(3) {
      ["total_arrematado"]=>
      int(27)
      ["id"]=>
      int(5)
      ["total_itens"]=>     
      int(100)
    }
    [1]=>
    array(3) {
      ["total_arrematado"]=>
      int(10)
      ["id"]=>
      int(7)
      ["total_itens"]=>
      int(100)
    }
  }
  [1]=>
  array(2) {
    [0]=>
    array(3) {
      ["total_arrematado"]=>
      int(50)
      ["id"]=>
      int(8)
      ["total_itens"]=>
      int(100)
    }
    [1]=>
    array(3) {
      ["total_arrematado"]=>
      int(60)
      ["id"]=>
      int(15)
      ["total_itens"]=>
      int(100)
    }
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.