logo
down
shadow

Navigating through multiple object and array structure in PHP


Navigating through multiple object and array structure in PHP

By : user2955793
Date : November 22 2020, 10:48 AM


Share : facebook icon twitter icon
Iterate through JS object/array, and if you find a match change the value and return the original object/array structure

Iterate through JS object/array, and if you find a match change the value and return the original object/array structure


By : vivek vijayan
Date : March 29 2020, 07:55 AM
I wish this helpful for you I am trying to get a functionality like PHP in Javascript/Jquery, where we pass the address of a value (&$value) and edit its value in the actual array structure in PHP. For example I have a object in the following format, , This recursively applies a function to a nested object:
code :
transform = function(obj, fun) {
    if (typeof obj != "object")
        return fun(obj);
    Object.keys(obj).forEach(function(k) {
        obj[k] = transform(obj[k], fun);
    });
    return obj;
}
transform(yourData, function(val) {
    if (val.replace)
        return val.replace(/bad/g, "censored");
    else
        return val;
})
Navigating through array and inserting into SQL - php (weird array structure)

Navigating through array and inserting into SQL - php (weird array structure)


By : Kasper Bertelsen
Date : March 29 2020, 07:55 AM
Inserting/navigating nested JavaScript object/array

Inserting/navigating nested JavaScript object/array


By : D.PEPIN
Date : March 29 2020, 07:55 AM
seems to work fine You could iterate the array and test if _id property has the wanted value. Then save either the node, the parent or the next item of the array.
For getting the parent node, the actual parent is saved as a closure and returned if the wanted _id is found.
code :
function getNode(array, id) {
    var node;
    array.some(function iter(a) {
        if (a._id === id) {
            node = a;
            return true;
        }
        return Array.isArray(a.subItems) && a.subItems.some(iter);
    });
    return node;
}

function getParent(array, id) {
    var parent ;
    array.some(function iter(p) {
        return function (a) {
            if (a._id === id) {
                parent = p;
                return true;
            }
            return Array.isArray(a.subItems) && a.subItems.some(iter(a));
        };
    }(undefined));
    return parent;
}

function getNextNode(array, id) {
    var node;
    array.some(function iter(a, i, aa) {
        if (a._id === id) {
            node = aa[i + 1];
            return true;
        }
        return Array.isArray(a.subItems) && a.subItems.some(iter);
    });
    return node;
}

var items = [{ _id: 0, content: 'Item 1 something', note: 'Some note for item 1' }, { _id: 5, content: 'Item 1.1 something', note: 'Some note for item 1.1' }, { _id: 1, content: 'Item 2 something', note: 'Some note for item 2', subItems: [{ _id: 2, parent_id: 1, content: 'Sub Item 1 something', subItems: [{ _id: 3, parent_id: 2, content: 'Sub Sub Item 4' }] }] }];

console.log(getNode(items, 3));
console.log(getParent(items, 2));
console.log(getNextNode(items, 5));
.as-console-wrapper { max-height: 100% !important; top: 0; }
How to structure Multiple object and array of JSON in java android

How to structure Multiple object and array of JSON in java android


By : lansus
Date : March 29 2020, 07:55 AM
will be helpful for those in need This is JSON file. I want make java can produce like this json. Just ignore the value, What i want is the structure of the json. , Use this code and Enjoy :)
code :
private void createJsonStructure() {

    try
    {
        JSONObject rootObject = new JSONObject();

        JSONArray carArr = new JSONArray();
        for (int i = 0; i < 2 ; i++)
        {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("CarId", "123");
            jsonObject.put("Status", "Ok");
            carArr.put(jsonObject);
        }
        rootObject.put("Car", carArr);


        JSONArray motorArr = new JSONArray();
        for (int i = 0; i < 2 ; i++)
        {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("MotorId", "123");
            jsonObject.put("Status", "Ok");
            motorArr.put(jsonObject);
        }
        rootObject.put("Motor", motorArr);


        JSONArray busArr = new JSONArray();
        for (int i = 0; i < 2 ; i++)
        {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("BusId", "123");
            jsonObject.put("Status", "Ok");
            busArr.put(jsonObject);
        }
        rootObject.put("Bus", busArr);

        Log.e("JsonObject", rootObject.toString(4));

    }
    catch (Exception ex)
    {
        ex.printStackTrace();
    }
}
How do I push a parameter into a multiple nested array/object structure?

How do I push a parameter into a multiple nested array/object structure?


