0

I try to explain myself:

I have 2 arrays, one of them is it:

Array(
[0] => 01/09/2014
[1] => 02/09/2014
[2] => 03/09/2014
[3] => 04/09/2014
[4] => 05/09/2014
[5] => 06/09/2014
[6] => 07/09/2014
[7] => 08/09/2014
[8] => 09/09/2014
[9] => 10/09/2014
[10] => 11/09/2014
[11] => 12/09/2014
[12] => 13/09/2014
[13] => 14/09/2014
[14] => 15/09/2014
[15] => 16/09/2014
[16] => 17/09/2014
[17] => 18/09/2014
[18] => 19/09/2014
[19] => 20/09/2014
[20] => 21/09/2014
[21] => 22/09/2014
[22] => 23/09/2014
[23] => 24/09/2014
[24] => 25/09/2014
[25] => 26/09/2014
[26] => 27/09/2014
[27] => 28/09/2014
[28] => 29/09/2014
[29] => 30/09/2014
)

And the other is:

Array(
[12/09/2014] => Array
    (
        [0] => 17:41:54
        [1] => 17:42:21
        [2] => 17:42:47
        [3] => 17:43:21
        [4] => 17:47:24
        [5] => 17:47:40
        [6] => 17:48:09
        [7] => 17:48:27
        [8] => 18:09:21
        [9] => 18:55:22
        [10] => 20:22:02
        [11] => 23:36:22
        [12] => 23:42:39
        [13] => 17:59:25
    )

[15/09/2014] => Array
    (
        [0] => 13:02:29
    )

[23/09/2014] => Array
    (
        [0] => 9:55:56
        [1] => 10:25:01
    )

[26/09/2014] => Array
    (
        [0] => 9:09:24
        [1] => 9:15:30
        [2] => 10:39:49
        [3] => 19:53:40
    )

[29/09/2014] => Array
    (
        [0] => 8:28:46
    )

[30/09/2014] => Array
    (
        [0] => 8:16:54
        [1] => 10:06:02
        [2] => 10:11:32
        [3] => 14:16:07
        [4] => 16:25:02
    )

[01/10/2014] => Array
    (
        [0] => 8:28:23
        [1] => 9:12:22
        [2] => 9:15:38
        [3] => 10:25:23
    )

I need to build other when if the second array index matches with some first array elements, put the subelements (times), if not, put "There aren't results" o something like this.

Understand?

Example:

Array
(
[01/09/2014] => Array
    (
        [0] => "There aren't results"

    )

[02/09/2014] => Array
    (
        [0] => "There aren't results"
    )

[03/09/2014] => Array
    (
        [0] => "There aren't results"
    )

.
.
.
 [12/09/2014] => Array
    (
         [0] => 17:41:54
         [1] => 17:42:21
         [2] => 17:42:47
         [3] => 17:43:21
         [4] => 17:47:24
         [5] => 17:47:40
         [6] => 17:48:09
         [7] => 17:48:27
         [8] => 18:09:21
         [9] => 18:55:22
         [10] => 20:22:02
         [11] => 23:36:22
         [12] => 23:42:39
         [13] => 17:59:25
    )

 [13/09/2014] => Array
    (
          [0] => "There aren't results"
    )
0

5 Answers 5

2

Just try with array_fill_keys with default value and merge it with data array:

$dates = array(
    '01/09/2014',
    '02/09/2014',
    '03/09/2014',
    '04/09/2014',
);

$times = array(
    '02/09/2014' => array(
        '14:41:54',
        '17:41:54',
    ),
    '04/09/2014' => array(
        '11:41:54',
    ),
);

$output = $times + array_fill_keys($dates, array("There aren't results"));
ksort($output);

var_dump($output);

Output:

array (size=4)
  '01/09/2014' => 
    array (size=1)
      0 => string 'There aren't results' (length=20)
  '02/09/2014' => 
    array (size=2)
      0 => string '14:41:54' (length=8)
      1 => string '17:41:54' (length=8)
  '03/09/2014' => 
    array (size=1)
      0 => string 'There aren't results' (length=20)
  '04/09/2014' => 
    array (size=1)
      0 => string '11:41:54' (length=8)
Sign up to request clarification or add additional context in comments.

2 Comments

Nice and elegant solution!
I like the solution by RichardBernards and now I can hardly understand this solution, but he said it's better than his solution, so, I think telling the truth hehe. Thanks for all answers, it's amazing the quickly of your answers. I'm really impressed...
1

A possible answer could be the following:

<?php

$dates = array();
$information = array();
$result = array();

foreach($dates as $date) {
    if(array_key_exists($date, $information)) {
        $result[$date] = $information[$date];
    } else {
        $result[$date] = array('There aren\'t any results');
    }
}

Ofcourse you'll have to change $dates to your first array and $information to your second array...

3 Comments

It's just what I wanted...Thanks... It was so easy and I couldn't see it haha.
Please also look at the solution provided by @hsz, although it's less readable for beginners, it's the more elegant solution to your problem =)
Yes, I did. But I think your solution is enough for me :)
1

Try this code

$first_arr = array();
$second_arr = array();
$new_array = array();

foreach($first_arr as $key => $value){
  if(isset($second_arr[$value])){
    $new_array[$key] = $second_arr[$value];
  }else{
    $new_array[$key] = array("There aren't results");
  }
}

Comments

1

You can use array_map:

$combined = array_map(function($value) use ($secondArray) {
    if (array_key_exists($value, $secondArray)) {
        return array(
            $value => $secondArray[$value],
        );
    } else {
        return array(
            $value => "There aren't results",
        );
    }
}, $firstArray);

Comments

1
$newA=array();  
foreach ($firstA as $fa)
{
    if(array_key_exists($fa,$secoundA))
    {

        $newA[$fa]=$secoundA[$fa];
    }
    else
    {
        $newA[$fa]="There aren't results";
    }

}
print_r($newA);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.