PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
SurfScape
Forum Newbie
Posts: 4 Joined: Mon Dec 15, 2003 2:50 am
Post
by SurfScape » Mon Dec 15, 2003 2:50 am
I have 2 arrays:
Code: Select all
Array
(
їemployee_id] => 13351
їdata] => Array
(
їE001] => 40
їE021] => 10
їE022] =>
їE09B] =>
)
and
Code: Select all
Array
(
їE001] => Array
(
їearnings_code] => E001
їdescription] => Regular Hours/Earnings
їearnings_type] => hours
)
їE021] => Array
(
їearnings_code] => E021
їdescription] => Overtime (1.5)
їearnings_type] => hours
)
їE022] => Array
(
їearnings_code] => E022
їdescription] => Double (2.0)
їearnings_type] => hours
)
їE09B] => Array
(
їearnings_code] => E09B
їdescription] => Holiday
їearnings_type] => hours
)
)
I want to place the values from the data array into the main array
so as to look like this:
Code: Select all
Array
(
їE001] => Array
(
їearnings_code] => E001
їdescription] => Regular Hours/Earnings
їearnings_type] => hours
їvalue] => 40
)
їE021] => Array
(
їearnings_code] => E021
їdescription] => Overtime (1.5)
їearnings_type] => hours
їvalue] => 10
)
їE022] => Array
(
їearnings_code] => E022
їdescription] => Double (2.0)
їearnings_type] => hours
їvalue] =>
)
їE09B] => Array
(
їearnings_code] => E09B
їdescription] => Holiday
їearnings_type] => hours
їvalue] =>
)
)
Thanks, in advance, I'm either too tired or too stupid to figure this out right now.
-Garrett
DuFF
Forum Contributor
Posts: 495 Joined: Tue Jun 24, 2003 7:49 pm
Location: USA
Post
by DuFF » Mon Dec 15, 2003 7:20 am
Well I've found a start but you will need to change somethings. Heres what I did:
If you start with these 2 arrays
Code: Select all
Array
(
їemployee_id] => 13351
їE001] => 40
їE021] => 10
їE022] =>
їE09B] =>
)
Array
(
їE001] => Array
(
їearnings_code] => E001
їdescription] => Regular Hours/Earnings
їearnings_type] => hours
)
їE021] => Array
(
їearnings_code] => E021
їdescription] => Overtime (1.5)
їearnings_type] => hours
)
їE022] => Array
(
їearnings_code] => E022
їdescription] => Double (2.0)
їearnings_type] => hours
)
їE09B] => Array
(
їearnings_code] => E09B
їdescription] => Holiday
їearnings_type] => hours
)
)
And then use $result = array_merge_recursive($array1, $array2);
The $result will be:
Code: Select all
Array
(
їemployee_id] => 13351
їE001] => Array
(
ї0] => 40
їearnings_code] => E001
їdescription] => Regular Hours/Earnings
їearnings_type] => hours
)
їE021] => Array
(
ї0] => 10
їearnings_code] => E021
їdescription] => Overtime (1.5)
їearnings_type] => hours
)
їE022] => Array
(
ї0] =>
їearnings_code] => E022
їdescription] => Double (2.0)
їearnings_type] => hours
)
їE09B] => Array
(
ї0] =>
їearnings_code] => E09B
їdescription] => Holiday
їearnings_type] => hours
)
)
Obviously, you'll want to find some way of making the [0] of being "value" and possibly deleting the "employee_id".
SurfScape
Forum Newbie
Posts: 4 Joined: Mon Dec 15, 2003 2:50 am
Post
by SurfScape » Mon Dec 15, 2003 5:30 pm
Here is the solution:
Code: Select all
if ( is_array( $_POSTї'data'] ) ) {
$values = $_POSTї'data'];
$keys = array_keys($_POSTї'data']);
for ($i=0; $i < count($keys); $i++ ) {
$pay_typesї$keysї$i]]ї'value'] = sprintf("%01.2f",$valuesї$keysї$i]]);
}
}
$_POST['data'] was the 1st small array and
$pay_types was the large array.
Added the sprintf function to format the values.
The resulting array is:
Code: Select all
Array
(
їE001] => Array
(
їearnings_code] => E001
їdescription] => Regular Hours/Earnings
їearnings_type] => hours
їvalue] => 40.00
)
їE021] => Array
(
їearnings_code] => E021
їdescription] => Overtime (1.5)
їearnings_type] => hours
їvalue] => 20.00
)
їE022] => Array
(
їearnings_code] => E022
їdescription] => Double (2.0)
їearnings_type] => hours
їvalue] => 10.00
)
їE01S] => Array
(
їearnings_code] => E01S
їdescription] => Sick Pay
їearnings_type] => mixed
їvalue] => 5.00
)
їE012] => Array
(
їearnings_code] => E012
їdescription] => Vacation Pay
їearnings_type] => mixed
їvalue] => 2.00
)
їE09B] => Array
(
їearnings_code] => E09B
їdescription] => Holiday
їearnings_type] => hours
їvalue] => 1.00
)
)
-Garrett