Check if array key exists in a multidimentional array

I have an array that’s generated using mysql like this:

$conn = connect();
    $stmt = $conn->prepare("select id, type, status from arraytest");
    $stmt->execute();
    $myArray = $stmt->fetchAll();

When I do print_r($myArray), I get this:

Array ( [0] => Array ( [id] => 3 [0] => 3 [type] => 0 [1] => 0 [status] => 0 [2] => 0 ) [1] => Array ( [id] => 6 [0] => 6 [type] => 0 [1] => 0 [status] => 1 [2] => 1 ) ) 

I can also access the array this way:

echo $myArray[0][0]. ' ' . $myArray[0][1]. ' ' . $myArray[0][2];
    echo '<br>';
    echo $myArray[1][0]. ' ' . $myArray[1][1]. ' ' . $myArray[1][2];

The table data is simple:

"id"	"type"	"status"
    "3"	   "0"	    "0"
    "6"	   "0"	    "1"

I need to run the array against a while loop and check if id from the array matches the $i. But how is this done with an array like this? I cant figure out? Can you please help?

while ($i <= 10) {
        echo $i;
    	echo ((array_key_exists($i,$myArray)) ? ' OK <br>' : ' Fail <br>'); // I need to check if $i == id from the array.
        $i++;
    	echo '<br>';
    }

Expected Output

1 Fail
    2 Fail
    3 OK
    4 Fail
    5 Fail
    6 OK
    //and so on

Hi Nordy,

This should give you the expected output:


$i = 1;
foreach($myArray as $row) {
    echo $i . (($row['id'] === $i) ? ' OK' : ' Fail') . '<br>';
    $i++;
}

Here’s my spin:

<?php

$myArray = array(
                  0 => array( 'id' => 3, 'type' => 0, 'status' => 1),
                  1 => array( 'id' => 5, 'type' => 1, 'status' => 0),
				  2 => array( 'id' => 7, 'type' => 1, 'status' => 1),
				  3 => array( 'id' => 9, 'type' => 1, 'status' => 1)
           );
echo '<pre>';
print_r($myArray);
echo '</pre>';


$loc = 1;
foreach($myArray as $row) {
	
	if (array_key_exists('id', $row)) {
		$i = $loc;
		while ($i <= 10) {
			echo $i . (($i == $row['id']) ? ' OK' : ' Fail') . '<br>';
			if ($i == $row['id']) {
				$loc = $i+1;
				break;
			}
			$i++;
		}
	}
}  

I tried the offered solutions and could not get them to work?


$myArray = array
(  
  array( 'id' => 3, 'type' => 0, 'status' => 1), 
  array( 'id' => 5, 'type' => 1, 'status' => 0), 
  array( 'id' => 2, 'type' => 1, 'status' => 1), 
  array( 'id' => 7, 'type' => 1, 'status' => 1), 
  array( 'id' => 4, 'type' => 1, 'status' => 1), 
  array( 'id' => 9, 'type' => 1, 'status' => 1)   
); 

echo 'array(id, type, status)';
  foreach ($myArray as $i => $row):
    $result = ($i===$row['id']) ? ' OK' : ' Fail';

    echo '<p>';
      echo $i .' => ';
      echo 'array(' .$row['id'] .',' .$row['type'] .',' .$row['status'] .')';
      echo ' ==> ' .$result;
    echo '</p>';

  endforeach;


Output:

array(id, type, status)

0 => array(3,0,1) ==> Fail

1 => array(5,1,0) ==> Fail

2 => array(2,1,1) ==> OK

3 => array(7,1,1) ==> Fail

4 => array(4,1,1) ==> OK

5 => array(9,1,1) ==> Fail

This isn’t array_key_exists then is it? As you want to know if the value of an array key matches the current increment.

May I ask why you want to do this?

Wouldn’t it be as simple as

while ($i <= 10)
{
  if ($myArray[$i]['id'] == $i)
    echo "OK";
  else
    echo "Failed";
  echo "<br />";
}

After $myArray = $stmt->fetchAll() but before while ($i <= 10), you could convert the array from having zero-based keys to having the rows’ id values as keys.


// Your DB stuff goes here

$myArray = array_combine(array_map(function($row) { return $row['id']; }, $myArray), $myArray);

// Your while loop goes here

Inside the loop, your array_key_exists() check will work because the array will look like:


array(
  3 => array('id' => '3', ...),
  6 => array('id' => '6', ...),
);

Docs: