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.