сортировать массив по отступам - PullRequest
0 голосов
/ 01 апреля 2019

У меня есть текущий массив в PHP, который извлекается из базы данных MySQL (PDO fetch all);

[0] => Array
    (
        [id] => 78
        [indent] => 2.2
        [partnumber] => OPPN-39-596
        [description] => BACK WASH NOZZLE - 32 DIA
        [size] => 
        [rev] => A
        [code] => MCH
        [quantity] => 1
        [total] => 1
        [notes] => 
        [partcost] => 
        [totalcost] => 
    )

[1] => Array
    (
        [id] => 23
        [indent] => 2.3
        [partnumber] => OPPN-21-508
        [description] => FRONT 12MM JET ASSY
        [size] => 
        [rev] => A
        [code] => ASY
        [quantity] => 3
        [total] => 3
        [notes] => 
        [partcost] => 
        [totalcost] => 
    )

[2] => Array
    (
        [id] => 17
        [indent] => 2.3.1
        [partnumber] => OPPN-21-356
        [description] => INTERNAL CIRCLIP Ø42 BORE S/S
        [size] => 
        [rev] => 
        [code] => HRD
        [quantity] => 1
        [total] => 3
        [notes] => 
        [partcost] => 
        [totalcost] => 
    )

[3] => Array
    (
        [id] => 26
        [indent] => 2.3.2
        [partnumber] => OPPN-21-509
        [description] => FRONT JET 12MM NOZZLE
        [size] => 
        [rev] => A
        [code] => MCH
        [quantity] => 1
        [total] => 3
        [notes] => 
        [partcost] => 
        [totalcost] => 
    )

[4] => Array
    (
        [id] => 28
        [indent] => 2.3.3
        [partnumber] => OPPN-21-510
        [description] => FLOW STRAIGHTENER 12MM INSERT 1
        [size] => 
        [rev] => A
        [code] => MCH
        [quantity] => 1
        [total] => 3
        [notes] => 
        [partcost] => 
        [totalcost] => 
    )

[5] => Array
    (
        [id] => 30
        [indent] => 2.3.4
        [partnumber] => OPPN-21-511
        [description] => FLOW STRAIGHTENER 12MM INSERT 2
        [size] => 
        [rev] => A
        [code] => MCH
        [quantity] => 1
        [total] => 3
        [notes] => 
        [partcost] => 
        [totalcost] => 
    )

[6] => Array
    (
        [id] => 34
        [indent] => 2.3.5
        [partnumber] => OPPN-21-524
        [description] => O RING 3.53 X 32.92 ID NITRILE
        [size] => 
        [rev] => A
        [code] => HRD
        [quantity] => 1
        [total] => 3
        [notes] => 
        [partcost] => 
        [totalcost] => 
    )

[7] => Array
    (
        [id] => 76
        [indent] => 2.4
        [partnumber] => OPPN-39-592
        [description] => FRONT 14MM JET ASSY
        [size] => 
        [rev] => A
        [code] => ASY
        [quantity] => 10
        [total] => 10
        [notes] => 
        [partcost] => 
        [totalcost] => 
    )

[8] => Array
    (
        [id] => 67
        [indent] => 2.4.1
        [partnumber] => OPPN-39-581
        [description] => FRONT JET 14MM NOZZLE
        [size] => 
        [rev] => A
        [code] => MCH
        [quantity] => 1
        [total] => 10
        [notes] => 
        [partcost] => 
        [totalcost] => 
    )

Мне нужно придумать код для преобразования массива в этот;

   [0] => Array
    (
        [id] => 78
        [indent] => 2.2
        [partnumber] => OPPN-39-596
        [description] => BACK WASH NOZZLE - 32 DIA
        [size] =>
        [rev] => A
        [code] => MCH
        [quantity] => 1
        [total] => 1
        [notes] =>
        [partcost] =>
        [totalcost] =>
    )

