-1

I have this multidimensional array with for loop that I use to breakdown certain number in a function.

However I'd also like to use array_diff_assoc() to get the differences.

for ($i1 = 0; $i1 <= 9; $i1++) {

    $scale_0_to_9[$i1];

    foreach($splitInThreeArray as $key=>$value) {

        if(strlen($value) != '3') {

            $value = '';
            $scale_0_to_9[$key]['9'] = $value;
            $scale_0_to_9[$key]['8'] = $value;
            $scale_0_to_9[$key]['7'] = $value;
            $scale_0_to_9[$key]['6'] = $value;
            $scale_0_to_9[$key]['5'] = $value;
            $scale_0_to_9[$key]['4'] = $value;
            $scale_0_to_9[$key]['3'] = $value;
            $scale_0_to_9[$key]['2'] = $value;
            $scale_0_to_9[$key]['1'] = $value;
            $scale_0_to_9[$key]['0'] = $value;
        }else {
            $num_0 = substr_count($value,'0');
            $num_1 = substr_count($value,'1');
            $num_2 = substr_count($value,'2');
            $num_3 = substr_count($value,'3');
            $num_4 = substr_count($value,'4');
            $num_5 = substr_count($value,'5');
            $num_6 = substr_count($value,'6');
            $num_7 = substr_count($value,'7');
            $num_8 = substr_count($value,'8');
            $num_9 = substr_count($value,'9');

            $scale_0_to_9[$key]['9'] = $num_9;
            $scale_0_to_9[$key]['8'] = $num_8;
            $scale_0_to_9[$key]['7'] = $num_7;
            $scale_0_to_9[$key]['6'] = $num_6;
            $scale_0_to_9[$key]['5'] = $num_5;
            $scale_0_to_9[$key]['4'] = $num_4;
            $scale_0_to_9[$key]['3'] = $num_3;
            $scale_0_to_9[$key]['2'] = $num_2;
            $scale_0_to_9[$key]['1'] = $num_1;
            $scale_0_to_9[$key]['0'] = $num_0;
        }
    }
}

I have this, but it doesn't work.

array_diff_assoc(
    $scale_0_to_9['0'], $scale_0_to_9['1'],
    $scale_0_to_9['2'], $scale_0_to_9['3'],
    $scale_0_to_9['4'], $scale_0_to_9['5']
)

I'm trying to compare the arrays inside $scale_0_to_9 and find at least 2 that have the most similarities.

But it doesn't have to be $scale_0_9['0'] as a starting point.

If for example $scale_0_9['2'] != $scale_0_9['8'] have different values that would work as well.

As long as they don't have the same values.

And I need to know the ['2'] $key because I need to match the two that have different values together.

