logo
down
shadow

Extract array with same name from multidimensional array


Extract array with same name from multidimensional array

Content Index :

Extract array with same name from multidimensional array
Tag : php , By : jrf
Date : November 29 2020, 12:01 PM

this will help I have this Array from a json that represent a form. I have to collect all the [components] in a new array , If I understood you correctly recursion can help:
function extractComponents($arrayWithComponents)
{
    $components = [];

    if (!isset($arrayWithComponents['components']) || !is_array($arrayWithComponents['components'])) {
        return $components;
    }

    foreach ($arrayWithComponents['components'] as $component) {
        if (isset($component['columns'])) {
            foreach ($component['columns'] as $column) {
                $components = array_merge($components, extractComponents($column));
            }
        } else {
            $components = array_merge($components, extractComponents($component));
            unset($component['components']);
            $components[] = $component;
        }
    }

    return $components;
}

var_dump(extractComponents($data));

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Which algorithm should I use to extract an array from a multidimensional array?


Tag : arrays , By : silvervino
Date : March 29 2020, 07:55 AM
this will help One relatively simple way is to approach this task as a recursive mapping of K-tuples of dimension N to K-tuples of dimension M < N. Since this is language-agnostic, the following facilities need to be available in your target language:
Given an instance of an N-dim array, find N Given an instance of an N-dim array and a k < N, find the number of items in dimension k Given an instance of an N-dim array A and a vector of N integers I, get or set the item at a position defined by indexes in the vector. For example, if the vector is I = {2, 3, 4}, there needs to be an operation for obtaining A[2, 3, 4] from the 3-D array A and a vector I. Given a vector D of size M defining dimensions, there needs to be an operation for constructing an array of M dimensions, each dimension taking the size of the corresponding item in the vector. For example, if the vector D = {3, 4}, there needs to be an operation that creates a 2-D array with dimensions 3 and 4.
void RecursiveCopy(Vector F, Vector D, Array S, Vector SI, int sp, Array T, Vector TI, int tp) {
    if (pos != TS.size) {
        if (F[sp] != -1) {
            // This is an "all" dimension
            for (int i = 0 ; i != D[sp] ; i++) {
                SI[sp] = i;
                TI[tp] = i;
                RecursiveCopy(F, D, S, SI, sp+1, T, TI, tp+1);
            }
        } else {
            // This is a fixed dimension
            SI[sp] = F[sp];
            RecursiveCopy(F, D, S, SI, sp+1, T, TI, tp);
        }
    } else {
        // Read from the source at indexes defined by vector SI
        var value = S.get(SI);
        // Write to the destination at indexes defined by vector TI
        T.setValue(TI, value); // Prerequisite 3
    }
}
Array getSubarray(Array S, Vector<string> request) {
    Vector F = Vector[S.Size]; // The number of dimensions in A; prerequisite 1
    Vector D = Vector[S.Size]; // The number of dimensions in A
    Assert(request.Size == S.Size); // Request must have N items
    int k = 0;
    Vector resDim;
    for (int i = 0 ; i != req.Size ; i++) {
        if (req[i] == "all") {
            k++;
            F[i] = -1;
            D[i] = A.dimensionOf(i); // Prerequisite 2
            resDim.Add(D[i]);
        } else {
            F[i] = ParseInteger(req[i]);
            D[i] = -1;
        }
    }
    Array T = Array(resDim); // Prerequisite #4
    Vector SI = Vector[S.Size];
    Vector TI = Vector[k];
    RecursiveCopy(F, D, S, SI, 0, T, TI, 0);
    return T;
}

Extract Multidimensional Array And Manipulate to Other Array


Tag : php , By : Paul
Date : March 29 2020, 07:55 AM
To fix this issue This is the standard task for an array_map. Assuming you have an initial array in $array variable:
$result = array_reduce(array_map(function($elem) {
  $a = array();
  foreach($elem['hari'] as $k => $v) {
    $a[] = array(
      'id_jadwal'      => 1,
      'hari'           => $v,
      'jam'            => $elem['jam'][$k],
      'mata_pelajaran' => $elem['mata_pelajaran'][$k]
    );
  }
  return $a;
}, $array), function($memo, $item) {
  return $memo = array_merge($memo, $item);
}, array());

How to extract multidimensional associative array to simple numeric array


Tag : php , By : kbrust
Date : March 29 2020, 07:55 AM
Any of those help If you are using PHP > 5.5.0, you can use array_column:
$ids = array_column($array, 'ID');
if(!function_exists("array_column"))
{

    function array_column($array,$column_name)
    {

        return array_map(function($element) use($column_name){return $element[$column_name];}, $array);

    }

}

How to extract first element of each child array in multidimensional array?


Tag : javascript , By : Alex
Date : March 29 2020, 07:55 AM
hop of those help? I have a multidimensional array: , You can use nested map() and then flat()
var arr = [ [['Name 1', 2, 'Nigeria'], ['Name 3', 52, 'Egypt'], ['Name 5', 75, 'South Africa']], [['Name 5', 8, 'Nigeria'], ['Name 1', 62, 'Egypt'], ['Name 3', 115, 'South Africa']], [['Name 6', 88, 'Nigeria'], ['Name 3', 92, 'Egypt'], ['Name 5', 825, 'South Africa']] ];
 
   const res = arr.map(x => x.map(a => a[0])).flat(2)
   console.log(res)
