Jump to content

Recommended Posts

Can anyone suggest a method for merging two different types of arrays?

 

My first array:

 

[0] => Array

        (

            [1] => Array

                (

                    [time] => 1183638600

                    [type] => middle

                    [name] =>

                    [date] => 8:30

                    [bgcolor] => 1

                )

 

            [2] => Array

                (

                    [time] => 1183640400

                    [type] => middle

                    [name] =>

                    [date] => 9:00

                    [bgcolor] => 2

                )

 

I need to look like this:

 

[12] => Array

        (

            [time] => 1183647600

            [type] =>

            [name] =>

            [date] => 11:00

            [bgcolor] =>

        )

 

    [13] => Array

        (

            [time] => 1183649400

            [type] =>

            [name] =>

            [date] => 11:30

            [bgcolor] =>

        )

 

So that I can merge the two to look like the second one. 

 

Any suggestions?

Thanks for your help, but neither worked.

 

This solution:

 

foreach ($first_array as $key => $array) {

    $new_array[$key] = $array;

}

 

created the same array I had in the first place, which was:

 

[0] => Array

        (

            [1182519000] => Array

                (

                    [time] => 1182519000

                    [type] => Middle

                    [date] => 9:30

                    [bgcolor] => 0

                )

 

            [1182520800] => Array

                (

                    [time] => 1182520800

                    [type] => Middle

                    [date] => 10:00

                    [bgcolor] => 0

                )

 

            [1182522600] => Array

                (

                    [time] => 1182522600

                    [type] => Middle

                    [date] => 10:30

                    [bgcolor] => 0

                )

 

        )

 

    [1] => Array

        (

            [1182529800] => Array

                (

                    [time] => 1182529800

                    [type] => Middle

                    [date] => 12:30

                    [bgcolor] => 1

                )

 

            [1182531600] => Array

                (

                    [time] => 1182531600

                    [type] => Middle

                    [date] => 13:00

                    [bgcolor] => 1

                )

 

        )

 

The second solution:

$new_array=$old_array[0];

 

gave me

 

[1182519000] => Array

        (

            [time] => 1182519000

            [type] => Middle

            [date] => 9:30

            [bgcolor] => 0

        )

 

    [1182520800] => Array

        (

            [time] => 1182520800

            [type] => Middle

            [date] => 10:00

            [bgcolor] => 0

        )

 

    [1182522600] => Array

        (

            [time] => 1182522600

            [type] => Middle

            [date] => 10:30

            [bgcolor] => 0

        )

 

which was only one of the two keys I needed.

 

I am stumped.  I've been working on this all weekend (ignoring my husband) going through several different looping situations to try to solve it, but haven't been able to.

 

Any other suggestions?

 

My merged array looks like this:

 

Var export of array:

array (
  0 => 
  array (
    1182529800 => 
    array (
      'time' => 1182529800,
      'type' => 'Middle',
      'date' => '12:30',
      'bgcolor' => 1,
    ),
    1182531600 => 
    array (
      'time' => 1182531600,
      'type' => 'Middle',
      'date' => '13:00',
      'bgcolor' => 1,
    ),
  ),
  1 => 
  array (
    1182519000 => 
    array (
      'time' => 1182519000,
      'type' => 'Middle',
      'date' => '9:30',
      'bgcolor' => 0,
    ),
    1182520800 => 
    array (
      'time' => 1182520800,
      'type' => 'Middle',
      'date' => '10:00',
      'bgcolor' => 0,
    ),
    1182522600 => 
    array (
      'time' => 1182522600,
      'type' => 'Middle',
      'date' => '10:30',
      'bgcolor' => 0,
    ),
  ),
  2 => 
  array (
    'time' => '1182517200',
    'type' => 'Start',
    'date' => '9:00',
    'bgcolor' => 0,
  ),
  3 => 
  array (
    'time' => '1182524400',
    'type' => 'End',
    'date' => '11:00',
    'bgcolor' => 0,
  ),
  4 => 
  array (
    'time' => '1182528000',
    'type' => 'Start',
    'date' => '12:00',
    'bgcolor' => 1,
  ),
  5 => 
  array (
    'time' => '1182533400',
    'type' => 'End',
    'date' => '13:30',
    'bgcolor' => 1,
  ),
)

 

Index 0 and 1 above need to be on the same dimension as 2 and above so that I can sort them put them into a table.

 

What I am trying to do is make a day view of a calendar that would eventually look like this:

 

 

9:00Start

9:30Middle

10:00Middle

10:30Middle

11:00End

12:00Start

12:30Middle

13:00Middle

13:30End

 

 

And my current approach to get the day view (after several attempts at different approaches!) is to make arrays of the start and end time (which I have in a database) and then merge them, which works fine except that to create the middle range I have to create an additional array which calculates a range of middle times for each record in the database (so, for example, the first record with a start and end time in my database created 3 middle range elements) and, as you can see from above, that turns out as an associative array where as the start and end arrays are not.  Therein lies my dilemma.

try

<?php
$old_arraj = array (
  0 => 
  array (
    1182529800 => 
    array (
      'time' => 1182529800,
      'type' => 'Middle',
      'date' => '12:30',
      'bgcolor' => 1,
    ),
    1182531600 => 
    array (
      'time' => 1182531600,
      'type' => 'Middle',
      'date' => '13:00',
      'bgcolor' => 1,
    ),
  ),
  1 => 
  array (
    1182519000 => 
    array (
      'time' => 1182519000,
      'type' => 'Middle',
      'date' => '9:30',
      'bgcolor' => 0,
    ),
    1182520800 => 
    array (
      'time' => 1182520800,
      'type' => 'Middle',
      'date' => '10:00',
      'bgcolor' => 0,
    ),
    1182522600 => 
    array (
      'time' => 1182522600,
      'type' => 'Middle',
      'date' => '10:30',
      'bgcolor' => 0,
    ),
  ),
  2 => 
  array (
    'time' => '1182517200',
    'type' => 'Start',
    'date' => '9:00',
    'bgcolor' => 0,
  ),
  3 => 
  array (
    'time' => '1182524400',
    'type' => 'End',
    'date' => '11:00',
    'bgcolor' => 0,
  ),
  4 => 
  array (
    'time' => '1182528000',
    'type' => 'Start',
    'date' => '12:00',
    'bgcolor' => 1,
  ),
  5 => 
  array (
    'time' => '1182533400',
    'type' => 'End',
    'date' => '13:30',
    'bgcolor' => 1,
  ),
);
$new_array =array();
foreach ($old_arraj as $old) if (array_key_exists('time',$old)) $new_array[] = $old; else $new_array = array_merge($new_array,$old);
array_multisort($new_array);
foreach ($new_array as $value) echo $value['date'], ' - ', $value['type'], "<br />\n";
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.