Give that array
I want to use the random string attached to the tab-title and tab content keys which are rEXMp and 3T2IV respectively , I want to use those to split the array into two arrays and getting rid of these characters in the new array and also add the cell_nums and grid_num and cell_weight which are directly following the end of the key to the same array

Array
(
    [0] => Array
        (
            [tab-title-rEXMp] => tab-1
        )

    [1] => Array
        (
            [tab-content-rEXMp] => tab-1 content
        )

    [2] => Array
        (
            [tab-title-rEXMp] => tab-2 
        )

    [3] => Array
        (
            [tab-content-rEXMp] => tab-2 content
        )

    [4] => Array
        (
            [tab-title-rEXMp] => tab-3
        )

    [5] => Array
        (
            [tab-content-rEXMp] => tab-3 content
        )

    [6] => Array
        (
            [cells_num] => 1
        )

    [7] => Array
        (
            [grid_num] => 0
        )

    [8] => Array
        (
            [cell_weight] => 100%
        )

    [9] => Array
        (
            [tab-title-3T2IV] => tab-4
        )

    [10] => Array
        (
            [tab-content-3T2IV] => tab-4 content
        )

    [11] => Array
        (
            [tab-title-3T2IV] => tab-5
        )

    [12] => Array
        (
            [tab-content-3T2IV] => tab-5 content
        )

    [13] => Array
        (
            [tab-title-3T2IV] => tab-6
        )

    [14] => Array
        (
            [tab-content-3T2IV] => tab-6 content
        )

    [15] => Array
        (
            [cells_num] => 1
        )

    [16] => Array
        (
            [grid_num] => 1
        )

    [17] => Array
        (
            [cell_weight] => 100%
        )

)

I wanna output something like

Array(
0 => Array(
   [tabs] => Array(
 0 => Array(
    'tab-title' => 'tab-1',
    'tab-content' => 'tab-1 content'
    ),
     1 => Array(
    'tab-title' => 'tab-2',
    'tab-content' => 'tab-2 content'
    ),
     2 => Array(
    'tab-title' => 'tab-3',
    'tab-content' => 'tab-3 content'
    )
  ),
  cells_num => 0,
  cell_weight => 100%,
  grid_num    =>   0
),
1=> Array(
[tabs] => Array(
    0 => Array(
    'tab-title' => 'tab-4',
    'tab-content' => 'tab-4 content'
    ),
     1 => Array(
    'tab-title' => 'tab-5',
    'tab-content' => 'tab-5 content'
    ),
     2 => Array(
    'tab-title' => 'tab-6',
    'tab-content' => 'tab-6 content'
    )
  ),
  cells_num => 0,
  cell_weight => 100%,
  grid_num    =>   1
)

)

any help will be appreciated :)

Recommended Answers

All 4 Replies

So, what have you done so far?

    function array_tabs($arr){
       foreach ( $arr as $k => $v ){
       foreach ( $v as $_k=>$_v ){
        if(!preg_match('/tab/',$_k)) continue;
        preg_match('/^\w+\-\w+\-(.*)$/',$_k,$tabs_ids);
         $matches[] = $tabs_ids[1];
         }
       }

       $tabs_objs = array_values(array_unique($matches)); 
        $tabs_objs_count = count($tabs_objs); 
        for($i = 0; $i < $tabs_objs_count; $i++){ 
         $t[]['tabs'][] = get_tabs($tabs_objs[$i],$arr); 
        }
        return $t;
     }

       function get_tabs($needle,$arr){
       foreach ( $arr as $k => $v ){
       foreach ( $v as $_k=>$_v ){
       if(preg_match('/'.$needle.'/',$_k)){
       $re[str_replace("-".$needle,"",$_k)][] = $_v;
       }
       $rs =  preg_grep('/(grid_num|cells_num|cell_weight)/',array_keys($v));
       if($rs){
        print_r($rs);
         $re[$rs[0]] =$v[$rs[0]];
       } 
         }
         }
           return $re;
       }


        print_r(array_tabs($_POST['object-tabs']));

     got this

     Array
(
    [0] => Array
        (
            [tabs] => Array
                (
                    [0] => Array
                        (
                            [tab-title] => Array
                                (
                                    [0] => tab-1
                                    [1] => tab-2
                                    [2] => tab-3
                                )

                            [tab-content] => Array
                                (
                                    [0] => tab-1 content
                                    [1] => tab-2 content
                                    [2] => tab-3 content
                                )

                            [cells_num] => 1
                            [grid_num] => 1
                            [cell_weight] => 100%
                        )

                )

        )

    [1] => Array
        (
            [tabs] => Array
                (
                    [0] => Array
                        (
                            [cells_num] => 1
                            [grid_num] => 1
                            [cell_weight] => 100%
                            [tab-title] => Array
                                (
                                    [0] => tab-4
                                    [1] => tab-5
                                    [2] => tab-6
                                )

                            [tab-content] => Array
                                (
                                    [0] => tab-4 content
                                    [1] => tab-5 content
                                    [2] => tab-6 content
                                )

                        )

                )

        )

)

The big problem here is that cells_num, grid_num and cell_weight do not have a suffix, as the others, that would simplify a lot the game. Anyway, if these groups are of fixed length you can split them and then rearrange as you like. An example:

<?php

include 'array.php';

function ohoh($a, $needle)
{
    $result = array();
    $i = 0;
    foreach($a as $key => $value)
    {
        foreach($value as $kv => $vv)
        {
            preg_match("(.*title|.*content)", $kv, $res);
            if(count($res))
            {
                $result[$i][$res[0]] = $vv;
                if(strstr($res[0], 'content')) $i++;
            }

            preg_match('/(grid_num|cells_num|cell_weight)/', $kv, $rs);
            if(count($rs) > 0) $result[$kv] = $vv;
        }
    }

    return $result;
}

function fire($a, $keys)
{
    $length = count($a) / count($keys);
    $arrays = array_chunk($a, $length);
    $result = array();

    $i = 0;
    foreach($arrays as $array)
    {
        $result[]['tabs'] = ohoh($array, $keys[$i]);
        $i++;
    }

    return $result;
}

$keys = array('rEXMp', '3T2IV');

echo "<pre>";
print_r(fire($a, $keys));
echo "</pre>";

Will print:

Array
(
    [0] => Array
        (
            [tabs] => Array
                (
                    [0] => Array
                        (
                            [tab-title] => tab-1
                            [tab-content] => tab-1 content
                        )

                    [1] => Array
                        (
                            [tab-title] => tab-2
                            [tab-content] => tab-2 content
                        )

                    [2] => Array
                        (
                            [tab-title] => tab-3
                            [tab-content] => tab-3 content
                        )

                    [cells_num] => 1
                    [grid_num] => 0
                    [cell_weight] => 100%
                )

        )

    [1] => Array
        (
            [tabs] => Array
                (
                    [0] => Array
                        (
                            [tab-title] => tab-4
                            [tab-content] => tab-4 content
                        )

                    [1] => Array
                        (
                            [tab-title] => tab-5
                            [tab-content] => tab-5 content
                        )

                    [2] => Array
                        (
                            [tab-title] => tab-6
                            [tab-content] => tab-6 content
                        )

                    [cells_num] => 1
                    [grid_num] => 1
                    [cell_weight] => 100%
                )

        )

)

If you cannot provide a starting $keys array, then just use numbers, something like $keys = 2 and change a bit the code, here's the example:

Bye!

Thank you ..that definitely will help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.