0

I have an array of strings that $count_per_slot = [ "12", "9", "67", "120", "9", "67", "7" ] and an array of which I will like to compare with the index of $count_per_slot so I can get the value of the index that is equal to $index_to_compare. The array of string to compare with is $index_to_compare = [ "3", "4" ] I was able to get the array of the index of $count_per_slot with the below code:


foreach($count_per_slot as $index => $row){

$index_of_count_per_slot = $row;

}

How do I compare $index_of_count_per_slot to $index_to_compare and any of the index that is true should return the value in $count_per_slot

  • Can't $index_to_compare just contain integers rather than strings. Then you can just use those as indices. – Simon Goater Jan 18 '23 at 14:22
  • Do you mean, you want to get 3th and 4th values from a $count_per_slot array? In this case just use 3 and 4 as indexes: $result = []; foreach ($index_to_compare as $i) { $result[] = $count_per_slot[$i] ?? null; } /* then exclude empty values: */ $result = array_filter($result); – Ilia Yatsenko Jan 18 '23 at 17:13

0 Answers0