[1] => Array
    (
        [id] => 23
        [indent] => 2.3
        [partnumber] => OPPN-21-508
        [description] => FRONT 12MM JET ASSY
        [size] =>
        [rev] => A
        [code] => ASY
        [quantity] => 3
        [total] => 3
        [notes] =>
        [partcost] =>
        [totalcost] =>
        [0] => Array
            (
                [id] => 17
                [indent] => 2.3.1
                [partnumber] => OPPN-21-356
                [description] => INTERNAL CIRCLIP Ø42 BORE S/S
                [size] =>
                [rev] =>
                [code] => HRD
                [quantity] => 1
                [total] => 3
                [notes] =>
                [partcost] =>
                [totalcost] =>
            )
        [1] => Array
            (
                 [id] => 26
                 [indent] => 2.3.2
                 [partnumber] => OPPN-21-509
                 [description] => FRONT JET 12MM NOZZLE
                 [size] =>
                 [rev] => A
                 [code] => MCH
                 [quantity] => 1
                 [total] => 3
                 [notes] =>
                 [partcost] =>
                 [totalcost] =>
              )
        [2] => Array
              (
                 [id] => 28
                 [indent] => 2.3.3
                 [partnumber] => OPPN-21-510
                 [description] => FLOW STRAIGHTENER 12MM INSERT 1
                 [size] =>
                 [rev] => A
                 [code] => MCH
                 [quantity] => 1
                 [total] => 3
                 [notes] =>
                 [partcost] =>
                 [totalcost] =>
              )
        [3] => Array
              (
                 [id] => 30
                 [indent] => 2.3.4
                 [partnumber] => OPPN-21-511
                 [description] => FLOW STRAIGHTENER 12MM INSERT 2
                 [size] =>
                 [rev] => A
                 [code] => MCH
                 [quantity] => 1
                 [total] => 3
                 [notes] =>
                 [partcost] =>
                 [totalcost] =>
              )
        [4] => Array
              (
                 [id] => 34
                 [indent] => 2.3.5
                 [partnumber] => OPPN-21-524
                 [description] => O RING 3.53 X 32.92 ID NITRILE
                 [size] =>
                 [rev] => A
                 [code] => HRD
                 [quantity] => 1
                 [total] => 3
                 [notes] =>
                 [partcost] =>
                 [totalcost] =>
              )
    )

[2] => Array
    (
        [id] => 76
        [indent] => 2.4
        [partnumber] => OPPN-39-592
        [description] => FRONT 14MM JET ASSY
        [size] =>
        [rev] => A
        [code] => ASY
        [quantity] => 10
        [total] => 10
        [notes] =>
        [partcost] =>
        [totalcost] =>
        [0] => Array
            (
                [id] => 67
                [indent] => 2.4.1
                [partnumber] => OPPN-39-581
                [description] => FRONT JET 14MM NOZZLE
                [size] =>
                [rev] => A
                [code] => MCH
                [quantity] => 1
                [total] => 10
                [notes] =>
                [partcost] =>
                [totalcost] =>
            )
    )

    [3] => Array
       (
            [id] => 76
            [indent] => 2.5
            [partnumber] => OPPN-39-592
            [description] => FRONT 14MM JET ASSY
            [size] =>
            [rev] => A
            [code] => ASY
            [quantity] => 10
            [total] => 10
            [notes] =>
            [partcost] =>
            [totalcost] =>
       )

Элемент массива с отступом 2.3.1 становится дочерним элементом элемента с отступом 2.3 и т. Д. Мне нужно придумать код, который автоматически форматирует этот массив так, как мне нужно , Я целый день боролся с многомерными петлями, но мне не повезло. Любое руководство и помощь будут оценены.

Ответы [ 2 ]

0 голосов
/ 01 апреля 2019

Я собрал этот код, который будет работать до тех пор, пока ваш верхний уровень всегда равен «x.x», например «1.0», а не просто «1», затем я проверил его, используя ваши данные как переменную $values. Надеюсь, это поможет.

<code>//An array for all the grouped elements
$grouped = array();

foreach( $values as $key=>$properties ){
    //Get the first two figures (and D.P) of the indent
    $indent = substr( $properties['indent'], 0, 3 );

    //Does this indent number already exist in the 
    if( !in_array( $indent, array_keys( $grouped ) ) ){
        //Create a new parent element
        $grouped[ $indent ] = $properties;
    } else {
        //Create a new child element
        $grouped[ $indent ][] = $properties;
    }
}

//Replace indent keys with numeric keys
$grouped = array_values( $grouped );

//Display
echo '<pre>' . print_r( $grouped, 1 ) . '
';
0 голосов
/ 01 апреля 2019

Я создал фрагмент, тесно связанный с вашим требованием, посмотрите.

$result = [];
foreach ($inputArr as $key => $value) {
    $indentArr = explode('.', $value['indent']);
    if (count($indentArr) == 2) {
        $result[$value['indent']] = $value;
    } else {
        unset($indentArr[2]);
        $parentIndent = implode('.',$indentArr);
        $result[$parentIndent][] = $value;
    }
}

Демо .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...