1

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.

2 Answers 2

2

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"
}
Sign up to request clarification or add additional context in comments.

8 Comments

How are you doing $json_array[$i]["code"] == $codes[$i]["code"] While both arrays doesn't have the same length? You need to loop each code in json and compare it in all the codes in the php array
It depends on what he exatly wants to do, he can also foreach one of two arrays and make all the custom checks
This also doesn't work because json_decode() will decode to an object by default, and it wasn't explictly told to convert to an associative array.
@pavelbere have you tested the function above? you are comparing the arrays based on $i not based on code!!!! for example if the first code is 1 in json array and 1 exists on php array on number 4, you will have empty comparison and false result!
@Burhan Kashour I just suggest an idea of how could it work, I don't know what exactly author needs, obviously it's not a working solution, is someone want use it he should adapt the code for specific case
|
1

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"
}
*/

2 Comments

Do I have to input the entire data from the 2 files in my new PHP file? Or can i work with require/inlcude?
I'm not sure I fully understand your question. But if your input data (the array and the JSON) reside in different places, then you are free to import them using either include() or require().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.