Reputation: 13
First Post, any help is appreciated.
I have a PHP file (Array) with data from a database, which i need to compare to a JSON file (Object). By looking through forums i have seen stuff about jQuery and AJAX requests, so i was thinking maybe i need to use those for my solution.
The PHP Array
<?php
$codes = [
[
'code' => '1111',
'name' => 'Management',
],
[
'code' => '1305',
'name' => 'Price',
],
[
'code' => '1161',
'name' => 'Service',
]
and the array goes on.
The JSON Object
[{"name":"Management","code":"1111","accountingArea":"3194","managerUsername":null},
{"name":"Storage","code":"9033","accountingArea":"3194","managerUsername":null}]
is the way the JSON Object is formatted. For some reason it has no name, which i don't know if it's a problem, when it comes to comparing two different files.
The product will need to be a PHP script that tells the user which entries are missing in the other file.
The only thing that needs to be compared are the codes.
I have not done anything with JSON files yet and am quite new to PHP too. So far i have only included both the files in my script file and don't know where to start things off.
Upvotes: 1
Views: 316
Reputation: 972
You can take JSON from file as string and use php json_decode()
function that function will return a php array, then you can just make a foreach cicle and check the codes
Your code should be similar to this
<?php
$codes = [
[
'code' => '1111',
'name' => 'Management',
],
[
'code' => '1305',
'name' => 'Price',
],
[
'code' => '1161',
'name' => 'Service',
]];
$json_str = '[{"name":"Management","code":"1111","accountingArea":"3194","managerUsername":null},
{"name":"Management","code":"11141","accountingArea":"3194","managerUsername":null},
{"name":"Management","code":"1305","accountingArea":"3194","managerUsername":null},
{"name":"Management","code":"1161","accountingArea":"3194","managerUsername":null},
{"name":"Storage","code":"9033","accountingArea":"3194","managerUsername":null}]';
$json_array = json_decode($json_str, true);
$result = array();
$codesN = array_column($codes, 'code');
$i = 0;
for($i; $i < count($json_array); $i++) {
if(in_array($json_array[$i]["code"], $codesN)) {
$result[] = $json_array[$i]["code"];
}
}
var_dump($result);
This will return:
array(3) {
[0]=>
string(4) "1111"
[1]=>
string(4) "1305"
[2]=>
string(4) "1161"
}
Upvotes: 2
Reputation: 2714
Try this. See comments for step-by-step explanation.
Output:
array(2) {
[1]=>
string(4) "1305"
[2]=>
string(4) "1161"
}
Code:
<?php
// Your input array.
$codes = [
[
'code' => '1111',
'name' => 'Management',
],
[
'code' => '1305',
'name' => 'Price',
],
[
'code' => '1161',
'name' => 'Service',
]
];
// Your input JSON.
$json = '[{"name":"Management","code":"1111","accountingArea":"3194","managerUsername":null},{"name":"Storage","code":"9033","accountingArea":"3194","managerUsername":null}]';
// Get differences:
// Get values from $codes that are not present in $json.
// Switch the arguments to perform the inverse comparison.
$diff = array_diff(
array_column($codes, 'code'),
array_column(json_decode($json, TRUE), 'code')
);
var_dump($diff);
/*
Output:
array(2) {
[1]=>
string(4) "1305"
[2]=>
string(4) "1161"
}
*/
Upvotes: 1