ekuemoah Posted August 15, 2011 Share Posted August 15, 2011 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 More sharing options...
justsomeguy Posted August 15, 2011 Share Posted August 15, 2011 That's not how arrays work, arrays do not have duplicate keys. I'm not sure quite what you're going for to suggest alternatives, what do the keys represent in your case? What do the names represent? Link to comment Share on other sites More sharing options...
Man In Tan Posted August 16, 2011 Share Posted August 16, 2011 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.