comparing two associative array

Discussion in 'PHP' started by c_user, Sep 27, 2013.

  1. c_user

    c_user New Member

    Joined:
    Aug 23, 2009
    Messages:
    86
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    Php dev
    Location:
    Bhubaneswar
    Hello friends,
    Say i am having two array : ex1 and ex2 (both are associative array)
    Can i compare array ex1 's value with array ex2 's key ?
    Here is the example code i am trying to implement but ..
    Code:
    <?php 
    
    $marks=array('Odiy'=>'52' , 'Urdu'=>'69');
    
    
    
    $postt=array ( 0=> array('id'=>'6', 'class_ref'=>'2nd' , 'subject_name'=>'Odiy') , 1=> array('id'=>'7', 'class_ref'=>'2nd' , 'subject_name'=>'Urdu') );
    
    foreach($postt as $foo)
    {
    foreach($marks as $s)
    {
    if($foo['subject_name']==$s)
    {
    	echo "Hello";	
    }
    else 
    echo "NOT right";
    }
    }
    var_dump($postt);
    var_dump($marks);
    ?>
    Show it in example ?
    Thanks for the reply friends.
    Take Care !
     
    Last edited: Sep 27, 2013
  2. c_user

    c_user New Member

    Joined:
    Aug 23, 2009
    Messages:
    86
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    Php dev
    Location:
    Bhubaneswar
    Lets me thanks http://php.net for giving me the base for R&D.
    And the most importantly to @Shabbir sir ; for letting me know that above stated query is possible.

    Coming to the solution part;
    U cannot compare the Key of one array with the value of another array.
    If u want to then u have to first make array whose values will the key of the array u are wanting to compare.
    Then compare the two values of the array (one array's value are the key for another value ).

    PHP:
    <?php 

    $marks
    =array('Odiy'=>'52' 'Urdu'=>'69');

    $postt=array ( 0=> array('id'=>'6''class_ref'=>'2nd' 'subject_name'=>'Odiy') , 1=> array('id'=>'7''class_ref'=>'2nd' 'subject_name'=>'Urdu') );

    $keys=array_keys($marks);  // Here u are changing the keys to values of another array.

    foreach($postt as $foo)
    {
    foreach(
    $keys as $s)
    {
    if(
    $foo['subject_name']==$s)
    {
        echo 
    "Hello"."<br>";    
    }
    else 
    continue;
    }
    }

    ?>
    The above prints:
    Hello
    Hello
    ..................................................................
    This is what i got if u have any please share.
    Thanking you.
    Good day.
     
    Last edited: Sep 28, 2013
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Nicely done :D
     
  4. c_user

    c_user New Member

    Joined:
    Aug 23, 2009
    Messages:
    86
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    Php dev
    Location:
    Bhubaneswar
    Found another way of bypassing it.
    PHP:
    <?php 

    $marks
    =array('Odiy'=>'52' 'Urdu'=>'69');

    $postt=array ( 0=> array('id'=>'6''class_ref'=>'2nd' 'subject_name'=>'Odiy') , 1=> array('id'=>'7''class_ref'=>'2nd' 'subject_name'=>'Urdu') );

    $keys=array_keys($marks);

    for(
    $i=0;$i<count($postt);$i++)
    {
    for(
    $j=0;$j<count($keys); $j++)
    {
        if(
    $j==$i)
        {
    echo 
    $postt[$i]['subject_name'];
    echo 
    $keys[$j];
        echo 
    "<br>";    
    }
    else 
    continue;
    }
    }


    var_dump($marks);
    var_dump($postt);
    var_dump($keys);
    ?>
    We can compare the index of the array using 'for' loop .
    Did a small change that to make the the index of the marks turn to integer.
    LoVe playing !
    Take care !
     
    shabbir likes this.
  5. alia123

    alia123 New Member

    Joined:
    Jan 8, 2016
    Messages:
    65
    Likes Received:
    5
    Trophy Points:
    0
    Hey, try this :
    Code:
    <?php
      $a = array(
        "who" => "you", 
        "what" => "thing", 
        "where" => "place",
        "when" => "hour"
      );
      // the haystack array
      $b = array(
        "when" => "time", 
        "where" => "place", 
        "who" => "you",
        "what" => "thing"
      );
      $c = count(array_intersect($a, $b));
      echo $c;
    ?> 
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice