1

Firstly, sorry for my bad english
I've got a problem with comparing multidimensional array values

Here is my code that i want to solve:

$bla1 = array( array(0.1,0.5), array(0.1,0.5) )
$bla2 = array( array(0.5,0.1), array(0.5,0.1) )
$bla3 = array( array(0.1,0.5), array(0.5,0.1) )    

  if(check_every_single_values_in_array($bla1){
      //Any command in here will not be executed / return false
      }
  if(check_every_single_values_in_array($bla2){
      //Any command in here will not be executed / return false
      }
  if(check_every_single_values_in_array($bla3){
      //Any command in here will not be executed / return false
      }

Any type-juggling and different values will not execute any command.
Otherwise, if there is no any type-juggling and different values it will execute a command:

$bla4 = array( array(0.5,0.5), array(0.5,0.5) )
$bla5 = array( array(1,1), array(1,1) )

  if(check_every_single_values_in_array($bla4){
     //Any command in here will be executed / return true
     }
  if(check_every_single_values_in_array($bla5){
     //Any command in here will be executed / return true
     }

I've tried to solve it with array_diff or some logical and arithmetic operator
and none of them working
My question is, How to compare all values in multidimensional array? And what is the best, shortest, and fastest way code to solve it?

Newbie123
  • 123
  • 1
  • 2
  • 13
  • `I've tried to solve it with array_diff or some logical and arithmetic operator` What code did you come up with to try and make it work with this? – SubjectCurio Jul 07 '14 at 07:57
  • like this `if($a[0] == $a[1]){echo 'bla';}` or this `if($a[0][0] == $a[0][1] == $a[1][0] == $a[1][1]){echo 'bla';}` – Newbie123 Jul 07 '14 at 08:00
  • Read about floats comparison: http://stackoverflow.com/q/3148937/1503018 – sectus Jul 07 '14 at 08:01
  • ok, i'll read it. But, what if the values are in integer? – Newbie123 Jul 07 '14 at 08:03
  • @Unknown just compare it with `==` – sectus Jul 07 '14 at 08:15
  • i've tried it, `if($a[0][0] == $a[0][1] == $a[1][0] == $a[1][1]){echo 'bla';}` it will produce an error, if i change the `==` operator in the middle into `||` then, every result will produce the echo command, even every values are different. – Newbie123 Jul 07 '14 at 08:25

1 Answers1

1

Alternatively, you can do something like this by using serialize():

$bla1 = array(array(0.35,0.5), array(0.35,0.5));
$bla2 = array(array(103.5,0.1), array(103.5,0.1));
$bla3 = array(array(0.1,0.5), array(0.5,0.1));

function check_every_single_values_in_array($array) {
    $same = true;
    $check = array_map('unserialize', array_unique(array_map('serialize', $array)));
    if(count($check) > 1) {
        $same = false;
    }
    return $same;
}

if(check_every_single_values_in_array($bla1)) {
    echo 'Every values on bla1 is same <br/>';
}
if(check_every_single_values_in_array($bla2)) {
    echo 'Every values on bla2 is same <br/>';
}
if(check_every_single_values_in_array($bla3)) {
    echo 'Every values on bla3 is same';
} else {
    echo 'Every values on bla3 is not same';
}
user1978142
  • 7,946
  • 3
  • 17
  • 20
  • This method is working great, thank you :), but i'm afraid that the serialize function will change the float number even it will be unserialized again :\ – Newbie123 Jul 07 '14 at 11:59
  • @Unknown if you don't want serialization, you could also change it into `json_encode`, then `json_decode` also – user1978142 Jul 07 '14 at 12:12
  • wow amazing kevin, the performance is increased by more than 300%, before = `4.8922801017761 µs` after = `1.458083152771 µs` (iteration = 100000) problem solved! :) – Newbie123 Jul 07 '14 at 12:36
  • Thank you kevin, but I'm so sorry kevin for my error. I have realized that your answer is not that what I want. When I saw your code few days ago, I thought all of the `$bla1` `$bla2` and `$bla3` will not echo anything. Suddenly today, I've found a bug in my code and then I checked your code. Only the `$bla3` that won't echo anything. So that's why the bug happened. What I need is code that all of the `$bla` variables above will not echo anything. Would you like to fix your code for me, kevin? – Newbie123 Jul 10 '14 at 14:26
  • @Newbie123 what do you mean? can you paraphrase? if you dont want an echo then remove the echo inside the ifs. or what you are saying that you're tying to `echo check_every_single_values_in_array($bla)`? normally, trying to echo a boolean (`echo true` == 1, `echo false` (no result)) – user1978142 Jul 10 '14 at 23:54
  • i mean, all of the if statement above must return `false` value, in other words `if(check_every_single_values_in_array($bla1)) { ANY COMMAND //This command MUST NOT be executed }else{ ANY COMMAND //This command MUST be executed}`, with your code only the if `$bla3` statement will return false and execute the command inside the else statement – Newbie123 Jul 11 '14 at 05:06
  • @Newbie123 then write an else statement on all of them – user1978142 Jul 11 '14 at 05:48
  • Thank you for all your attention kevin, but that's not what i mean, please check my updated question above. – Newbie123 Jul 11 '14 at 09:37
  • hello Kevin? I'm still waiting for you -_- – Newbie123 Jul 15 '14 at 14:44