0

I have an array of arrays and I want to loop through them and create another array. The inner arrays of original array has a property called 'type' and its value is 'invoice'. It can have multiple arrays of type = invoice. I want to populate a new array with this invoice type.

For example:

$originalArray = [
    [
        "type" => "note",
        "format" => 'IMAGE'
    ],
    [
        "type" => "invoice",
        "text" => "boom boom"
    ],
    [
        "type" => "invoice",
        "text" => "bam bam"
    ]
];

$newArray = [];

So in the loop I want to first check if type is invoice. Then check if newArray has any with type invoice. If it doesn't then it will push this to newArray:

[
  "type" => "invoice",
  "values" => [
    [
      "type" => "text",
      "text" => "boom boom",
    ],
  ]
]

if it does exist, then it will just push it to values array:

"values" => [
    [
      "type" => "text",
      "text" => "boom boom",
    ],
    [
      "type" => "text",
      "text" => "bam bam",
    ],
]

the final newArray should look like this:

[
  "type" => "invoice",
  "values" => [
    [
      "type" => "text",
      "text" => "boom boom",
    ],
    [
      "type" => "text",
      "text" => "bam bam",
    ],
  ]
]

This is what I tried so far:

$originalArray = [
    [
        "type" => "note",
        "format" => 'IMAGE'
    ],
    [
        "type" => "invoice",
        "text" => "boom boom"
    ],
    [
        "type" => "invoice",
        "text" => "bam bam"
    ]
];

$newArray = [];

foreach ($originalArray as $arr) {
  
  if ($arr['type'] === 'invoice') {

    if (in_array("invoice", array_column($newArray, "type"))) {
  
       foreach ($newArray as $item) {

          if ($item['type'] === 'invoice') {

              $item['values'][] = [
                'type' => 'text',
                'text' => $arr['text']
              ];
              
          }
        }
     } else {

          $newArray[] = [
            'type' => 'invoice',
            'values' => [
              [
                'type' => 'text',
                'text' => $arr['text'],
              ],
            ],
          ];

      } 
  
  }

}

var_dump($newArray);

The dump only shows one invoice item, the first one. What am I doing wrong here?

s.khan
  • 297
  • 1
  • 7
  • 24
  • 1
    tldr; `foreach` will give you a _copy_ of the array value by default, and changing the copy won't change the original array. If you read the question/answers to the duplicate question above, it explains what's going on and how to solve it. – M. Eriksson Jul 06 '23 at 11:36
  • @M.Eriksson Yes, that thread answers it. The `&` fixed it. – s.khan Jul 06 '23 at 11:56
  • Post it as an answer – s.khan Jul 06 '23 at 11:57
  • 1
    Since there already are (a lot) of answers to this question (like the posted link), this question can either be removed, or it will be closed as a duplicate. For the future, do more research before asking here. This is one of those questions that has been asked and answered sooooo many times before (both here and on many other sites). – M. Eriksson Jul 06 '23 at 12:12
  • I'll remove it now, thanks for your help – s.khan Jul 06 '23 at 12:15

0 Answers0