3

i am trying to count a result of a array i get back from a script. there are two cases that i can get back option a is

Array ( [Id] => 1779 [SupplierId] => 1809 [SupplierName] => cccccc)

And second option ib

Array ( 
[0] => Array ( [Id] => 2020 [SupplierId] => 1809 [SupplierName] => vvv) 
[1] => Array ( [Id] => 2058 [SupplierId] => 1809 [SupplierName] => bbb) 
[2] => Array ( [Id] => 2063 [SupplierId] => 1809 [SupplierName] => xx) 
)

if i do count in to cases i get back 3 How can i count that in option A i get back 1, and in option B i will get back 3 ?

1
  • Dani Evan Rubenstein check the answers and mark+up-vote one as accepted answer. You can up-vote others too, if they are useful.Thanks Commented Nov 7, 2016 at 12:50

3 Answers 3

2

You can create a function like this:

function countRes($arr) {
    return is_array(end($arr)) ? count($arr) : 1;
}

$arr1 = array("Id" => 1779, "SupplierId" => 1809, "SupplierName" => "cccccc");

$arr2 = array(array("Id" => 2020, "SupplierId" => 1809, "SupplierName" => "vvv"),
              array("Id" => 2058, "SupplierId" => 1809, "SupplierName" => "bbb"),
              array("Id" => 2063, "SupplierId" => 1809, "SupplierName" => "xx"));

echo countRes($arr1); // 1
echo countRes($arr2); // 3

It checks if the last element is an array, and returns the number of arrays in the resulting array. Otherwise it returns 1 because the resulting array itself contains the data.

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

5 Comments

what happen if the case is :- $arr2 = array(array("Id" => 2020, "SupplierId" => 1809, "SupplierName" => "vvv"), array("Id" => 2058, "SupplierId" => 1809, "SupplierName" => "bbb"),"Id" => 2063); ? end($arr) will not work i think?
Well, OP did not give a case like that.
yes i know, but i think a good answer need to cover max possibility. BTW your answer is correct for OP senario. +1
@Anant Yeah that would be a better answer. But I'll have to wait for OP's response about this. If he does expect results like that. Anyway thanks. :)
@DaniEvanRubenstein check the answers and mark+up-vote one as accepted answer. You can up-vote others too, if they are useful.Thanks
0

First, a comment. Methods should always return the same type of object. If you can modify the code of the script to always return an array of arrays, please do so.

If you cannot, then hacky solutions are in order.

You can use the typeof operator of php to find if the elements in the first array are integers or more arrays:

http://php.net/manual/es/function.gettype.php

Comments

0
$array1 = array ( 'Id' => 1779, 'SupplierId' => 1809, 'SupplierName' => 'cccccc');

$array2 = array(
        array ( 'Id' => 1779, 'SupplierId' => 1809, 'SupplierName' => 'cccccc'),
        array ( 'Id' => 1779, 'SupplierId' => 1809, 'SupplierName' => 'cccccc'),
        array ( 'Id' => 1779, 'SupplierId' => 1809, 'SupplierName' => 'cccccc')
    );

// sub arrays with key
$array3 = array( 
    'first'=>array ( 'Id' => 1779, 'SupplierId' => 1809, 'SupplierName' => 'cccccc'), 
    'second'=>array ( 'Id' => 1779, 'SupplierId' => 1809, 'SupplierName' => 'cccccc'), 
    'third'=>array ( 'Id' => 1779, 'SupplierId' => 1809, 'SupplierName' => 'cccccc') 
    );


echo arrayCount($array3);


function arrayCount($array){
    foreach($array as $key=>$value){
        if(is_array($value)){
            return count($array);
        }else{
            return 1;
        }
    }
}

1 Comment

what happen if the second array is associative multi-dimenaional array? $array[0] will not work. for example:-$array2 = array( 'first'=>array ( 'Id' => 1779, 'SupplierId' => 1809, 'SupplierName' => 'cccccc'), 'second'=>array ( 'Id' => 1779, 'SupplierId' => 1809, 'SupplierName' => 'cccccc'), 'third'=>array ( 'Id' => 1779, 'SupplierId' => 1809, 'SupplierName' => 'cccccc') );

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.