0

What am I trying to get is..

I do not know check_mobile_exists( $mobiles, $electronics ) this function is returning true all time. I want true when $mobile array key is existed in the $electronics key list and false when not existed. On this result basis I am looping the post result. But as I said that function is returning true on every iteration. Any one to suggest me what should I do now?

function check_mobile_exists(  $mobiles, $electronics ) {
    foreach( $mobiles as $mobile => $price ) {
        if( array_key_exists( $mobile, $electronics ) ) {
            return true;
        }
        return false;
    }
}

$mobiles = array(
    'iPhone' => '$350',
    'tablet' => '$100',
    'samsung' => '$200',
    'walton'  => '$60'
);

$electronics = array(
    'iPhone' => 'OK',
    'tablet' => 'will do',
    'walton' => 'No need'
);

while( have_posts() ): the_post();
    if( check_mobile_exists(  $mobiles, $electronics ) ){ // returning only true on every iterating
        echo get_the_title() .' do something....<br />';
    } else {
        echo 'Do another....';
    }
endwhile;
Dez
  • 5,702
  • 8
  • 42
  • 51
Chayan Biswas
  • 566
  • 2
  • 6
  • 18
  • When you want to return `true`? if all `mobiles` in `electronics` array? – Jyothi Babu Araja Dec 24 '16 at 13:10
  • Thanks for your ans. I want to return true when the keys of `$mobile` is existed in `$electronics` and that time in while loop "do something" will be executed. And when key of $mobile will not existed in `$electronics` it will return false and while loop will execute "Do another ..". That means 1. true ===> "do something" 2. true ===> "do something" 3. false ====> "Do another " ... 4. true ==> "do something".. in this way as the array exists in my example. @JyothiBabuAraja – Chayan Biswas Dec 24 '16 at 13:48
  • Okay, I updated answer according to your need. Check it out. – Jyothi Babu Araja Dec 24 '16 at 13:56

2 Answers2

0

Try this

$mobiles = array(
    'iPhone' => '$350',
    'tablet' => '$100',
    'samsung' => '$200',
    'walton'  => '$60'
);
$electronics = array(
    'iPhone' => 'OK',
    'tablet' => 'will do',
    'walton' => 'No need'
);

while( have_posts() ): the_post();
  foreach( $mobiles as $mobile => $price ) {
    if( array_key_exists( $mobile, $electronics ) ) {
        echo get_the_title() .' do something....<br />';
    }else{
        echo 'Do another....';
    }   
  }
endwhile;

UPDATED: So as you said, without using another function you can do this.

Jyothi Babu Araja
  • 10,076
  • 3
  • 31
  • 38
0

Do it witout check mobile function

$mobiles = array(
    'iPhone' => '$350',
    'tablet' => '$100',
    'samsung' => '$200',
    'walton'  => '$60'
);
$electronics = array(
    'iPhone' => 'OK',
    'tablet' => 'will do',
    'walton' => 'No need'
);




while( have_posts() ): the_post();
  foreach ( $mobiles as $m_key => $mobile ) {
    if ( array_key_exists( $m_mey, $electronics) ) { 
        do somethin
    } else { 
        do somethin
    }
}
endwhile;
DivineCoder
  • 702
  • 5
  • 11
  • Thanks for your ans. But this is not iterating in while loop. I mean if intersection is not empty it should return true and then in while loop it will print "do something".So for first two key of `$mobile` it will return true and print "do something" at third one will be returned false and should print "Do another". Please can you check it? – Chayan Biswas Dec 24 '16 at 13:54
  • And if not Mobile in electronics : Do another.. This way I want it. – Chayan Biswas Dec 24 '16 at 14:04
  • The check_mobile function is then a little lame here do it this way i have updated the answer. What do you think ? – DivineCoder Dec 24 '16 at 14:08
  • But this is giving this result. Please [Check It](https://s30.postimg.org/xf1dmmwkh/dfddfs.png) – Chayan Biswas Dec 24 '16 at 14:31
  • I see, I have created a working example here https://repl.it/EwfJ/0. Can you confirm the results ? Can you explain what is expected ? – DivineCoder Dec 25 '16 at 15:27