0

This is my array

Array
(
    [2] => Array
        (
            [0] => Array
                (
                    [id] => 2
                    [res_id] => 1
                    [grand_total] => 303.42
                    [time] => 2016-07-28 11:04:38 AM
                    [status] => 0
                )

            [1] => Array
                (
                    [id] => 2
                    [res_id] => 1
                    [grand_total] => 303.42
                    [time] => 2016-07-28 11:04:38 AM
                    [status] => 0
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [res_id] => 1
                    [grand_total] => 303.42
                    [time] => 2016-07-28 11:04:17 AM
                    [status] => 0
                )

        )

)

From this I need sub array count i.e., the array having two indexes such as 2 & 1 from this 2 & 1 there are some nested arrays found such as 0 & 1 for each

Here,I need array count as follows

Array
(
    [2] => Array
        (
            [count] = 2
        )

    [1] => Array
        (
            [count] = 1
        )

)

How should I get this..

Someone help me out of this...

Thank you..

2

2 Answers 2

2

It is very easy foreach your array and use count or sizeof function.

$desiredArray = array();
foreach ($myarray as $key => $value) {
    $desiredArray [$key] ['count'] = sizeof ($value); 
}

print_r ($desiredArray);

The output will be as your desired output

Array
(
    [2] => Array
        (
            [count] = 2
        )

    [1] => Array
        (
            [count] = 1
        )

)
Sign up to request clarification or add additional context in comments.

Comments

1

It's simple, and is better to create new array where you can save count of elements of main array items:

$counts = array();
foreach ($array as $k => $values) {
    $counts[$k] = count($values);
}

print($counts); // gives desired result

Also you don't need to have extra array for the $counts array, what you get is:

array (
    2 => 2,
    1 => 1
)

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.