SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Check if array key exists in a multidimentional array

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

    Code:
    $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:

    Code:
    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:

    Code:
    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:

    Code:
    "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?

    Code:
    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**

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

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,433
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    Hi Nordy,

    This should give you the expected output:
    PHP Code:
    $i 1;
    foreach(
    $myArray as $row) {
        echo 
    $i . (($row['id'] === $i) ? ' OK' ' Fail') . '<br>';
        
    $i++;


  3. #3
    SitePoint Enthusiast Strider64's Avatar
    Join Date
    Aug 2012
    Posts
    38
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Here's my spin:

    PHP Code:
    <?php

    $myArray 
    = array( 
                      
    => array( 'id' => 3'type' => 0'status' => 1),
                      
    => array( 'id' => 5'type' => 1'status' => 0),
                      
    => array( 'id' => 7'type' => 1'status' => 1),
                      
    => 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++;
            }
        }
    }

  4. #4
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,880
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    I tried the offered solutions and could not get them to work?

    PHP Code:
    $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
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,214
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    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
    PHP Code:
    while ($i <= 10)
    {
      if (
    $myArray[$i]['id'] == $i)
        echo 
    "OK";
      else
        echo 
    "Failed";
      echo 
    "<br />";


  6. #6
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    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.

    Code php:
    // 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:

    Code:
    array(
      3 => array('id' => '3', ...),
      6 => array('id' => '6', ...),
    );
    Docs:
    Salathe
    Software Developer and PHP Manual Author.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •