json_add for php

JSON (JavaScript Object Notation) is a lightweight data-interchange format.

1. Easy for humans to read and write.
2. Easy for machines to parse and generate.
3. Collection of name/value pairs.
4. Used for C, C++, C#, Java, JavaScript, Perl, Python, and many others.
5. Compatible with universal data structures – array, vector, list, or sequence.

json_decode — Decodes a JSON string
json_encode — Returns the JSON representation of a value
json_add — Review below code 😉

<?php
function json_add($old, $new) {  // function to add json strings  $old, $new are arrays
        $old_arr = json_decode($old, true);
        if(is_array($old_arr)) {
            $new_arr = array_merge($old_arr, $new);
            return json_encode($new_arr);
        }
        else {
            // it means first column updation
            return json_encode ( $new );
        }
    }

/*
here $json_str is json formated string in which we have to add new $array
*/
json_add($json_str, $array);    
    
?>

Every opportunity is trouble… Every trouble is opportunity 😉

Install JSON on linux

Have the fun with json….

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate.

# yum -y install php-pear

# pecl install json

# vi /etc/php.d/json.ini
extension=json.so

# service httpd restart