Replacing the indices in an indexed array with the keys from an associate array?

Hi, can someone help me with replacing the indices in the indexed array with the keys from the associate array while the final array will still be stored back to the indexed array?

<?php

  $Refractive_Index =  [
    "Water" => 1.333,
	"Ethanol" => 1.36,
	"Olive oil" => 1.47,
	"Ice" => 1.31,
	"Fused silica" => 1.46,
	"PMMA" => 1.49,
	"Window glass" => 1.52,
	"Polycarbonate" => 1.58,
	"Sapphire" => 1.77,
	"Diamond" => 2.42
  ];

 $Critical_Angle = [48.6, 47.3, 42.9, 49.8, 43.2, 42.2, 41.1, 39.3, 34.4, 24.4];
?>
<html>
<table>
   <?php

  foreach ($Refractive_Index as $key =>$value)
        {
  ?><tr>
  <td style='border: 1px solid #000;'><?php echo $key; ?></td>
  <td style='border: 1px solid #000;'><?php echo $value; ?></td>  
   </tr>
     <?php
        }
     ?>
      </table>
           </html>

I’d like the output to be something similar to this:
Water β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž - 48.6
Ethanol β€Žβ€β€β€Ž β€β€β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž - 47.3
Olive Oil β€Žβ€Žβ€β€β€Ž β€Ž β€Žβ€β€β€Ž - 48.6
Ice β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž - 49.8
β€Žβ€Žβ€Žβ€Žβ€Žβ€Žβ€Žβ€Žβ€Žβ€Žβ€Žβ€Žβ€Žβ€Žβ€Žβ€Žβ€Žβ€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€¦β€Žand so on

Thank you so much!

See - PHP: array_keys - Manual and PHP: array_combine - Manual

Hello there, thank you very much for your response! But for some reason, this is what the output says:
image

I tried both array_values and array_keys but both of the output remains the same. So sorry for being troublesome :sweat_smile:

Produced this output when I tried it -

Water 48.6
Ethanol 47.3
Olive oil 42.9
Ice 49.8
Fused silica 43.2
PMMA 42.2
Window glass 41.1
Polycarbonate 39.3
Sapphire 34.4
Diamond 24.4
1 Like

Hmm the closest results I can get is this-

Are there any additional functions I have to add to make it show under the table?

Here’s my current progress:
unset($refractiveIndex[$value]);
$value_switch = array_combine($refractiveIndex, $critAngle);

and-

   <tr>
  <td style='border: 1px solid #000;'><?php echo $key; ?></td>
  <td style='border: 1px solid #000;'><?php echo $value_switch; ?></td>  
   </tr>

Thanks a lot in advance !!

You need to combine the keys of $Refractive_Index with the values of $Critical_Angle. As phpdr said you can get the keys of $Refractive_Index using array_keys, then combine them both using array_combine, like so:

$materialNames = array_keys($refractiveIndex);
$materialsToCriticalAngles = array_combine($materialNames, $criticalAngles);

$materialsToCriticalAngles will now look something like:

Array
(
    [Water] => 48.6
    [Ethanol] => 47.3
    [Olive oil] => 42.9
    [Ice] => 49.8
    [Fused silica] => 43.2
    [PMMA] => 42.2
    [Window glass] => 41.1
    [Polycarbonate] => 39.3
    [Sapphire] => 34.4
    [Diamond] => 24.4
)
1 Like

No.

What does array_combine() do? It uses the first array parameter as the keys for the resultant array and it uses the second array parameter as the values for the resultant array. How do you get the first array parameter to be the keys of the $Refractive_Index array? Use array_keys().

foreach(array_combine(array_keys($Refractive_Index),$Critical_Angle) as $key =>$value)
// the rest of your foreach {...} code here...
1 Like

Ahh, I understand now. Thank you for your detailed explanation @phdr, @skawid:slight_smile: The output now looks like how I wanted it to be too, thank you so much!

Sponsor our Newsletter | Privacy Policy | Terms of Service