Home > front end >  Comparing array item differences in php
Comparing array item differences in php

Time:08-10

I have the following json string.

$json_data = '{
                "action": "Action1",
                "data": [
                    {
                        "id": "1",
                        "amount": "",
                        "text": "Text 1"
                    },
                    {
                        "id": "2",
                        "amount": 1,
                    },
                    {
                        "channel_id": "20",
                        "amount": "2",
                        "text": "Text 3"
                    }
                ]
            }';

I am looking at achieving the following:

  1. Tell that item 2 is lacking the text key
  2. Tell that the amount key in item 1 has no value (is empty)

I have tried array_diff but its seems not to be the right approach.

Any leads on how to achieve this?

CodePudding user response:

First, your JSON is invalid. No trailing commas are allowed. See line containing "amount": 1,.

$json_data = '{
                "action": "Action1",
                "data": [
                    {
                        "id": "1",
                        "amount": "",
                        "text": "Text 1"
                    },
                    {
                        "id": "2",
                        "amount": 1
                    },
                    {
                        "channel_id": "20",
                        "amount": "2",
                        "text": "Text 3"
                    }
                ]
            }';

To parse for missing or invalid fields, you can simply iterate with foreach over the data and do the validation.

$data = json_decode($json_data, true);

foreach ($data['data'] as $item) {
    if ($item['amount'] === '') {
        print_r($item);
        echo "No amount given\n\n";
    }
    if (!isset($item['text'])) {
        print_r($item);
        echo "Missing text field\n\n";
    }
}

This will output

Array
(
    [id] => 1
    [amount] => 
    [text] => Text 1
)
No amount given

Array
(
    [id] => 2
    [amount] => 1
)
Missing text field

CodePudding user response:

JSON need to be corrected. This can be done by some logic:

<?php
$json_data = '{
    "action": "Action1",
    "data": [
        {
            "id": "1",
            "amount": "",
            "text": "Text 1"
        },
        {
            "id": "2",
            "amount": 1
        },
        {
            "channel_id": "20",
            "amount": "2",
            "text": "Text 3"
        }
    ]
}';

$arrdata = json_decode($json_data, true)['data'];

echo '<pre>';
    print_r($arrdata);
echo '</pre>';

$arr1 = $arrdata[0];
$arr2 = $arrdata[1];
$arr3 = $arrdata[2];

$diff = array_diff_assoc($arr1, $arr2, $arr3);

echo '<pre>';
    print_r($diff);
echo '</pre>';

$i = 0;
$diffArr = array();
foreach($arrdata as $arrdat){
    if(!isset($arrdat['id'])){
        $diffArr[] = array('id', $i);
    }
    if(!isset($arrdat['channel_id'])){
        $diffArr[] = array('channel_id', $i);
    }
    if(!isset($arrdat['amount'])){
        $diffArr[] = array('amount', $i);
    }
    $i  ;
}

echo '<pre>';
print_r($diffArr);
?>

Array
(
    [0] => Array
        (
            [id] => 1
            [amount] => 
            [text] => Text 1
        )

    [1] => Array
        (
            [id] => 2
            [amount] => 1
        )

    [2] => Array
        (
            [channel_id] => 20
            [amount] => 2
            [text] => Text 3
        )

)
Array
(
    [id] => 1
    [amount] => 
    [text] => Text 1
)
Array
(
    [0] => Array
        (
            [0] => channel_id
            [1] => 0
        )

    [1] => Array
        (
            [0] => channel_id
            [1] => 1
        )

    [2] => Array
        (
            [0] => id
            [1] => 2
        )

)
  •  Tags:  
  • php
  • Related