Jump to content

Better solution to group an array by an specific key


mapg

Recommended Posts

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

Link to comment
Share on other sites

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 by Psycho
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.