This may be a basic question but I am struggling with a way to count values without doing multiple foreach loops. I have this array of objects (partial list included):
array(51) {
[0]=>
object(stdClass)#971 (4) {
["hole"]=>
string(1) "2"
["club"]=>
string(1) "6"
["shot_type"]=>
string(1) "6"
["shot_loc"]=>
string(1) "6"
}
[1]=>
object(stdClass)#970 (4) {
["hole"]=>
string(1) "2"
["club"]=>
string(2) "16"
["shot_type"]=>
string(1) "8"
["shot_loc"]=>
string(1) "1"
}
[2]=>
object(stdClass)#969 (4) {
["hole"]=>
string(1) "2"
["club"]=>
string(2) "19"
["shot_type"]=>
string(1) "3"
["shot_loc"]=>
string(1) "2"
}
[3]=>
object(stdClass)#968 (4) {
["hole"]=>
string(1) "1"
["club"]=>
string(1) "1"
["shot_type"]=>
string(1) "6"
["shot_loc"]=>
string(1) "6"
}
[4]=>
object(stdClass)#967 (4) {
["hole"]=>
string(1) "1"
["club"]=>
string(2) "15"
["shot_type"]=>
string(1) "5"
["shot_loc"]=>
string(1) "3"
}
The number of objects in the list vary but each object will have the key=>values as shown. What I would like to return is an array with count of each of the values of "hole". Something like this:
`array(18) {
[1]=> 4
[2]=> 5
[3]=> 6`
and so on where the key is each of the values of "hole" and the new array value is the count.
I have attempted forms of count, count(get_object_vars($)), and others but all examples I find are counting the objects. Thanks in advance.
Your question is a bit confusing, but this should do the trick for you:
$holes = [];
foreach ($array as $object) {
if (isset($object->hole)) {
$hole = $object->hole;
if (!isset($holes[$hole])) {
$holes[$hole] = 0;
}
$holes[$hole]++;
}
}
I tested it using this:
$object1 = (object)['hole'=>'2'];
$object2 = (object)['hole'=>'3'];
$object3 = (object)['hole'=>'1'];
$object4 = (object)['hole'=>'3'];
$array = [$object1,$object2,$object3,$object4];
$holes = [];
foreach ($array as $object) {
if (isset($object->hole)) {
$hole = $object->hole;
if (!isset($holes[$hole])) {
$holes[$hole] = 0;
}
$holes[$hole]++;
}
}
echo "<pre>";
print_r($holes);
Prints
Array
(
[2] => 1
[3] => 2
[1] => 1
)
It sounds like you want an array of 18 (?) elements returned with the count of number of occurences of each hole.
$new_array = array_reduce($your_array, function ($carry, $item) {
if (property_exists($item, "hole") && array_key_exists($item->hole - 1, $carry)) {
$carry[$item->hole - 1]++;
}
return $carry;
}, array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0));
Related
This question already has answers here:
PHP multidimensional array search by value
(23 answers)
Closed 1 year ago.
I have an $array with build:
array(5) {
[0]=>
array(5) {
["woj"]=>
string(4) "2"
["pow"]=>
string(2) "11"
["gmi"]=>
string(1) "2"
["rodz_gmi"]=>
string(1) "2"
["name"]=>
string(10) "Test1"
}
[1]=>
array(5) {
["woj"]=>
string(1) "2"
["pow"]=>
string(2) "11"
["gmi"]=>
string(1) "2"
["rodz_gmi"]=>
string(1) "2"
["name"]=>
string(8) "test2"
}
[2]=>
array(5) {
["woj"]=>
string(1) "2"
["pow"]=>
string(2) "11"
["gmi"]=>
string(1) "2"
["rodz_gmi"]=>
string(1) "2"
["name"]=>
string(12) "test3"
}
[3]=>
array(5) {
["woj"]=>
string(1) "2"
["pow"]=>
string(2) "11"
["gmi"]=>
string(1) "2"
["rodz_gmi"]=>
string(1) "2"
["name"]=>
string(8) "Test4"
}
[4]=>
array(5) {
["woj"]=>
string(1) "2"
["pow"]=>
string(2) "11"
["gmi"]=>
string(1) "2"
["rodz_gmi"]=>
string(1) "2"
["name"]=>
string(11) "Test5"
}
}
and I want to create a php function, which can be search main array key by value (name).
Example - search main array key by name: test3. Result should be 2, because second main key of $array contain subarray where name key = test3.
I tied with array_search, but with multidimensional array, this function not work.
Thanks for your help.
You may try the following for your multi-dimensional array:
function myCustomSearch($data,$searchVal){
foreach($data as $index => $subArray){
foreach($subArray as $key=>$value){
if($value === $searchVal)return $index;
}
}
return null; //return null if not found
}
eg use assuming your current array shared in question is stored in $data
$resultIndex = myCustomSearch($data,'test3');
Let me know if this works for you.
I have two arrays where indexes of both arrays are contains objects
$array1=array(1) {
[0]=> object(stdClass) (3) {
["aid"]=> string(1) "1"
["a_number"]=> string(1) "0"
["id_of"]=> string(1) "1"
}
}
$array2=array(3) {
[0]=> object(stdClass) (3) {
["id"]=> string(1) "1",
["number"]=> string(1) "0" ,
["flag"]=> string(1) "1" ,
["zflag"]=> string(1) "0" ,
["xflag"]=> string(1) "1"
} ,
[1]=> object(stdClass) (3) {
["id"]=> string(1) "2",
["number"]=> string(1) "2" ,
["flag"]=> string(1) "2" ,
["zflag"]=> string(1) "0" ,
["xflag"]=> string(1) "1"
},
[1]=> object(stdClass) (3) {
["id"]=> string(1) "3",
["number"]=> string(1) "3",
["flag"]=> string(1) "3" ,
["zflag"]=> string(1) "0" ,
["xflag"]=> string(1) "1"
}
}
I want to compare between the value of $id key in all elements of $array2 with the value of $id_of each element of $array1, if it's not exist then return the element of $array1. Below is my code but it doesn't work
public function unanswered($array1,$array2){
if(!(empty($array2))){
$unanswered_arrays=array();
foreach($array1 as $b){
foreach($array2 as $a){
if($b->id != $a->id_of){
array_push($unanswered_arrays,(object)$b);
}
}
}
return $unanswered_arrays;
}
return $array1;
}
If you're using your function as unanswered($array1,$array2) then replace $array1 with $array2 and vice versa in foreach loops or pass unanswered($array2,$array1) instead of.
Demo
guys lets play in multidimensional array little bit :p
lets said i have $filter which it contain multidimensional array :
array(2) {
["time"]=>
array(3) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(1) "3"
}
["people"]=>
array(5) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(1) "3"
[3]=>
string(1) "4"
[4]=>
string(1) "5"
}
}
from the array it should be return :
array(3) {
["time"]=>
string(1) "1"
["people"]=>
string(1) "1"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "1"
["people"]=>
string(1) "2"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "1"
["people"]=>
string(1) "3"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "1"
["people"]=>
string(1) "4"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "1"
["people"]=>
string(1) "5"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "2"
["people"]=>
string(1) "1"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "2"
["people"]=>
string(1) "2"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "2"
["people"]=>
string(1) "3"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "2"
["people"]=>
string(1) "4"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "2"
["people"]=>
string(1) "5"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "3"
["people"]=>
string(1) "1"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "3"
["people"]=>
string(1) "2"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "3"
["people"]=>
string(1) "3"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "3"
["people"]=>
string(1) "4"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "3"
["people"]=>
string(1) "5"
["promo_id"]=>
string(1) "1"
}
actually i could get that result with this following code :
foreach ($filter['time'] as $row){
foreach ($filter['people'] as $item){
$new_array['time'] = $row;
$new_array['people'] = $item;
$new_array['promo_id'] = $promo_id;
vd($new_array,"new");
}
}
but i cant use that following code, because it using $filter['time'] and $filter['people'], how about if the time or people is a random string?
so can you show me guys, to looping it from $filter?
as like this :
foreach ($filter as $key => $row){
}
thank you (:
function combination($arr,$res,$completed_key){
$array_key = array_keys($arr);
$last_array_key = array_pop($array_key);
$start = false;
$temp = $res;
$res=array();
$i=0;
foreach($arr as $key=>$row){
if(!in_array($key,$completed_key)&& $start==false){
array_push($completed_key,$key);
$start=true;
}
if($start){
if(count($temp)==0){
foreach($row as $key2 => $row2){
$res[][$key]=$row2;
}
}else{
foreach($temp as $temp_key=>$temp_row){
foreach($row as $key2 => $row2){
$res[$i]=$temp_row;
$res[$i][$key]=$row2;
$i++;
}
}
}
}
if($start){
break;
}
}
if(!in_array($last_array_key,$completed_key)){
return $this->combination($arr,$res,$completed_key,$debugger);
}else{
return $res;
}
}
This function will return you want.
$filter["time"]=array(1,2,3);
$filter["people"]=array(1,2);
$filter["size"]=array("a","b");
$res=$this->combination($filter,array(),array());
I'll explain later.
How can I fetch the value "3" from this set of arrays:
array(1) { [0]=> string(1) "1" }
array(1) { [0]=> string(1) "3" }
array(1) { [0]=> string(1) "0" }
The arrays are output from a foreach statement of parenting array, which is:
array(3) { [0]=> string(8) "St" [1]=> string(1) "1" [2]=> string(1) "0" }
array(3) { [0]=> string(16) "Fu" [1]=> string(1) "3" [2]=> string(1) "0" }
array(3) { [0]=> string(13) "Pa" [1]=> string(1) "0" [2]=> string(1) "0" }
Where I am going for the second line value: "Fu" [1]=> string(1) "3"
Maybe I am doing it wrong from the first array?
You're not giving us much to go on. Are the 3 arrays already in a parent array, in an object, etc.? Below is how to get the # 3 from the 3 arrays...but I'm guessing this is not actually what you are asking, we likely need much more detail...the real problem you are trying to solve.
function getThree($arr1, $arr2, $arr3) {
$array = array();
$array[] = $arr1;
$array[] = $arr2;
$array[] = $arr3;
foreach( $array AS $subArray ) {
// whichever condition works for you
if( $subArray[0] == 'Fu' || $subArray[1] == 3 ) {
return $subArray;
}
}
}
I grouped an array using the following script
$grouped_array = array();
foreach($ungrouped_array as $item) {
//group them by id
$grouped_array[$item['id']][] = $item;
}
Now this grouped array looks like this
array(3) {
[1]=>
array(2) {
[0]=>
array(1) {
["id"]=>
string(1) "1"
}
[1]=>
array(1) {
["id"]=>
string(1) "1"
}
}
[6]=>
array(1) {
[0]=>
array(1) {
["id"]=>
string(1) "6"
}
}
[2]=>
array(4) {
[0]=>
array(1) {
["id"]=>
string(1) "2"
}
[1]=>
array(2) {
["id"]=>
string(1) "2"
["sub"]=>
string(1) "1"
}
[2]=>
array(2) {
["id"]=>
string(1) "2"
["sub"]=>
string(1) "2"
}
[3]=>
array(1) {
["id"]=>
string(1) "2"
}
}
}
I have deleted the most part of the array to make it shorter but there is no [0] field in this grouped array
All array fields are given the name of [id]'s value. I have no problem with that, I just have to short it again by [ID]
any suggestion will be great.
This should work to get 1, 2, 6:
<?php
$grouped_array = array();
foreach($ungrouped_array as $item) {
$grouped_array[$item['id']][] = $item;
}
// sort by key.
ksort( $grouped_array, SORT_NUMERIC );
print_r( $grouped_array );