0

I don't know if the title matches the theme.

I need from the following array item eigenschaften the entries in a new array in a certain structure:

Array
(
    [2635] => Array
    (
        [produkt_kombi_id] => 2635
        [sort_order] => 1
        [produkt_kombi_artikelnummer] => 2-0811-4LED
        [produkt_kombi_preis_typ] => fix
        [produkt_kombi_preis] => 50.0000
        [eigenschaften] => Array
            (
                [10] => 53
                [11] => 57
                [3] => 7
            )

    )

    [2641] => Array
    (
        [produkt_kombi_id] => 2641
        [sort_order] => 2
        [produkt_kombi_artikelnummer] => 2-0812-4LED
        [produkt_kombi_preis_typ] => fix
        [produkt_kombi_preis] => 50.0000
        [eigenschaften] => Array
            (
                [10] => 53
                [11] => 57
                [3] => 8
            )

    )

    [2647] => Array
    (
        [produkt_kombi_id] => 2647
        [sort_order] => 3
        [produkt_kombi_artikelnummer] => 2-0813-4LED
        [produkt_kombi_preis_typ] => fix
        [produkt_kombi_preis] => 50.0000
        [eigenschaften] => Array
            (
                [10] => 53
                [11] => 57
                [3] => 9
            )

    )
    [2712] => Array
    (
        [produkt_kombi_id] => 2712
        [sort_order] => 24
        [produkt_kombi_artikelnummer] => 2-0812
        [produkt_kombi_preis_typ] => fix
        [produkt_kombi_preis] => 0.0000
        [eigenschaften] => Array
            (
                [10] => 54
                [11] => 59
                [3] => 8
            )

    )

    [2713] => Array
    (
        [produkt_kombi_id] => 2713
        [sort_order] => 25
        [produkt_kombi_artikelnummer] => 2-0813
        [produkt_kombi_preis_typ] => fix
        [produkt_kombi_preis] => 0.0000
        [eigenschaften] => Array
            (
                [10] => 54
                [11] => 59
                [3] => 9
            )

    )

I need a structure like this as new array:

Array (
    [10] => array (
        [53]=> array (
            [11] => array (
                [57] => array (
                    [3] => 7
                )
            )
        ),
       [54]=> array (
            [11] => array (
                [57] => array (
                    [3] => 7
                )
            )
        )
    )
)

Each "key" and "value" from $array['eigenschaften'] must be a new array in the following "key" when "key" not exist.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87

1 Answers1

0

This was a little intriguing so I used my solution from How to access and manipulate multi-dimensional array by key names / path?:

function set($path, &$array=array(), $value=null) {
    $temp =& $array;

    foreach($path as $key) {
        $temp =& $temp[$key];
    }
    $temp = $value;
}

But first we need to get the keys and values into the path array (might be a slicker way but this came first to mind):

foreach($array as $a) {
    $path = array();

    foreach($a['eigenschaften'] as $k => $v) {
        $path[] = $k;
        $path[] = $v;
    }
    $value = array_pop($path);
    set($path, $result, $value);
}

The first function will overwrite values for duplicate paths with different values. To only set the first one you need to check if it is null:

function set($path, &$array=array(), $value=null) {
    $temp =& $array;

    foreach($path as $key) {
        $temp =& $temp[$key];
    }
    if($temp === null) {
        $temp = $value;
    }
}
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87