-1

I have an array named hello as follows,

hello(
    0 => ([name => abc, add=>def, city=>ny,phone=>12345]);
    1 => ([name => pqr, add=>mno, city=>qw,phone=>67890]);
    2 => ([name => abc, add=>def, city=>ny,phone=>14785]);
    3 => ([name => ghi, add=>foo, city=>yu,phone=>258]);
    4 => ([name => jkl, add=>exy, city=>ny,phone=>95145]);
);

It has few elements and that elements are also array itself, like while retring query result. I want to count a perticular name that repeats. Like in hello[0] and hello[2], so my ans should be abc=2.

How can I do that?

Thanks.

2

3 Answers 3

0

Edit 2: Now you want a way to count all of the objects:

$arr = [
    0 => (object) ['name' => 'abc', 'add'=>'def', 'city'=>'ny', 'phone'=>'12345'],
    1 => (object) ['name' => 'pqr', 'add'=>'mno', 'city'=>'qw', 'phone'=>'67890'],
    2 => (object) ['name' => 'abc', 'add'=>'def', 'city'=>'ny', 'phone'=>'14785'],
    3 => (object) ['name' => 'ghi', 'add'=>'foo', 'city'=>'yu', 'phone'=>'258'],
    4 => (object) ['name' => 'jkl', 'add'=>'exy', 'city'=>'ny', 'phone'=>'95145'],
];

function countBy (callable $f, $xs) {
  return array_reduce($xs, function ($acc, $x) use ($f) {
    $y = call_user_func($f, $x);
    $acc[$y] = isset($acc[$y]) ? $acc[$y] + 1 : 1;
    return $acc;
  }, []);
}

json_encode(countBy (function ($x) { return $x->name; }, $arr));
// => { "abc": 2, "pqr": 1, "ghi": 1, "jkl": 1 } 

Edit: You lied about the data types so I changed $x['name'] to $x->name and $x[$attr] to $x->{$name}


You can use array_reduce

array_reduce($arr, function ($acc, $x) {
  return $x->name === 'abc' ? $acc + 1 : $acc;
}, 0);
// => 2

Or write it as a function

function countAttr($attr, $match, $xs) {
  return array_reduce($xs, function ($acc, $x) use ($attr, $match) {
    return $x->{$attr} === $match ? $acc + 1 : $acc;
  }, 0);
}

countAttr('name', 'abc', $arr)(
// => 2
Sign up to request clarification or add additional context in comments.

5 Comments

it will work, but the array is dynamic so, i can't specify " === abc".
Ok I provided a function use too
returns array " Cannot use object of type stdClass as array "
Well you said they were arrays, not stdClass objects. In that case, see my edit.
ok thnks, it works. Now can you tell me if i want to compare all of them with each other and wanted to get final result of all count of all values. thanks again.
0

In this case defining a function would be best so that count for any name cen be get. You can try this -

$array = [
    0 => ['name' => 'abc', 'add'=>'def', 'city'=>'ny','phone'=>'12345'],
    1 => ['name' => 'pqr', 'add'=>'mno', 'city'=>'qw','phone'=>'67890'],
    2 => ['name' => 'abc', 'add'=>'def', 'city'=>'ny','phone'=>'14785'],
    3 => ['name' => 'ghi', 'add'=>'foo', 'city'=>'yu','phone'=>'258'],
    4 => ['name' => 'jkl', 'add'=>'exy', 'city'=>'ny','phone'=>'95145'],
];
function get_name_count($array, $name)
{  
    // filter array for name
    $temp = array_filter($array, function($a) use($name) {
        return ($a['name'] === $name);
    });
    // return count
    return count($temp);
}

var_dump(get_name_count($array, 'abc'));

Output

int(2)

2 Comments

He/she lied about the input data so this will fail for the same reason my original answer did. Nice answer otherwise ^_^
I guess so too. :)
-1
// hello , please use some standard name for array 
// code not tested 
// define variable if required 
$field_name="name";
$value="abc";
$count=0;
echo count_field($field_name,$value);
function count_field($field_name,$value)
{
 foreach($hello as $val)
 {
   if($val[$field_name]==$value)
   {
   $count++;
   }
 }
 return $count;
}

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.