I recently started experimenting with multidimensional arrays in PHP and I was wondering what is the most effective way to check if a value exists in a 2D matrix array.
Example: I want to find if user_id = 7 exists in the 2d matrix array. How can I achieved that ?
$matrix = array();
$query = "SELECT
CAST(AVG(r.rating_value) AS DECIMAL(10,1)) as 'rating',
u.user_id,
s.store_id
FROM odr o
INNER JOIN rating r ON o.rating_id = r.rating_id
INNER JOIN store s ON o.store_id = s.store_id
INNER JOIN user u ON o.user_id = u.user_id
GROUP BY s.store_id, u.user_id
ORDER BY rating_value;";
$result = $mysqli->query($query);
$rowcount = mysqli_num_rows($result);
if ($rowcount > 1) {
while ($row = $result->fetch_array()) {
$matrix[$row['user_id']][$row['store_id']] = $row['rating'];
}
}
var_dump($matrix);
//output:
//array(2) { [41]=> array(1) { [35]=> string(3) "5.0" } [37]=> array(2) {
//[37]=> string(3) "5.0" [35]=> string(3) "5.0" } }