0

I have 2 arrays:

$myArray = array ( 'name' => 'Dollar', 'sign' => '$', 'format' => '1', 'decimals' => '1', 'conversion_rate' => '1.324400' );

$myArrayNew = array ( 0 => 'Name', 1 => 'Sign', 2 => 'Format', 3 => 'Decimals', 4 => 'Conversion Rate');

When i use

$combinedarrays = array_combine($myArrayNew, $myArray);

the output is

Array ( [Name] => Dollar [Sign] => $ [Format] => 1 [Decimals] => 1 [Conversion Rate] => 1.324400 )

That is what I need, but the problem is when my first array is multidimensional like:

$myArray = array( array ( 'name' => 'Dollar', 'sign' => '$', 'format' => '1', 'decimals' => '1', 'conversion_rate' => '1.324400' ),
                  array ( 'name' => 'Euro', 'sign' => '€', 'format' => '2', 'decimals' => '1', 'conversion_rate' => '1.000000' ));

So, how to change the keys to be like below?

$myArray = array( array ( 'Name' => 'Dollar', 'Sign' => '$', 'Format' => '1', 'Decimals' => '1', 'Conversion Rate' => '1.324400' ),
                  array ( 'Name' => 'Euro', 'Sign' => '€', 'Format' => '2', 'Decimals' => '1', 'Conversion Rate' => '1.000000' ));

1 Answer 1

0

You need to loop on sub arrays.

Try this:

$myArrayNew = array ( 0 => 'Name', 1 => 'Sign', 2 => 'Format', 3 => 'Decimals', 4 => 'Conversion Rate');

$myArray = array( array ( 'name' => 'Dollar', 'sign' => '$', 'format' => '1', 'decimals' => '1', 'conversion_rate' => '1.324400' ),
              array ( 'name' => 'Euro', 'sign' => '€', 'format' => '2', 'decimals' => '1', 'conversion_rate' => '1.000000' ));

$combinedarrays = array();
foreach($myArray as $subarr)
   $combinedarrays[] = array_combine($myArrayNew, $subarr);

print_r($combinedarrays);
1
  • i knew i need to loop throug the multidimensional array. Commented Feb 5, 2014 at 17:53

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.