Home > Back-end >  PHP - Count number of values in a specific index of multidimensional array
PHP - Count number of values in a specific index of multidimensional array

Time:06-08

I have a multidimensional array where I need to get the count of similar values in a specific index and create a new array with the results. I am using PHP 7.3.

For example, I need to count the number of similar values in index [3] .

Desired results MUFA-D = 2, BRD-IS = 3, JBC-BAK-B = 1.

[0] => Array
    (
        [0] => CHICAGO
        [1] => 14
        [2] => MUFFIN A LINE
        [3] => MUFA-D
        [4] => Arm Bearings - Check for play, lubricate if needed. 
    )

[1] => Array
    (
        [0] => CHICAGO
        [1] => 14
        [2] => MUFFIN A LINE
        [3] => MUFA-D
        [4] => Linkage - Check for wear and broken links. 
    )

[2] => Array
    (
        [0] => MEMPHIS
        [1] => 05
        [2] => BREADING LINE 1
        [3] => BRD1-IS
        [4] => Gear Box oil level
    )

[3] => Array
    (
        [0] => MEMPHIS
        [1] => 05
        [2] => BREADING LINE 1
        [3] => BRD1-IS
        [4] => Bearings visual inspection
    )

[4] => Array
    (
        [0] => MEMPHIS
        [1] => 05
        [2] => BREADING LINE 1
        [3] => BRD1-IS
        [4] => Electrical Plug condition
    )

[5] => Array
    (
        [0] => CHICAGO
        [1] => 02
        [2] => JBC LINE 2
        [3] => JBC-BAK-B
        [4] => Plate separator shaft
    )

)

CodePudding user response:

Using a couple of useful builtin functions you can do

$in = [
        ['CHICAGO', '14', 'MUFFIN A LINE', 'MUFA-D', 'Arm Bearings - Check for play, lubricate if needed.'],
        ['CHICAGO', '14', 'MUFFIN A LINE', 'MUFA-D', 'Linkage - Check for wear and broken links.'],
        ['MEMPHIS', '05', 'BREADING LINE 1', 'BRD1-IS', 'Gear Box oil level'],
        ['MEMPHIS', '05', 'BREADING LINE 1', 'BRD1-IS', 'Bearings visual inspection'],
        ['MEMPHIS', '05', 'BREADING LINE 1', 'BRD1-IS', 'Electrical Plug condition'],
        ['CHICAGO', '02', 'JBC LINE 2', 'JBC-BAK-B', 'Plate separator shaft']
];

function grab3($occ)
{
    return $occ[3];
}

print_r(array_count_values(array_map('grab3', $in)));

And the result is an array where the values are now the key of an array and the value is the count


Array
(
    [MUFA-D] => 2
    [BRD1-IS] => 3
    [JBC-BAK-B] => 1
)

CodePudding user response:

You can just loop over the array and use the indexes value as a key in an associative array to count. Then $result will contain your desired results.

$data = [
    ['CHICAGO', '14', 'MUFFIN A LINE', 'MUFA-D', 'Arm Bearings - Check for play, lubricate if needed.',],
    ['CHICAGO', '14', 'MUFFIN A LINE', 'MUFA-D', 'Linkage - Check for wear and broken links.',],
    ['MEMPHIS', '05', 'BREADING LINE 1', 'BRD1 - IS', 'Gear Box oil level',],
    ['MEMPHIS', '05', 'BREADING LINE 1', 'BRD1 - IS', 'Bearings visual inspection',],
    ['MEMPHIS', '05', 'BREADING LINE 1', 'BRD1 - IS', 'Electrical Plug condition',],
    ['CHICAGO', '02', 'JBC LINE 2', 'JBC - BAK - B', 'Plate separator shaft',],
];

$result = [];
foreach ($data as $item) {
    $index = $item[3];
    $result[$index] = isset($result[$index]) ? $result[$index]   1 : 1;
}

print_r($result);

Will print

Array
(
    [MUFA-D] => 3
    [BRD1 - IS] => 4
    [JBC - BAK - B] => 2
)

CodePudding user response:

You could use this function on your condition.

function countSimilarValuesByIndex(array $MyArray,int $Index){
    $SimilarValues = [];
    foreach($MyArray as $Object){
        $SimilarValues[$Object[$Index]] = (isset($SimilarValues[$Object[$Index]]))?   $SimilarValues[$Object[$Index]]:1;
    }
    return $SimilarValues;
}

$MyArray = [
    ['CHICAGO', 14, 'MUFFIN A LINE', 'MUFA-D', 'Arm Bearings - Check for play, lubricate if needed.'],
    ['CHICAGO', 14, 'MUFFIN A LINE', 'MUFA-D', 'Linkage - Check for wear and broken links.'],
    ['MEMPHIS', 05, 'BREADING LINE 1', 'BRD1-IS', 'Gear Box oil level'],
    ['MEMPHIS', 05, 'BREADING LINE 1', 'BRD1-IS', 'Bearings visual inspection'],
    ['MEMPHIS', 05, 'BREADING LINE 1', 'BRD1-IS', 'Electrical Plug condition'],
    ['CHICAGO', 02, 'JBC LINE 2', 'JBC-BAK-B', 'Plate separator shaft']
];
$Index = 1;

$YourResult = countSimilarValuesByIndex($MyArray,$Index);

Result:

[  
    [14] => 2,
    [5] => 3,
    [2] => 1
]
  • Related