webdevask.com

Menu

  • Home
  • New
  • Add question

php - How to convert single array to multidimensional and group base on value name

View Original

one text

Solution:

You can use to group your array based on the group_name and filter_group_id values, making a key for the result array based on those two values and adding the matching filter values to the filters subarray for each key. You can then use to re-index the result array numerically:

$result = array_reduce($data, function ($c, $v) {
    $key = $v['group_name'] . '#' . $v['filter_group_id'];
    if (!isset($c[$key])) {
        $c[$key] = array('group_name' => array('group_name' => $v['group_name'], 'filter_group_id' => $v['filter_group_id']));

    }
    $c[$key]['filters'][] = array('filter_id' => $v['filter_id'], 'name' => $v['name']);
    return $c;
}, array());

$result = array_values($result);
print_r($result);

The output is too long to show here but matches your requirement.

Demo on 3v4l.org

Source

See also:

  • php สร้าง Google ชีตจากบัญชีบริการ

  • php - Laravel 8.0 Installation Failed

  • javascript - How to include HTML data (tags) as value in "arrayToDataTable" of Google Pie Chart?

  • how to Grouping array in php

  • php - vender folder from my laravel project has more 1k error

© 2021
E-mail: contact@webdevask.com
Back to top