Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Chris1989's avatar

Compare data from two different sources

Im trying to compare two same keys from different sources (API and sql table) like expenses['vat'] and $phpArray['vat'] and when matching that then fetch only matched data to blade from $expenses query

also want have the ability on matched data to make sql updates to some values from matched @phpArray to $expenses

Is it difficult that? now the test i do i get error: Array to string conversion

Here is the code:

  $xmlObject = simplexml_load_string($response);
                    $json = json_encode($xmlObject);

                    $phpArray = json_decode($json, true);
                   
                   
                    $expenses = Expenses::query()->get();
                  
                   
                    $differenceArray = array_diff($phpArray,$expenses);
                    
                    dd($differenceArray);
                     
0 likes
13 replies
Sinnbeck's avatar

You are getting every single row in the database and want to compare each of them to $phpArray? Or does ($phpArray have a list of vats you want to search? Show what data you are working with

Chris1989's avatar

@sinnbeck

Exact, i want to check every single row, from database compare them to $phparray(data from api), look the blade export to see what i want if i am understandable, https://prnt.sc/vgL5IusLeIZD As you can see on photo , the $phparray is data fetch from api. the first row matching via VAT with the first row of $expenses so when press the btn matched data want only show the $expenses first row that is matched , and have ability to insert the mark field of @phparray inside @expenses matched row.

want to know that the compare condition must have same VAT, same DATE and same TOTAL AMOUNT, sow the matched data always wil be one to one

also i make a dd( $phpArray , $expenses); to see the export of that data

https://prnt.sc/2NvkMv-7ysYh

Sry for my English , hope to understand what i want

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Ok so you want every row that matches a vat

Something like this

$vats = collect($phpArray)->pluck('vat');
$expenses = Expenses::query()->whereIn('vat', $vats)->get();

Edit: oh it needs to match on 3 columns. Is that a unique key, those 3 column?

1 like
Sinnbeck's avatar

Do you actually store totals in the database as a string with €?

Chris1989's avatar

@Sinnbeck With only two single lines code worked haha!, a small problem , to make more than one condition i think its a litle more complicated , i think i have wrong cod eon the expenses, i want something like And where in? is it possible that?

  $vats = collect($phpArray['bookInfo'])->pluck('counterVatNumber','grossValue');
                   
  $expenses = Expenses::query()->whereIn('vat','total_cost', $vats)->get();
                 
  dd($expenses);
Sinnbeck's avatar

@Chris1989 yeah that complicates it alot. You cannot check it using my method. Only one column

That's why I asked if you have a unique key on those 3 columns together

Sinnbeck's avatar

@Chris1989 ok I cannot think of any way to get that data out in a single query. Is the idea to insert missing rows from the api and update the rest or?

Chris1989's avatar

@Sinnbeck yeah specifically if matches data then want insert the column minMark from api inside $expenses (ill create new column for that) ok even with a single column check method how can get that matched data from api to make the sql update? It would be very helpful if you give me an example

Please or to participate in this conversation.