array help

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

Post Reply
SurfScape
Forum Newbie
Posts: 4
Joined: Mon Dec 15, 2003 2:50 am

array help

Post by SurfScape »

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
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

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

Solution

Post by SurfScape »

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++ ) &#123;
      $pay_types&#1111;$keys&#1111;$i]]&#1111;'value'] = sprintf("%01.2f",$values&#1111;$keys&#1111;$i]]);
   &#125;
&#125;
$_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
(
    &#1111;E001] => Array
        (
            &#1111;earnings_code] => E001
            &#1111;description] => Regular Hours/Earnings
            &#1111;earnings_type] => hours
            &#1111;value] => 40.00
        )

    &#1111;E021] => Array
        (
            &#1111;earnings_code] => E021
            &#1111;description] => Overtime (1.5)
            &#1111;earnings_type] => hours
            &#1111;value] => 20.00
        )

    &#1111;E022] => Array
        (
            &#1111;earnings_code] => E022
            &#1111;description] => Double (2.0)
            &#1111;earnings_type] => hours
            &#1111;value] => 10.00
        )

    &#1111;E01S] => Array
        (
            &#1111;earnings_code] => E01S
            &#1111;description] => Sick Pay
            &#1111;earnings_type] => mixed
            &#1111;value] => 5.00
        )

    &#1111;E012] => Array
        (
            &#1111;earnings_code] => E012
            &#1111;description] => Vacation Pay
            &#1111;earnings_type] => mixed
            &#1111;value] => 2.00
        )

    &#1111;E09B] => Array
        (
            &#1111;earnings_code] => E09B
            &#1111;description] => Holiday
            &#1111;earnings_type] => hours
            &#1111;value] => 1.00
        )

)
-Garrett
Post Reply