0

I have array multidimensional array,

Array(

    Array
    (
        [UTTL_M_FT] => 2616.00
        [TUG] => 5310
        [univercity] => 1
        [year] => 2010
    )

    Array
    (
        [UTTL_M_FT] => 2710.00
        [TUG] => 5405
        [univercity] => 1
        [year] => 2012
    )

    Array
    (
        [UTTL_M_FT] => 2622.00
        [TUG] => 7803
        [univercity] => 2
        [year] => 2010
    )

    Array
    (
        [UTTL_M_FT] => 2759.00
        [TUG] => 8423
        [univercity] => 2
        [year] => 2012
    )

    Array
    (
        [UTTL_M_FT] => 3330.00
        [TUG] => 6879
        [univercity] => 3
        [year] => 2011
    )

    Array
    (
        [UTTL_M_FT] => 3373.00
        [TUG] => 6796
        [univercity] => 3
        [year] => 2013
    )

    Array
    (
        [UFTF_ASN] => 749
        [UFTF_TTL] => 5296
        [univercity] => 1
        [year] => 2013
    )

)

And we require array in following format,

Array(

[univercity] => 1,

Array
    (
        [UTTL_M_FT] => 2616.00
        [TUG] => 5310       
        [year] => 2010
    ),

Array
    (
        [UTTL_M_FT] => 2710.00
        [TUG] => 5405
        [year] => 2012
    ),  

Array
    (
        [UFTF_ASN] => 749
        [UFTF_TTL] => 5296
        [year] => 2013
    )

),


Array(
[univercity] => 2,

Array
    (
        [UTTL_M_FT] => 2759.00
        [TUG] => 8423       
        [year] => 2012
    )

),  

Array(

[univercity] => 3,

    Array
    (
        [UTTL_M_FT] => 3330.00
        [TUG] => 6879       
        [year] => 2011
    ),
    Array
    (
        [UTTL_M_FT] => 3373.00
        [TUG] => 6796
        [year] => 2013
    )
);

2 Answers 2

1

This will work for you, append the same university id array into the main container data array

<?php   

     $data =array();
        foreach($inpuArray as $array){
            $universityId = $array['univercity'];
            unset($array['univercity']);
            $data['univercity'][$universityId][] = $array;
        }
        print_r($data);

    ?>
0
0

This works for me:

$new = array();
$i=0;
foreach($array as $a){
    $u = $a['univercity'];
    unset($a['univercity']);
    $found = false;
    foreach($new as $k=>$v){
        if(isset($new[$k]['univercity']) && $new[$k]['univercity'] == $u){
            $new[$k][] = $a;
            $found = true;
            break;
        }
    }
    if(!$found){
        $new[$i]['univercity']=$u;
        $new[$i][]=$a;
        $i++;
    }
}
echo"<pre>";
var_export($new);
echo"</pre>";

Output:

array (
  0 => 
  array (
    'univercity' => 1,
    0 => 
    array (
      'UTTL_M_FT' => 2616,
      'TUG' => 5310,
      'year' => 2010,
    ),
    1 => 
    array (
      'UTTL_M_FT' => 2710,
      'TUG' => 5405,
      'year' => 2012,
    ),
    2 => 
    array (
      'UFTF_ASN' => 749,
      'UFTF_TTL' => 5296,
      'year' => 2013,
    ),
  ),
  1 => 
  array (
    'univercity' => 2,
    0 => 
    array (
      'UTTL_M_FT' => 2622,
      'TUG' => 7803,
      'year' => 2010,
    ),
    1 => 
    array (
      'UTTL_M_FT' => 2759,
      'TUG' => 8423,
      'year' => 2012,
    ),
  ),
  2 => 
  array (
    'univercity' => 3,
    0 => 
    array (
      'UTTL_M_FT' => 3330,
      'TUG' => 6879,
      'year' => 2011,
    ),
    1 => 
    array (
      'UTTL_M_FT' => 3373,
      'TUG' => 6796,
      'year' => 2013,
    ),
  ),
)
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.