Welcome Guest, Not a member yet? Register   Sign In
help with calendar library
#1

[eluser]jochemvn[/eluser]
My question is more about arrays then about the calendar library. I have 1 calendar, but I want to add data to my calendar from multiple models.

Each model returns an array. How dow I merge these arrays?

Code:
Array 1:
Array
(
    [15] => Appointment with Mom
    [21] => Watching television
)

Array 2:
Array
(
    [12] => Paul's birthday
    [21] => Jenny's birthday
)

How do I merge these 2 array's so they look like this:
Code:
Array 3:
Array
(
    [12] => Paul's birthday
    [15] => Appointment with Mom
    [21] => Watching television
    [21] => Jenny's birthday
)

Using array_merge resets the numbers, which correspond with the day's of a month, so that's no good. I could probably use a foreach to add the elements of the second array to the first, but I'm wondering if there's a single command to get this working.

Any suggestions?
#2

[eluser]Georgi Budinov[/eluser]
Try this:

Code:
$result = $array1 + $array2;
#3

[eluser]jochemvn[/eluser]
holy s... too easy that one Smile

thanx a lot... :red:
#4

[eluser]jochemvn[/eluser]
hmm that didn't quite work out, since array 3 now look like this:

Code:
Array 3:
Array
(
    [12] => Paul's birthday
    [15] => Appointment with Mom
    [21] => Jenny's birthday
)

instead of this:

Code:
Array 3:
Array
(
    [12] => Paul's birthday
    [15] => Appointment with Mom
    [21] => Watching television
    [21] => Jenny's birthday
)

Of course it's not possible to get duplicate entries in an array, however is there a php function that detects duplicate entries and creates an array in an array like this:

Code:
Array 3:
Array
(
    [12] => Paul's birthday
    [15] => Appointment with Mom
    [21] => Array
            (
               [0] => Watching television
               [1] => Jenny's birthday
            )
)

Any suggestions?
#5

[eluser]Georgi Budinov[/eluser]
Yes, the keys in the array get overridden with the values from the second array. There is no easy shortcut for doing exactly what you want as far as I know. You better create your own function iterating through the arrays, merging them as you want and use it.
#6

[eluser]buckit[/eluser]
is this what you are looking for?
Code:
<?php
function merge_arrays($one, $two){
    foreach($two as $key => $val){
        if(array_key_exists($key, $one)){
            $one[$key] = array($val,$one[$key]);
        }else{
            $one[$key] = $val;
        }
    }
  return $one;
}
    

    $one[15] = "Appointment with Mom";
    $one[21] = "Watching television";

    $two[12] = "Paul's birthday";
    $two[21] = "Jenny's birthday";

    $three = merge_arrays($one, $two);

    ?>
result:
Code:
Array ( [15] => Appointment with Mom
                  [21] => Array ( [0] => Jenny's birthday
                                  [1] => Watching television )
                  [12] => Paul's birthday )




Theme © iAndrew 2016 - Forum software by © MyBB