1

I have the following Array...

Array
(
    [main] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [name_one] => banana
                    [name_two] => 
                )

            [1] => Array
                (
                    [id] => 2
                    [name_one] => orange
                    [name_two] => banana
                )

            [2] => Array
                (
                    [id] => 3
                    [name_one] => 
                    [name_two] => orange
                )

            [3] => Array
                (
                    [id] => 4
                    [name_one] => pear
                    [name_two] => 
                )
            [4] => Array
                (
                    [id] => 5
                    [name_one] => pear
                    [name_two] => mango
                )
            [5] => Array
                (
                    [id] => 6
                    [name_one] => 
                    [name_two] => 
                )

        )

)

the logic behind is the following, If the field "name_two" is not empty ignor "name_one" and count only "name_two". If the field "name_two" is empty the take "name_one" and count. If both are empty..No count.

The result from the above array should look like the following table...

------------------------------
category   |  total
------------------------------
banana     |    2
-----------------------------
orange     |    1
-----------------------------
pear       |    1
-----------------------------
mango      |    1
-----------------------------
Total      |    5

I have tried different method including foreach, for and while loop in php but no luck...How do i achieve the table above in php?

if it is easy, the array structure is changed? you are most welcome to change as you like and i can adapt it here. The main issue is generating the table from the array look like the above structure.

1 Answer 1

1

Something like this will do what you want. It processes each element of the array, looking for a valid name_two value, or if not, then a valid name_one value, and increments the count of that value.

$counts = array();
foreach ($array['main'] as $item) {
    if (!empty($item['name_two'])) {
        @$counts[$item['name_two']]++;
    }
    elseif (!empty($item['name_one'])) {
        @$counts[$item['name_one']]++;
    }
}
foreach ($counts as $key => $count) {
    echo "$key: $count\n";
}

Output:

banana: 2
orange: 1 
pear: 1 
mango: 1

Demo on 3v4l.org

Note I use @ in front of the increment statements to avoid "Undefined index" errors when a name is first encountered. If you don't want to do that, you can use the ?? (NULL coalescing operator) in PHP7 e.g.

$counts[$item['name_two']] = ($counts[$item['name_two']] ?? 0) + 1;

or in PHP prior to 7

$counts[$item['name_two']] = (isset($counts[$item['name_two']]) ? $counts[$item['name_two']] : 0) + 1;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.