mapg Posted December 16, 2019 Share Posted December 16, 2019 Hi all, I would like to ask you if somebody could have a better idea to build this function. The current one is working, but I feel this built a bit in an amateur manner. Feel free to add your ideas so we can learn more about PHP and how to deal with arrays. I have an array: $arrayVideoSpecs = Array( Array( 'aspect' => '4:3', 'density' => '442368', 'resolution' => '768x576' ), Array( 'aspect' => '4:3', 'density' => '307200', 'resolution' => '640x480' ), Array( 'aspect' => '16:9', 'density' => '2073600', 'resolution' => '1920x1080' ), Array( 'aspect' => '16:9', 'density' => '121600', 'resolution' => '1280x720' ) ); and I want an array as output grouped by video aspect ratio where the key is the pixel density and the value is the video resolution, namely like that for aspect ratio 16:9 ... Array ( [2073600] => 1920x1080 [121600] => 1280x720 ) Then I coded this function which is working but seems amateur ... function groupAndExtractByAspect($array, $groupBy, $aspect, $density, $resolution) { $groupByAspect = Array(); foreach ($array as $value) { $groupByAspect[$value[$aspect]][] = Array($value[$density], $value[$resolution]); } $arrayClean = Array(); foreach ($groupByAspect as $key => $value) { if ($key == $groupBy) { $arrayClean[$key] = $value; } } foreach ($arrayClean as $aspectGroup) { $arrayOutput = Array(); for ($i = 0; $i <= count($aspectGroup); $i++) { $densityIsValid = false; $resolutionIsValid = false; if (!empty($arrayClean[$groupBy][$i][0])) { $density = $arrayClean[$groupBy][$i][0]; $densityIsValid = true; } if (!empty($arrayClean[$groupBy][$i][1])) { $resolution = $arrayClean[$groupBy][$i][1]; $resolutionIsValid = true; } if (($densityIsValid === true) && ($resolutionIsValid === true)) { $arrayOutput[$density] = $resolution; } } } return $arrayOutput; } The usage is as follow ... $showArray = groupAndExtractByAspect($arrayVideoSpecs, '16:9', 'aspect', 'density', 'resolution'); echo '<pre>'; print_r($showArray); echo '</pre>'; Thank you very much for your ideas! Mapg Quote Link to comment https://forums.phpfreaks.com/topic/309693-better-solution-to-group-an-array-by-an-specific-key/ Share on other sites More sharing options...
Psycho Posted December 16, 2019 Share Posted December 16, 2019 (edited) Not sure why you have all the unnecessary parameters in the function - unless there is some use I am not seeing. This will produce the same output function groupAndExtractByAspect($inputAry, $aspect) { $outputAry = array(); foreach($inputAry as $key => $dataAry) { //Skip if not the selected aspect if($dataAry['aspect'] != $aspect) { continue; } $outputAry[$dataAry['density']] = $dataAry['resolution']; } return $outputAry; } $showArray = groupAndExtractByAspect($arrayVideoSpecs, '16:9'); echo '<pre>'; print_r($showArray); echo '</pre>'; Alternatively, you could just generate a new multi-dimensional array that create the values for ALL aspect ratios function groupAndExtractByAspect($inputAry, $aspect) { $outputAry = array(); foreach($inputAry as $key => $dataAry) { $outputAry[$dataAry['aspect']][$dataAry['density']] = $dataAry['resolution']; } return $outputAry; } This will produce: Array ( [4:3] => Array ( [442368] => 768x576 [307200] => 640x480 ) [16:9] => Array ( [2073600] => 1920x1080 [121600] => 1280x720 ) ) Edited December 16, 2019 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/309693-better-solution-to-group-an-array-by-an-specific-key/#findComment-1572566 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.