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..

2
  • How you use find method? Commented Oct 15, 2015 at 12:23
  • I used for my custom needs in my application. In what case you're asking for find method..? Commented Nov 19, 2015 at 8:21

3 Answers 3

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;
Sign up to request clarification or add additional context in comments.

1 Comment

You could even skip the overwriting step by just redefining $oldArray. The values will exist until the array is overwritten.
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>";
?>

1 Comment

Thanks for your answer, but how to implement can you explain in detail as a function.
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.

1 Comment

Plus one for the bizarre idiom.

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.