Reputation: 2491
Restructuring PHP array data. Testing the "data block 1" works fine and the data is correctly exploded in $newArray_1 and finally sorted in $newArray_2. I do not really use the explode but choosed to kept it in the code since it will be used later to extract the needed name of key within new arrays.
Question: Is it possible to use the same code base in order to solve with the same handling of "data block 2" ?
My code:
// Unsorted array -------------------------------------
$frontendData = [
'datasheet' => [
'Name_1_balance0' => 1, // Data block 1
'Name_1_balance2' => 3, // Data block 1
'Name_1_balance3' => 4, // Data block 1
'Name_1_balance1' => 2, // Data block 1
// 'Name_2_balance0' => 5, // Data block 2
// 'Name_2_balance2' => 7, // Data block 2
// 'Name_2_balance3' => 8, // Data block 2
// 'Name_2_balance1' => 6, // Data block 2
]
];
ksort($frontendData['datasheet']);
print_r($frontendData);
// Explode array -------------------------------------
$newArray_1 = [];
foreach ( $frontendData as $mainKey => $elements ) {
foreach ( $elements as $subKey => $value ){
$newData = explode("_", $subKey);
$newData[] = $value;
$newArray_1[$mainKey][] = $newData;
}
}
print_r($newArray_1);
// Restructure array -------------------------------------
$newArray_2['datasheet'] = ['Name_1'];
for ($i=0; $i <=3 ; $i++) {
$newArray_2['datasheet'][1]['balance'][$i] =
$newArray_1['datasheet'][$i][3];
}
print_r($newArray_2);
Result (Processing "Data block 1"):
Array
(
[datasheet] => Array
(
[0] => Name_1
[1] => Array
(
[balance] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
)
)
)
Wanted result (Processing "Data block 1" & "Data block 2"):
Array
(
[datasheet] =>
Array
(
[0] => Name_1
[1] => Array
(
[balance] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
)
),
Array
(
[0] => Name_2
[1] => Array
(
[balance] => Array
(
[0] => 5
[1] => 6
[2] => 7
[3] => 8
)
)
)
)
Upvotes: 0
Views: 201
Reputation: 147216
You can iterate over your $frontendData
array, using preg_match
to extract the name and balance number (thus avoiding the need to sort the array), and building a result array using those values indexed by the name. We then use array_values
to re-index the array numerically and assign it to a datasheet
key in the output:
$result = array();
foreach ($frontendData['datasheet'] as $key => $balance) {
preg_match('/^(\w+)_balance(\d+)$/', $key, $match);
$result[$match[1]][0] = $match[1];
$result[$match[1]][1]['balance'][$match[2]] = $balance;
}
$result = array('datasheet' => array_values($result));
print_r($result);
Output (for your sample data):
Array
(
[datasheet] => Array
(
[0] => Array
(
[0] => Name_1
[1] => Array
(
[balance] => Array
(
[0] => 1
[2] => 3
[3] => 4
[1] => 2
)
)
)
[1] => Array
(
[0] => Name_2
[1] => Array
(
[balance] => Array
(
[0] => 5
[2] => 7
[3] => 8
[1] => 6
)
)
)
)
)
Note that entries in the balance
array occur in the same order as they are in the original datasheet
array so indexes may not be in numeric order. If that is a concern (for example, if you wanted to iterate the values using foreach
rather than a for
loop over the indexes), you should sort $frontendData['datasheet']
by its keys, using uksort
with a callback of strnatcmp
to ensure that balance10
sorts after balance2
to balance9
:
uksort($frontendData['datasheet'], 'strnatcmp');
Upvotes: 2
Reputation: 504
try this
<?php
// Unsorted array -------------------------------------
$frontendData = [
'datasheet' => [
'Name_2_balance0' => 5, // Data block 2
'Name_1_balance0' => 1, // Data block 1
'Name_1_balance2' => 3, // Data block 1
'Name_1_balance3' => 4, // Data block 1
'Name_1_balance1' => 2, // Data block 1
'Name_2_balance2' => 7, // Data block 2
'Name_2_balance3' => 8, // Data block 2
'Name_2_balance1' => 6, // Data block 2
]
];
ksort($frontendData['datasheet']);
// print_r($frontendData);
// Explode array -------------------------------------
$newArray_1 = array('datasheet'=>array());
$key='';
$i = 0;
foreach ( $frontendData['datasheet'] as $mainKey => $elements ) {
$newData = explode("_", $mainKey);
if(sizeof($newData)>=3){
$newkey=$newData[0].'_'.$newData[1];
if($newkey!=$key){
$key=$newkey;
array_push($newArray_1['datasheet'],array());
array_push($newArray_1['datasheet'][$i], $newkey);
array_push($newArray_1['datasheet'][$i], array());
$k=array();
array_push($k, $elements);
$newArray_1['datasheet'][$i][1]=array('balance' => $k);
$i++;
}else{
array_push($newArray_1['datasheet'][$i-1][1]['balance'], $elements);
}
}
}
echo "<pre>";
print_r($newArray_1);
echo "</pre>";
// Restructure array -------------------------------------
//$newArray_2['datasheet'] = ['Name_1'];
?>
Upvotes: 1