Order the array key position by group in PHP

260

I googled and found array key change only by index. But what I want is by group. There are 3 groups in the sample array. 1. Attendances 2. MainGroups and 3. Masters

My input is like that.

array(
    (int) 12 => 'Attendances',
    (int) 15 => '_master_add',
    (int) 16 => '_master_full_month',
    (int) 17 => '_master_edit_grade',
    (int) 18 => '_master_edit',
    (int) 19 => '_master_delete',
    (int) 20 => '_master_upload_csv',
    (int) 36 => 'MainGroups',
    (int) 39 => '_master_add',
    (int) 40 => '_master_edit',
    (int) 41 => '_master_delete',
    (int) 43 => 'Masters',
    (int) 46 => '_master_add',
    (int) 47 => '_master_edit',
    (int) 48 => '_master_delete',
    (int) 52 => '_master_stop'
)

The output I want is 1. Attendances 2. Masters and 3. MainGroups

array(
    (int) 12 => 'Attendances',
    (int) 15 => '_master_add',
    (int) 16 => '_master_full_month',
    (int) 17 => '_master_edit_grade',
    (int) 18 => '_master_edit',
    (int) 19 => '_master_delete',
    (int) 20 => '_master_upload_csv',
    (int) 43 => 'Masters',
    (int) 46 => '_master_add',
    (int) 47 => '_master_edit',
    (int) 48 => '_master_delete',
    (int) 52 => '_master_stop'
    (int) 36 => 'MainGroups',
    (int) 39 => '_master_add',
    (int) 40 => '_master_edit',
    (int) 41 => '_master_delete',
)
433

Answer

Solution:

There are no groups. Maybe you see them because you know what the values mean, php only sees a bunch of numbers as keys with a value attatched.

I see two options to solve your problem.

  1. Create a an array that containt the numbers of the keys your 'groups' refer to:

    $keys=array(

    "Attendances'=>array(15,16,1718,19,20),

    'Masters'=>array(46,47,48,52),

    'MainGroups'=>array(39,40,41)

    );

Now you can refer to a 'group value by calling

$myarray['Attendances'[0]] (for $myarray[15] ==> '_master_add')

This only works if the keys are always the same.

Your second option is to resturcture your array so it really contains groups:

array(
    'Attendances'= array(
        (int) 1 => '_master_add',
        (int) 2 => '_master_full_month',
        (int) 3 => '_master_edit_grade',
        (int) 4 => '_master_edit',
        (int) 5 => '_master_delete',
        (int) 6 => '_master_upload_csv'
    ),
    'MainGroups'=>array(
       (int) 1 => '_master_add',
       (int) 2 => '_master_edit',
       (int) 3 => '_master_delete',
       (int) 4 => 'Masters',
       (int) 5 => '_master_add',
       (int) 6 => '_master_edit',
       (int) 7 => '_master_delete',
       (int) 8 => '_master_stop'
    ),
   'Masters'=>array(
       (int) 1 => '_master_add',
       (int) 2 => '_master_edit',
       (int) 3 => '_master_delete',
       (int) 4 => '_master_stop'
   )
);

In that case you can call$myarray['Attendances[1]'] for '_master_add'

Edit After reading your comments, the following might work. It gives the output desired. But looping this array can only be done with aforeach to preserve the order.

Edit 2 Added a search for the keys of the 'group names' so you can create the order array without knowing the keys.

$my_array=array(... see above...);
$key1=array_search('Attendances',$my_array);
$key2=array_search('MainGroups',$my_array);
$key3=array_search('Masters',$my_array);

foreach($my_array as $k=>$v){
    if($k >= $key1 && $k<$key2){
        $group_1[]=$k;
        }
    elseif($k >= $key2 && $k<$key3){
        $group_2[]=$k;
        }
    else{
        $group_3[]=$k;
        }
    }

$order=array_merge($group_1,$group_3,$group_2);    
$new_array=array();

foreach($order as $key => $value){
    $new_array[$value]=$my_array[$value];
    }

echo '<pre>'.print_r($new_array,true).'<pre>';
331

Answer

Solution:

$input_arr = array(
    12 => 'Attendances',
    15 => '_master_add',
    16 => '_master_full_month',
    17 => '_master_edit_grade',
    18 => '_master_edit',
    19 => '_master_delete',
    20 => '_master_upload_csv',
    36 => 'MainGroups',
    39 => '_master_add',
    40 => '_master_edit',
    41 => '_master_delete',
    43 => 'Masters',
    46 => '_master_add',
    47 => '_master_edit',
    48 => '_master_delete',
    52 => '_master_stop'
);
$output_array = array();
$group_found = false;
$sort_arr = array('Attendances', 'Masters', 'MainGroups');

foreach ($sort_arr as $sort_group) {
    foreach ($input_arr as $key => $value) {
        if ($group_found == true || $value == $sort_group) {

            if ($group_found) {
                if (strpos($value, '_') !== false) {
                    $output_array[$key] = $value;
                } else {
                    $group_found = false;
                }
            }

            if ($value == $sort_group) {
                $group_found = true;
                $output_array[$key] = $value;
            }
        }
    }
}

Finally, I got it by double looping and using new array rather than modifying in the input array. Thanks to Michel for I've got an idea for using new array for my problem.

debug($output_array);
array(
    (int) 12 => 'Attendances',
    (int) 15 => '_master_add',
    (int) 16 => '_master_full_month',
    (int) 17 => '_master_edit_grade',
    (int) 18 => '_master_edit',
    (int) 19 => '_master_delete',
    (int) 20 => '_master_upload_csv',
    (int) 43 => 'Masters',
    (int) 46 => '_master_add',
    (int) 47 => '_master_edit',
    (int) 48 => '_master_delete',
    (int) 52 => '_master_stop',
    (int) 36 => 'MainGroups',
    (int) 39 => '_master_add',
    (int) 40 => '_master_edit',
    (int) 41 => '_master_delete'
)

People are also looking for solutions to the problem: php - How to generate array from an array

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.