0

I'm trying to find difference between these two arrays by doing:

 $linkedAritclesToBeRemoved = array_diff($oldLinkedArticleIds, $linkedArticlesIds);
$linkedArticlesToBeAdded = array_diff($linkedArticlesIds, $oldLinkedArticleIds);

The above code throws a warning Notice: Array to string conversion, This warning is because my one array is single array and the other is multidimensional. So i want to remove this warning message and i want the arrays to be compatible for array_diff function! How can I do it?

Note: I know it's a notice but i want to remove this.

New Array (linkedArticlesIds):

array(5) {
  [0]=>
  string(1) "6"
  [1]=>
  string(1) "2"
  [2]=>
  string(1) "1"
  [3]=>
  string(1) "7"
  [4]=>
  string(1) "8"
}

Old Array( $oldLinkedArticleIds ):

array(3) {
  [0]=>
  array(6) {
    ["id"]=>
    int(30)
    ["article_id"]=>
    int(1)
    ["linked_article_id"]=>
    int(6)
    ["order_by"]=>
    NULL
    ["created_at"]=>
    string(19) "2019-01-03 00:00:00"
    ["updated_at"]=>
    string(19) "2019-01-03 00:00:00"
  }
  [1]=>
  array(6) {
    ["id"]=>
    int(31)
    ["article_id"]=>
    int(1)
    ["linked_article_id"]=>
    int(2)
    ["order_by"]=>
    NULL
    ["created_at"]=>
    string(19) "2019-01-03 00:00:00"
    ["updated_at"]=>
    string(19) "2019-01-03 00:00:00"
  }
  [2]=>
  array(6) {
    ["id"]=>
    int(32)
    ["article_id"]=>
    int(1)
    ["linked_article_id"]=>
    int(1)
    ["order_by"]=>
    NULL
    ["created_at"]=>
    string(19) "2019-01-03 00:00:00"
    ["updated_at"]=>
    string(19) "2019-01-03 00:00:00"
  }
}
DojoDev
  • 95
  • 1
  • 11

1 Answers1

1

If you just need to compare the article ID's within your $oldLinkedArticleIds array, you need to extract the ID column. You can easily do this with array_column()...

$oldIds = array_column($oldLinkedArticleIds, "id");

and use this new list in your comparisons...

$linkedAritclesToBeRemoved = array_diff($oldIds, $linkedArticlesIds);
$linkedArticlesToBeAdded = array_diff($linkedArticlesIds, $oldIds);
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • Thanks `$oldIds = array_column($oldLinkedArticleIds, "linked_article_id");` i was in need of `linked_article_id` – DojoDev Jan 03 '19 at 13:37