0

I have a html form that outputs the following array in php

How can I convert

from:

array(
    [products_id|17|187|quantity] => 1
    [products_id|17|187|include] => 0
    [products_id|17|187|attr|25|name] => Lengte
    [products_id|18|188|quantity] => 1
    [products_id|18|188|include] => 1
    [products_id|18|188|attr|25|name] => Lengte
    [products_id|18|188|attr|21|name] => Dikte (D)
    [products_id|18|188|attr|22|name] => Hoogte (H)
    [products_id|18|188|attr|23|name] => Breedte (B)
)

to:

array(
  [products_id] => array(
    [17] => array(
      [187] => array(
        [quantity] => 1
        [include] => 0
        [attr] => array(
          [25] = array(
            [name] => Lengte
            )
          )
        )
      )
    )
    [18] => array(
      [188] => array(
        [quantity] => 1
        [include] => 0
        [attr] => array(
          [25] = array(
            [name] => Lengte
          )
          [21] = array(
            [name] => Dikte (D)
          )
          [22] = array(
            [name] => Hoogte (H)
          )
          [23] = array(
            [name] => Breedte (B)
        )
      )
    )
  )
)

I already found this answer Convert POST to multidimensional array , and using the second answer takes me one step, but how do I get all steps converted?

$result = [];
foreach($productArray as $key => $val){
    $exp = explode("|", $key, 2);
    $result[$exp[0]][$exp[1]] = $val;
}
array(
  [products_id] => array(
    [17|187|quantity] => 1
    [17|187|include] => 0
    [17|187|attr|25|name] => Lengte
    [18|188|quantity] => 1
    [18|188|include] => 1
    [18|188|attr|25|name] => Lengte
    [18|188|attr|21|name] => Dikte (D)
    [18|188|attr|22|name] => Hoogte (H)
    [18|188|attr|23|name] => Breedte (B)
  )
)

This text here is so that I can post my question, as Stack seems to not like the amount code to text ratio. But I really do not know what to add extra.

3
  • How to use the dupe: 3v4l.org/qc9D1 Commented Feb 15, 2020 at 0:55
  • @Nick , Thank you very much. That works perfectly. I searched a lot of questions, but had not found the one you reference. Commented Feb 15, 2020 at 6:52
  • No worries - if I close a question as a dupe I do always try to give a solution as well. Commented Feb 15, 2020 at 6:53

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.