0

Please take a look at the following array..

Array 
( 
  [0] => Array 
  ( 
    [fee_id] => 15 
    [fee_amount] => 308.5 
    [year] => 2009 
  ) 

  [1] => Array 
  ( 
    [fee_id] => 14 
    [fee_amount] => 308.5 
    [year] => 2009 
  ) 

)

I Need output like this,

    Array 
    ( 
     [rfp] => Array 
      ( 
        [fee_id] => 15 
        [fee_amount] => 308.5 
        [year] => 2009 
      ) 

     [user] => Array 
      ( 
       [fee_id] => 14 
       [fee_amount] => 308.5 
       [year] => 2009 
      ) 

    )

Is there any possibilities to do that..? I read this PHP rename array keys in multidimensional array

But it explains about the renaming of array keys, But I need this to implement in my live application.

Please help me..

Community
  • 1
  • 1

3 Answers3

6

Just build a new array with the data:

$newArray=[
    'rfp'=>$oldArray[0],
    'user'=>$oldArray[1]
];

if you really need to, you can then overwrite the old variable to hold the new array:

$oldArray = $newArray;
Steve
  • 20,703
  • 5
  • 41
  • 67
  • You could even skip the overwriting step by just redefining `$oldArray`. The values will exist until the array is overwritten. – Peter Oct 29 '15 at 13:22
3

try this code

<?php
$arr[0]['fee_id']=12;
$arr[0]['fee_amount']=308.5;
$arr[0]['year']=2009;

$arr[1]['fee_id']=14;
$arr[1]['fee_amount']=308.5;
$arr[1]['year']=2009;

echo "<pre>";
print_r($arr);
echo "</pre>";


$arr2['rfp']=$arr[0];
$arr2['user']=$arr[1];

echo "<pre>";
print_r($arr2);
echo "</pre>";
?>
Abhishek Sharma
  • 6,689
  • 1
  • 14
  • 20
2

This will be as easy as screwing in the imaginary lightbulb.

$array['key_name_you_want'] = $array[0];
unset($array[0]);

This will set all $array[0] values to $array['key_name_you_want'] and then unset $array[0] so it will no longer be in $array.

Peter
  • 8,776
  • 6
  • 62
  • 95