Jump to content

Working with PHP Arrays


ekuemoah
 Share

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" }

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...