Nikk
  • 7,384
  • 8
  • 44
  • 90
  • you should provide a sample input and desired output. – goat Jul 16 '16 at 23:42
  • But I don't want to select them individually like this `$scale_0_to_9['1']['5']`. I'd like to select the entire thing under `$scale_0_to_9['1'][]`. Is that possible? – Nikk Jul 16 '16 at 23:43
  • I notice you're using string keys even though the keys are numerical. eg: `$scale_0_to_9[$key]['9']` Is it important that they be strings? Would `$scale_0_to_9[$key][9]` instead be a problem? – BeetleJuice Jul 16 '16 at 23:48
  • If I use `$scale_0_to_9[$key][9]` I will only get `[9]`. I want to get everything under ``$scale_0_to_9[$key]` and compare it to the other `$scale_0_to_9[$key]`. And would like to find at least 2 different ones. – Nikk Jul 16 '16 at 23:53
  • I am trying this atm, `array_intersect($scale_0_to_9['0'],$scale_0_to_9['1'],$scale_0_to_9['2'],$scale_0_to_9['3'],$scale_0_to_9['4'],$scale_0_to_9['5'])` but its not working some of the time. I just need to find 2 that are different. – Nikk Jul 16 '16 at 23:58
  • `call_user_func_array` – Wrikken Jul 17 '16 at 00:04
  • What you're trying to do is difficult to understand. Adding an example input with desired output might be helpful. Are you looking for which values in `$scale_0_9['0']` are not in any of the others? Are you looking for which key-value pairs are not in any of the others? – BeetleJuice Jul 17 '16 at 00:11
  • @BeetleJuice Yes I am looking for which values in `$scale_0_9['0']` are not in any of the others. But it doesn't have to be `$scale_0_9['0']`. If for example `$scale_0_9['2'] & $scale_0_9['8']` have different values that would work as well. As long as they don't have the same values. And I need to know the `['2']` key because I need to match the two that have different values together. – Nikk Jul 17 '16 at 00:14
  • Maybe i'm just slow here but it's confusing to me because I can interpret what you're saying in multiple ways. For instance: "`$scale_0_9['2']` & `$scale_0_9['8']` have different values... As long as they don't have the same values". Does that mean all the values are different? does it mean any single value is different? What if the values are the same but the keys are different such as `$scale_0_9['2']['1']` and `$scale_0_9['2']['2']`? And what would the final desired result look like? Sample input/output in your question would help. – BeetleJuice Jul 17 '16 at 00:25
  • @BeetleJuice I'm not comparing the keys. Only the values. Well it would be ideal they have completely different values. But there will be instances when maybe they'll share a common value but the value should be present at most 1 times in each `$scale_0_9['x']`. It also is not necessary to start with `['0']`. It could be any two `['x']` that have the most differences between them. – Nikk Jul 17 '16 at 00:29

2 Answers2

0
call_user_func_array('array_diff_assoc',$scale_0_to_9['1']);
Wrikken
  • 69,272
  • 8
  • 97
  • 136
  • Or possibly `call_user_func_array('array_diff_assoc',$scale_0_to_9);`, not sure what you're actually trying to do. – Wrikken Jul 17 '16 at 00:07
  • I'm trying to compare the arrays inside `$scale_0_to_9` and find at least 2 that have the least similarities possible. And then match those two together. – Nikk Jul 17 '16 at 00:22
0

The comparison you want is complex, and your results are not well defined. In the OP you state

I'm trying to compare the arrays inside $scale_0_to_9 and find at least 2 that have the most similarities.

And a bit later:

If for example $scale_0_9['2'] != $scale_0_9['8'] have different values that would work as well. As long as they don't have the same values.

In comments you add:

it would be ideal they have completely different values. But there will be instances when maybe they'll share a common value but the value should be present at most 1 times in each $scale_0_9['x']

What you want is too hard to pin down. That said, I've rewritten what you posted to make it much simpler. Everything in your first big block of code around if(strlen($value)!='3' can be trimmed to just a few lines:

$digits = ['0','1','2','3','4','5','6','7','8','9'];

if(strlen($value)!==3){
    //store in each key, an empty string
    foreach ($digits as $d){$scale_0_to_9[$key][$d] = '';}
}else{
    foreach ($digits as $d){
        //store in each key, the # of times the matching digit
        //appears in value
        $scale_0_to_9[$key][$d] = substr_count($value,$d);
    }
}
BeetleJuice
  • 39,516
  • 19
  • 105
  • 165
  • Thx for the shorter version...but it doesn't solve the problem. So for example I have number `231 in [0] => Array ([9] => 0 [8] => 0 [7] => 0 [6] => 0 [5] => 0 [4] => 0 [3] => 1 [2] => 1 [1] => 1 [0] => 0)` and I have other numbers that will be added the same way. Now out of all of those `[x] => Array()`'s I want to find the two that don't match (for starters). Then I will combine them together and get a set of digits that don't repeat. That would be the ideal case, theres exceptions...but I just need a different point of view how this could be done to start with. – Nikk Jul 17 '16 at 02:36
  • Would it work if I implode it back into sting and then add it to a new array thats not multidimensional. And finally run array_diff() ? – Nikk Jul 17 '16 at 03:01
  • @Borsn I really wanted to help, but I simply don't know the answer because I can't wrap my mind around what you're trying to do precisely. I decided to post my code above to help you simplify what you have so far, but I didn't go into finding the `diff` for the reason I just stated. – BeetleJuice Jul 17 '16 at 03:05