Home > Software engineering >  Form an array list in the another format using PHP
Form an array list in the another format using PHP

Time:11-30

I have an associate array format like $aa. Need to form an array list in the different format.

$aa = Array
 (
'Std'=> Array
    (
         'Add/Remove/Modify',
         'Create',
         'Addition',
         'repository',
    ),

'Agl' => Array
    (
         'Disk',
         'center',
         'Service ',
    ),

'Error' => Array
    (
         'VM',
         'DNS',
         'Upgrade',
    ),

'Hyg' => Array
    (
       'Health',
        'VM ',
        'Clear',
    ),

'Int' => Array
    (
         'iExecute',
         'Storage',
         'CMDB',
    ),

'Jor' => Array
    (
         'Uptime ',
         'Server ',
        'Report',
    ),

'Mon' => Array
    (
         'jobs',
         'mon',
         'SLA',
    ),

);

Have to form the array like the below format in PHP. Help me on this. Thanks in advance

$res = array( 
    array( 
            'Add/Remove/Modify','Disk', 'VM','Health', 'iExecute','Uptime ', 'jobs'
    ),
    array(
        'Create','center', 'DNS', 'VM ','Storage','Server ', 'mon'
    ),
    array(
        'Addition', 'Service ', 'Upgrade', 'Clear', 'CMDB', 'Report','SLA'
    ),
    array('repository')
    ); 

CodePudding user response:

array_column should help you out. If you know the maximum items you can have in one of your arrays, you do not need to find the max, like I'm doing here.

$array = [
    'a' => [
        '1.1',
        '1.2',
        '1.3',
        '1.4',
    ],
    'b' => [
        '2.1',
        '2.2',
        '2.3',
        '2.4',
        '2.5',
    ],
    'c' => [
        '3.1',
        '3.2',
        '3.3',
    ],
];

$max_items = 0;
foreach ($array as $sub) {
    $count = count($sub);
    if ($count > $max_items) {
        $max_items = $count;
    }
}

$new_array = [];
for ($i = 0; $i < $max_items; $i  ) {
    $new_array[] = array_column(array_values($array), $i);
} 

var_dump($new_array);

Result

array(5) {
  [0]=>
  array(3) {
    [0]=>
    string(3) "1.1"
    [1]=>
    string(3) "2.1"
    [2]=>
    string(3) "3.1"
  }
  [1]=>
  array(3) {
    [0]=>
    string(3) "1.2"
    [1]=>
    string(3) "2.2"
    [2]=>
    string(3) "3.2"
  }
  [2]=>
  array(3) {
    [0]=>
    string(3) "1.3"
    [1]=>
    string(3) "2.3"
    [2]=>
    string(3) "3.3"
  }
  [3]=>
  array(2) {
    [0]=>
    string(3) "1.4"
    [1]=>
    string(3) "2.4"
  }
  [4]=>
  array(1) {
    [0]=>
    string(3) "2.5"
  }
}

CodePudding user response:

You can first get the largest number of columns in one of the inner arrays by using max and array_map. Then you can get each individual column through array_column which takes integer keys as well for the index.

$result = [];
$maxNumberOfColumns = max(array_map('count', $aa));

for ($i = 0; $i < $maxNumberOfColumns; $i  ) {
    $result[] = array_column($aa, $i);
}

echo '<pre>', print_r($result, true), '</pre>';

The result:

(
    [0] => Array
        (
            [0] => Add/Remove/Modify
            [1] => Disk
            [2] => VM
            [3] => Health
            [4] => iExecute
            [5] => Uptime 
            [6] => jobs
        )

    [1] => Array
        (
            [0] => Create
            [1] => center
            [2] => DNS
            [3] => VM 
            [4] => Storage
            [5] => Server 
            [6] => mon
        )

    [2] => Array
        (
            [0] => Addition
            [1] => Service 
            [2] => Upgrade
            [3] => Clear
            [4] => CMDB
            [5] => Report
            [6] => SLA
        )

    [3] => Array
        (
            [0] => repository
        )

)

CodePudding user response:

This solution uses two loops, the first helps us determine the maximum length of the $res array while the second loops through each value to populate the $res sub-arrays.

$res = [];
$len_arr = [];

// Figure out the length of the $res array
foreach ($aa as $key => $value) {
    array_push($len_arr, count($value));
}

//Populate the $res array with $len number of empty arrays
$len = max($len_arr);
$res = array_fill(0, $len, []);

//Loop through each array to populate the inner array of the $res array
foreach ($aa as $key => $value) {
    for ($i = 0; $i < count($value); $i  ) {
        array_push($res[$i], $value[$i]);
    }
}

echo '<pre>', print_r($res, true), '</pre>';

The result of printing $res is shown below:

Array
(
    [0] => Array
        (
            [0] => Add/Remove/Modify
            [1] => Disk
            [2] => VM
            [3] => Health
            [4] => iExecute
            [5] => Uptime 
            [6] => jobs
        )

    [1] => Array
        (
            [0] => Create
            [1] => center
            [2] => DNS
            [3] => VM 
            [4] => Storage
            [5] => Server 
            [6] => mon
        )

    [2] => Array
        (
            [0] => Addition
            [1] => Service 
            [2] => Upgrade
            [3] => Clear
            [4] => CMDB
            [5] => Report
            [6] => SLA
        )

    [3] => Array
        (
            [0] => repository
        )

)

I hope you find this useful.

  • Related