By : user3188655
Date : March 29 2020, 07:55 AM
hope this fix your issue given the following database , To achieve expected, add below to code to the commented section
code :
if(y.includes(movie) == false) {
        //**push movie into the existing object.**
      DB.forEach(v => {
        if(v.genre === genre){
          v.movies.push({title: movie})
        }
      })
    } 
DB = [
    {
        genre:'thriller', 
        movies:[
            {
                title:'the usual suspects', release_date:1999
            }
        ]},
        {
        genre:'commedy', 
        movies:[
            {
                title:'pinapple express', release_date:2008
            }
        ]}
]


var moviesDB = function(DB, genre, movie) {
movie = typeof movie === 'object'? movie.title : movie; // to check whether movie parameter is string or object
    var x = []

    for (var i = 0; i < DB.length; i++) {
        x.push(DB[i].genre);
    }

    if(x.includes(genre) == false) {
        DB.push({genre: genre, movies: []});    
    } else {
        console.log("genre already here")
    }

    var y = []

    for (var i = 0; i < DB.length; i++) {
        DB[i].movies.forEach (function (object){
            y.push(object.title)
        })
    }

    if(y.includes(movie) == false) {
        //**push movie into the existing object.**
      DB.forEach(function(object) {
    if(object.genre === genre){
      object.movies.push({title: movie})
    }
  })
} else {
        return `the movie the ${movie} is already in the database!`
    }

    return DB;
}

console.log(moviesDB(DB, "drama", "A drama movie"))
console.log(moviesDB(DB, "commedy", "Scary movie"))
console.log(moviesDB(DB, 'commedy', 'pinapple express'))
Related Posts Related Posts :
  • New to PHP, trying to extract information from another website
  • JavaScript AmChart to Image for Email
  • Is javascript validation enough to keep my forms secure?
  • Regex extract variables from [shortcode]
  • Download a .mp3 link and save it to computer with PHP
  • Error with the ereg_replace
  • Getting the fields attached to a bundle of an entity in Drupal
  • Php how to check multiple times (within `foreach`) if values from array exists in mysql?
  • MySQL and PHP parsing strange string
  • Pull specific data from multidimensional associative array
  • How to get the original URI extension using PHP Tonic?
  • Upload videos and images from Android to PHP server?
  • Unset a multidimensional array with another multidimensional array with values than key
  • Calling a user e-mail to add to a mailto link PHP
  • Why is override_function coming back as an undefined function
  • PHP mySQLi_fetch_all: iterate through each row
  • Using pow in php
  • What's wrong with my PHP Script? T_String issue
  • how to call a function of another file without including that file
  • is_rgb function in php
  • transform the numbers to letters using php
  • How to catch an exception from another class method PHP
  • PHP exec is blocked by ISP
  • PHP4 to PHP5 migration character encoding. latin1 vs utf8
  • Begin Transaction not defined
  • How much harm can a user do with xss on a page unique to them?
  • How to import data from a php file to javascript variable
  • PHP and Javascript - log complete workflow
  • When I echo !!! 0 it gives me a strange result
  • jquery ajax post dies after submit
  • How to upload file with curl on sftp server
  • Advanced search using a text input to search through only one category
  • Why do I get "Non-static method App\Models\Category::products() should not be called statically" in laravel
  • Stop notices from undefined indexes in arrays
  • PHP Excel Error when entering SUM function to a cell
  • laravel 4 eloquent eager load relation count
  • click submit in html and run php code
  • Extremely frustrating behavior with Internet Explorer, Bootstrap and Respond.js
  • Vkontakte vtag issues
  • Divide the number to a correct decimal
  • mysqli_insert_query not working
  • How to make a sitemap using zend framework 1
  • Laravel: array to Model with relationship tree
  • $_SERVER['DOCUMENT_ROOT'] vs $path = $_SERVER['DOCUMENT_ROOT']
  • What is the best way to store multiple checkbox values in the database and work with it later on
  • Why does foreach copy the array when we did not modify it in the loop?
  • Receive Jquery/Ajax Post Request in PHP
  • How to better use the if(!empty()){ //codes} statement
  • PHP error mysql_close() boolean given
  • Move an image file from one server to another using PHP
  • PHP: Encoding non ISO-8859-1 charactrers into utf8
  • Handling MySQL errors in PHP
  • Object orientated mysql num_rows query in one line
  • Remove not closed html elements from end of text
  • Generate random ENUM value [PHP, MySQL]
  • Deleting data from table using jQuery and SQL
  • How to make sure the constructor is executed when I'm trying to reach a static property?
  • Trying to understand arrow next to PHP function
  • Form using external php file to show "Sucess"
  • WooCommerce - new shipping method - how to query individual product weights
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org