0

I can't sleep well with this one problem. I couldn't find other solution. I have tried some functions, but I can't achieve desirable result with those. So one guy told me to use array_walk(). Yes this function does what I want, but i can't or i don't know how to properly insert it in my code.

My PHP code:

$d=array();
foreach(range(0, 3) as $rs) {
    foreach(range(0, 5) as $c){

        //here is working php code to generate '$randomRs' by '$rs' and '$c' vars..

        $d['rs'.$rs]['c'.$c] = $randomRs;

    }
}

Generated JSON output:

{
"rs0":{
   "c0":"pl",
   "c1":"wd",
   "c2":"wd",
   "c3":"pl",
   "c4":"bl"
},
"rs1":{
   "c0":"gr",
   "c1":"gr",
   "c2":"lk",
   "c3":"gr",
   "c4":"lk"
}
and so on...

All i need is one simple code to count specific values of that array. For example: i need to count how many "wd" and "lk" there is in array. Guy told me to use array_walk(), but i am new with those array's and i really need some advise!

7
  • Why would it matter what function you use? Commented Feb 1, 2017 at 21:49
  • Possible duplicate of Count specific values in multidimensional array Commented Feb 1, 2017 at 21:51
  • @miken32 I have tried those in that thread, but i need to search by value, not by key.. Commented Feb 1, 2017 at 21:57
  • @jeroen dunno. All i need is to count key values. That's it. He told me that I can use array_walk() for that. Commented Feb 1, 2017 at 21:59
  • Count in the entire array or each sub array separately? Commented Feb 1, 2017 at 22:32

1 Answer 1

2

Inside your loop just use:

$temp[] = $randomRs;

Then you can count them easily:

$count = array_count_values($temp);
echo $count['wd'];
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.