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?

6
  • 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? Commented Jul 7, 2014 at 7: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';} Commented Jul 7, 2014 at 8:00
  • Read about floats comparison: stackoverflow.com/q/3148937/1503018 Commented Jul 7, 2014 at 8:01
  • ok, i'll read it. But, what if the values are in integer? Commented Jul 7, 2014 at 8:03
  • @Unknown just compare it with == Commented Jul 7, 2014 at 8:15

1 Answer 1

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';
}
Sign up to request clarification or add additional context in comments.

9 Comments

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 :\
@Unknown if you don't want serialization, you could also change it into json_encode, then json_decode also
wow amazing kevin, the performance is increased by more than 300%, before = 4.8922801017761 µs after = 1.458083152771 µs (iteration = 100000) problem solved! :)
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 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))
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.