Jump to content

Working with PHP Arrays


ekuemoah

Recommended Posts

I am working with the following code on codepad.org<?php$office = array('Mckenzie','Aberdeen');$office_wage = array(10,10);$warehouse = array('Martin','Alison','Cuptech','Kroger');$warehouse_wage = array(12,12,12,12);$oEmployees = array_combine($office,$office_wage);$oWarehouse = array_combine($warehouse,$warehouse_wage);$allEmployees = array_merge($oEmployees,$oWarehouse);//print_r($allEmployees);$U=array_values($allEmployees);$R=array_flip($allEmployees);print_r($R);?>The output I got was the following:Array( [10] => Aberdeen [12] => Kroger)It outputs the last values in each array when the values are the same. It treats it like the values are duplicates. How do I treat each array value as unique so that I get a print out of everything.I want to get the following results:Array( [10] => Mckenzie [10] => Aberdeen [12] => Martin [12] => Alison [12] => Cuptech [12] => Kroger)

Link to comment
Share on other sites

The names seem to represent the employees, and the number seem to represent their wages. So, it would make more sense to use the employees as keys, and the wages as values. You don't need the second array, that is just an inverted version of the first array. Instead, use array_search() or a foreach($allEmployees as $key => $value) statement.

<?php$office = array('Mckenzie','Aberdeen');$office_wage = array(10,10);$warehouse = array('Martin','Alison','Cuptech','Kroger');$warehouse_wage = array(12,12,12,12);$oEmployees = array_combine($office,$office_wage);$oWarehouse = array_combine($warehouse,$warehouse_wage);$allEmployees = array_merge($oEmployees,$oWarehouse);print_r($allEmployees);//$U=array_values($allEmployees);//$R=array_flip($allEmployees);//print_r($R);echo array_search(12, $allEmployees);//will only show one employee: Martinforeach($allEmployees as $key => $value){ echo '[',$value,] => ',$key,"\n" }

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...