Multidimensional php array error

Asked

Viewed 71 times

-2

I’m trying to find the difference between multidimensional arrays but presents these errors:

Notice: Array to string Conversion in C: server htdocs System tests.php on line 319

Notice: Array to string Conversion in C: server htdocs System tests.php on line 319

Could someone help me?

$array_1 = array(
    array( 'nome' => 'Alexandre',   'idade' => '65' ),
    array( 'nome' => 'camila',    'idade' => '33' ),
    array( 'nome' => 'Zezinha', 'idade' => '29' ),
    array( 'nome' => 'Rosana',  'idade' => '64' )
);
$array_2 = array(
    array( 'nome' => 'Alexandre',   'idade' => '65' ),
    array( 'nome' => 'camila',    'idade' => '33' )
);
print_r( array_diff_assoc( $array_1, $array_2) );
  • You’re spending two arrays sequential (which have array associative) for a function that compares arrays associative. What I intended to do with this code?

  • @Andersoncarloswoss want to separate the names that are not repeated, have to print Zezinha and Rosana, he does this, but from the above mistake.

  • And what to do with age? It should also be compared?

  • @Andersoncarloswoss I think there is no need, because the age already comes attached to the name, so separating by name, automatically already separates the corresponding age!

  • 1

    But what if there are two Camilas with different ages?

  • @Andersoncarloswoss had not thought about it! So if it is possible to compare also the same name by different age

Show 1 more comment

1 answer

2


The difference between array_diff and array_diff_assoc is that the second compares the index as well. In your case, how are two arrays numerical index (sometimes called sequential) where apparently no matter what index the value is. If Alexandre/65 is the first in a array and the last one on the other makes no difference, it’s the same record, so should utilise array_diff.

However, as stated in official documentation:

Note:

Two elements are considered equal if and only if, (string) $elem1 === (string) $elem2. In words: when the representation of string is the same.

And this is where the problem lies. Its elements are array and cannot be converted to string in this way; and that is what justifies the error message you had:

Notice: Array to string Conversion in ...

Why does language compare values like string? I don’t know, there must be some reason (or not...). To solve this you must do it manually:

$diff = [];

foreach ($array_1 as $arr) {
    if (!in_array($arr, $array_2)) {
        $diff[] = $arr;
    }
}

print_r($diff);

With that the way out would be:

Array
(
    [0] => Array
        (
            [nome] => Zezinha
            [idade] => 29
        )

    [1] => Array
        (
            [nome] => Rosana
            [idade] => 64
        )

)

The function in_array is based on the loose comparison between the elements, ==, but you can use the restricted, ===, if the third parameter, strict, is defined as true.


An alternative is to manually implement the comparison function and use array_udiff:

function compare_array($a, $b)
{
  return $a <=> $b;
}

$diff = array_udiff($array_1, $array_2, 'compare_array');

The result would be the same. With anonymous function would be:

$diff = array_udiff($array_1, $array_2, function ($a, $b) {
  return $a <=> $b;
});
  • Sensational both the explanation and the final result of the code. I didn’t know I needed to do it manually, I thought the arrays functions in the DOC would solve it. Thanks Anderson

  • I was seeing something of the function with callback. And how would I sort by name? Would I print in alphabetical order? As in this case Rosana would come in Dice 0

  • I did it using your example of anonymity , but using uasort uasort($diff, Function($a, $b) { Return $a['name'] > $b['name']; });

  • @Thiagomoreira Yes, in this way. As the arrays are the same size, the operator <=> will compare value to value, then it will sort first by name and, between equal names, sort by age.

  • thanks for the help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.