var arr = [ [['Name 1', 2, 'Nigeria'], ['Name 3', 52, 'Egypt'], ['Name 5', 75, 'South Africa']], [['Name 5', 8, 'Nigeria'], ['Name 1', 62, 'Egypt'], ['Name 3', 115, 'South Africa']], [['Name 6', 88, 'Nigeria'], ['Name 3', 92, 'Egypt'], ['Name 5', 825, 'South Africa']] ];
 
 const res = [].concat(...arr.map(x => x.map(x => x[0])))
 console.log(res)

How to extract values from a multidimensional array to form another array?


Tag : javascript , By : baumichel
Date : March 29 2020, 07:55 AM
I wish did fix the issue. You could use reduce to group the items like this. Create an accumulator object with each type of food as key. Destructure each inner array to get the first and second item.
var MyArray = [
  ["coke", "beverages", "checked", 000],
  ["baguette", "bread", "checked", 001],
  ["water", "beverages", "checked", 000],
  ["pepsi", "beverages", "checked", 000],
  ["wholemeal", "bread", "checked", 001],
]

const grouped = MyArray.reduce((r, [item, type]) => {
  r[type] = r[type] || [];
  r[type].push(item)
  return r;
}, {})

const { bread, beverages } = grouped

console.log(bread, beverages)
{
  "beverages": [ "coke", "water", "pepsi" ],
  "bread": [ "baguette", "wholemeal" ]
}
Related Posts Related QUESTIONS :
  • Error connecting to CommissionJunction Token : [60] SSL certificate problem: unable to get local issuer certificate
  • How do I add a new key value pair to an object in PHP?
  • Http post request to php with dart
  • How to add a route in with blade into parameters of a component
  • How can i get the source of a 777 - CHMOD php file?
  • Convert 2010-04-16 16:30:00 to "Tomorrow Afternoon"
  • How can I hide a $_GET variable to be more secured in PHP?
  • Login to Gmail Inbox using Curl?
  • Upload Excel or CSV file to MySQL with PHP
  • Outlook Marking Email as Junk Email
  • Upload an image using class.upload.php via AJAX, display image in form using jQuery
  • mysql_connect() causes page to not display (WAMP)
  • How to limit a users uploaded image size using PHP
  • What's the difference whether or not to run PHP in safe mode?
  • Logging in to a website cURL
  • Using php to create a password system with chinese characters
  • PHP is not called properly in IE using set interval function in Javascript
  • How to read pdf, ppt, xl, doc files content into a string in php/python
  • php script for currency conversion
  • In PHP... best way to turn string representation of a folder structure into nested array
  • PayPal for Indian Rupees (INR) any solution
  • PHP Deployment to Live Server
  • Ideal Multi-Developer Lamp Stack?
  • How can I save an image from a file input field using PHP & MySQL?
  • Warning: array_multisort() [function.array-multisort]: Argument #1 is expected to be an array or a sort flag in
  • Connecting to external MySQL DB from a web server not running MySQL
  • Is it possible to add custom fields to a Drupal taxonomy term?
  • PHP 6.0 - Roadmap?
  • jQuery wait for ajax call to be over before continuing
  • checkUnique function?
  • How to define an n-m relation in doctrine?
  • Sort the unsorted text file and rewrite to same text file in sorted order
  • Version control a content management system?
  • How can i run Single PHP File using apache-tomcat-6.0.18?
  • Array of objects is empty when I come to retrieve one from the array
  • PHP Library: Calculate a bounding box for a given lat/lng location
  • Good Wordpress Theme/plugin for showing off code examples and scripts
  • Replace all spaces and special symbols with dash in URL using PHP language
  • globally get any field value in user table of logged in user
  • Can you get a job if you know XHTML, CSS and Basic PHP?
  • HTML Upload Form will only upload files found in the directory of the PHP file
  • Determine if a PHP array uses keys or indices
  • Are frameworks really necessary for beginners/intermediates?
  • PHP File Downloading Questions
  • Who owns a php exec tar extracted file?
  • Best way to get timezone of user based on area code
  • Mysql BETWEEN clause being ignored. No syntax error
  • Zend Framework: Getting request object in bootstrap
  • PHP + GD: imagetruecolortopalette not keeping transparency
  • Good Guide For Web App Security?
  • Creating a new wordpress post from twitter tweets
  • Query to show images with recent posts in Wordpress sidebar/widget
  • select & print multiple value from dropdown list
  • Storing a digital signature for bookings on a web based system
  • How to generate all permutations of a string in PHP?
  • How do I get the PostgreSQL PHP extension working on Windows?
  • Parsing WordPress XML, slash:comments syntax?
  • Hide order status in the status dropdown on WooCommerce admin orders page
  • PHP Converting Hex string to Byte Array - converting VB.Net to PHP
  • Mutiple forms using same php mailer script
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com