You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP对象数组去重及元素出现次数统计实现方法

Hey there! Let's figure out how to solve this problem. You've got an array of PHP stdClass objects, and you want to count occurrences of each optionAnswer value, then output a string like test 2 good 1. Here are a couple of solid approaches:

解决方案

方法一:基础循环统计(直观易懂)

This method uses a simple loop to iterate through your object array and build up a count of each answer. It's great if you prefer clear, step-by-step code.

// Your original array of stdClass objects
$objectArray = [
    (object) ["id" => "148", "questionResId" => "76", "optionText" => "some Cute Moment.", "optionId" => "30", "optionAnswer" => "test"],
    (object) ["id" => "142", "questionResId" => "72", "optionText" => "some Cute Moment.", "optionId" => "30", "optionAnswer" => "test"],
    (object) ["id" => "221", "questionResId" => "136", "optionText" => "some Cute Moment.", "optionId" => "30", "optionAnswer" => "Good"]
];

// Initialize an empty array to hold counts
$answerCounts = [];

// Loop through each object to count answers
foreach ($objectArray as $obj) {
    // Convert answer to lowercase to match your desired output (case-insensitive count)
    $answer = strtolower($obj->optionAnswer);
    
    if (isset($answerCounts[$answer])) {
        // If the answer exists, increment its count
        $answerCounts[$answer]++;
    } else {
        // If it's the first time, set count to 1
        $answerCounts[$answer] = 1;
    }
}

// Build the output string
$output = '';
foreach ($answerCounts as $answer => $count) {
    $output .= "$answer $count ";
}

// Trim extra trailing space and echo the result
$output = trim($output);
echo $output; // Output: test 2 good 1

关键细节:

  • We use strtolower() to make the count case-insensitive (so "Good" and "good" are treated the same, which aligns with your sample output). If you need case-sensitive counting, just remove this line.
  • The $answerCounts array uses answer values as keys and their occurrence counts as values.

方法二:函数式写法(更简洁)

If you prefer more concise code, you can use PHP's array functions like array_reduce() and array_map() to achieve the same result in fewer lines:

$objectArray = [
    (object) ["id" => "148", "questionResId" => "76", "optionText" => "some Cute Moment.", "optionId" => "30", "optionAnswer" => "test"],
    (object) ["id" => "142", "questionResId" => "72", "optionText" => "some Cute Moment.", "optionId" => "30", "optionAnswer" => "test"],
    (object) ["id" => "221", "questionResId" => "136", "optionText" => "some Cute Moment.", "optionId" => "30", "optionAnswer" => "Good"]
];

// Use array_reduce to count occurrences
$answerCounts = array_reduce($objectArray, function($carry, $obj) {
    $answer = strtolower($obj->optionAnswer);
    $carry[$answer] = ($carry[$answer] ?? 0) + 1;
    return $carry;
}, []);

// Build and output the result string
$output = implode(' ', array_map(function($answer, $count) {
    return "$answer $count";
}, array_keys($answerCounts), $answerCounts));

echo $output; // Output: test 2 good 1

关键细节:

  • array_reduce() iterates through the array and accumulates the count in the $carry variable.
  • The null coalescing operator (?? 0) safely initializes the count to 0 if the answer hasn't been seen yet.
  • array_map() pairs each answer key with its count, then implode() joins everything into a single string.

内容的提问来源于stack exchange,提问作者Sourabh Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:53:09