I have the current array in PHP which is being extracted from a MySQL database (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] =>
)
I need to come up with some code to convert the array to this;
[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] =>
)
The array element with an indent of 2.3.1 becomes a child element of the element which has an indent of 2.3 and so on... I need to come up with some code that automatically formats this array to way I need it. I have fighting with multi dimensional for loops all day now but had no luck. Any guidance and assistance would be appreciated.
I have created snippet strongly coupled for your requirement, have a look.
$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;
}
}
Demo.
I have put together this code which will work as long as your top level is always "x.x", for example "1.0" and not just "1" , I then tested it using your given data as $values
variable. Hope this helps.
//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 ) . '</pre>';