I have an array,
echo '<pre>';print_r($assetArray);echo '</pre>';
results…
Array
(
[0] => Array
(
[name] =>
[id] => 1
[beginning_ru] => 35.0
[ending_ru] => 48.0
[beginning_x] => 0.0
[external_width] => 17.500
[external_height] => 24.500
[asset_type] => 1
[manufacturer] => 3Com
[mounting_direction] => Front
)
)
I’d like to break the array into two arrays based on a keys (mounting_direction) value which can either be Front/Back
Would it be something like
<?php
function testFront()
{
if($assetArray['mounting_direction'] == "Front") {
//add into new array
}
}
function testBack()
{
if($assetArray['mounting_direction'] == "Back") {
//add into new array
}
}
$frontAssetArray = (array_filter($assetArray,"testFront"));
$backAssetArray = (array_filter($assetArray,"testBack"));
?>
Drummin
November 17, 2021, 3:32am
2
You can always build a new array with the mounting direction as the primary key.
$SortedAsset = array();
foreach($assetArray as $key => $row):
$SortedAsset[$row['mounting_direction']][$key] = $row;
endforeach;
It should come out like this.
Array
(
[Front] => Array
(
[0] => Array
(
[name] =>
[id] => 1
[beginning_ru] => 35.0
[ending_ru] => 48.0
[beginning_x] => 0.0
[external_width] => 17.500
[external_height] => 24.500
[asset_type] => 1
[manufacturer] => 3Com
[mounting_direction] => Front
)
[1] => Array
(
[name] =>
[id] => 2
[beginning_ru] => 35.0
[ending_ru] => 48.0
[beginning_x] => 0.0
[external_width] => 17.500
[external_height] => 24.500
[asset_type] => 1
[manufacturer] => 3Com
[mounting_direction] => Front
)
[3] => Array
(
[name] =>
[id] => 4
[beginning_ru] => 35.0
[ending_ru] => 48.0
[beginning_x] => 0.0
[external_width] => 17.500
[external_height] => 24.500
[asset_type] => 1
[manufacturer] => 3Com
[mounting_direction] => Front
)
[6] => Array
(
[name] =>
[id] => 7
[beginning_ru] => 35.0
[ending_ru] => 48.0
[beginning_x] => 0.0
[external_width] => 17.500
[external_height] => 24.500
[asset_type] => 1
[manufacturer] => 3Com
[mounting_direction] => Front
)
)
[Back] => Array
(
[2] => Array
(
[name] =>
[id] => 3
[beginning_ru] => 35.0
[ending_ru] => 48.0
[beginning_x] => 0.0
[external_width] => 17.500
[external_height] => 24.500
[asset_type] => 1
[manufacturer] => 3Com
[mounting_direction] => Back
)
[4] => Array
(
[name] =>
[id] => 5
[beginning_ru] => 35.0
[ending_ru] => 48.0
[beginning_x] => 0.0
[external_width] => 17.500
[external_height] => 24.500
[asset_type] => 1
[manufacturer] => 3Com
[mounting_direction] => Back
)
[5] => Array
(
[name] =>
[id] => 6
[beginning_ru] => 35.0
[ending_ru] => 48.0
[beginning_x] => 0.0
[external_width] => 17.500
[external_height] => 24.500
[asset_type] => 1
[manufacturer] => 3Com
[mounting_direction] => Back
)
)
)
You then use the known KEY to print or loop through that array. Just check for ‘Front’ or 'Back".
if(array_key_exists('Front',$SortedAsset)):
echo "<pre>";
print_r($SortedAsset['Front']);
echo "</pre>";
endif;
1 Like
woaw, thanks!
Is there a way to get a count of how many records in the sortedArray() in the front key and the back?
Drummin
November 17, 2021, 6:03am
4
For the fun of it…
$mounting_directions = array('Front','Back');
foreach($mounting_directions as $direction):
if(array_key_exists($direction,$SortedAsset)):
echo $direction.': ' . count($SortedAsset[$direction]).'<br />';
endif;
endforeach;
rpkamp
November 17, 2021, 7:46pm
5
@lurtnowski It worries me that for someone who asks a lot of questions regarding arrays, you don’t seem to have a good grip on what they are and what you can do with them.
Is there something we can help you with in terms of basics of arrays and the manipulations that one can apply to them?
2 Likes
system
Closed
February 17, 2022, 2:47am
6
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.