0

I would need to combine two different fields. In the first field I generate days of the month. I want to list all days of the month. I would like to add a second field to them, where there are items for each day. But, for example, there are no items on weekends or on other days. Ie. that field two will always have fewer items. The second field is tightened from the DB. I would need to do a JOIN like in MySQL for the first field.

It occurred to me that in MySQL it would be possible to make a temporary table with a given month and link it here, but I don't think it's right.

$arrayDate = [0 => '20210401',1 => '20210402',2 => '20210403',3 => '20210404',4 => '20210405',5 => '20210406',6 => '20210407',7 => '20210408',8 => '20210409',9 => '20210410',10 => '20210411',11 => '20210412',12 => '20210413',13 => '20210414',14 => '20210415',15 => '20210416',16 => '20210417',17 => '20210418',18 => '20210419',19 => '20210420',20 => '20210421',21 => '20210422',22 => '20210423',23 => '20210424',24 => '20210425',25 => '20210426',26 => '20210427',27 => '20210428',28 => '20210429',29 => '20210430'];
$arrayItem[35] = ['id' => 35, 'date' => '20210401', 'item' => 'aaaa'];
$arrayItem[36] =  ['id' => 36, 'date' => '20210402', 'item' => 'bbbb'];
$arrayItem[37] =  ['id' => 36, 'date' => '20210430', 'item' => 'cccc'];

// i need output
20210401 - aaaa
20210402 - bbbb
20210403 - empty
20210404 - empty
...
20210430 - cccc

EDIT: I use nested loops, but I still can't get the right output

    foreach ($arrayDate as $date) {
        foreach ($arrayItem as $item) {
            if ($date == $item['date']) {
                bdump($item['date']);
            } else {
                bdump($date);
            }
        }
    }

bdump($item['date']) = '20210401', '20210402', '20210430'
bdump($date) = '20210401', '20210401', '20210402', '20210402', '20210403', '20210403', '20210403', '20210404', '20210404', '20210404', '20210405', '20210405', '20210405' ....
4
  • Use nested loops. The main loop loops over $arrayDate. The inner loop loops over $arrayItem, searching for the matching date. If it finds it, it prints the item. Commented Apr 14, 2021 at 23:19
  • In the database you would do this with a LEFT JOIN between the two tables. Commented Apr 14, 2021 at 23:20
  • To make it more efficient, you can convert $arrayItem into an associative array that uses the date as the key. Then you don't need the inner loop, just access the correspond element of the associative array. Commented Apr 14, 2021 at 23:20
  • Can you please give an example of how convert $arrayItem into an associative array that uses the date. Thx Commented Apr 15, 2021 at 8:00

1 Answer 1

1

With array_column you create a array from $arrayItem with date as key. $dateItem is an array like

array (
  20210401 => "aaaa",
  20210402 => "bbbb",
  20210430 => "cccc",
)

The output you can do with a simple foreach.

$dateItem = array_column($arrayItem,'item','date');
foreach($arrayDate as $date){
  echo $date.' '.($dateItem[$date] ?? 'empty')."<br>\n";
}

Note:

With

array_column($arrayItem,null,'date') 

you get a two-dimensional array with a date as a key that can be used.

array (
  20210401 => 
  array (
    'id' => 35,
    'date' => "20210401",
    'item' => "aaaa",
  ),
  20210402 => 
  array (
    'id' => 36,
    'date' => "20210402",
    'item' => "bbbb",
  ),
  20210430 => 
  array (
    'id' => 36,
    'date' => "20210430",
    'item' => "cccc",
  ),
)
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, but how do I assign this for all days? E.g. April is 30 days old. I need to view all 30 days and if there is a match, assign the items. Array $arrayItem actually contains more properties that I need to get to. That's the way I would lose them.
The solution shows it for all days. $arrayItem can also be larger. The 'date' field must always be unique.
I'm sorry, it works. Great. How could I get an id in $arrayItem, for example, and possibly other items such as color, weight? Thx
Thank you once again. I added $dateItem2 = array_column($arrayItem,'id','date'); and it works.
Yes. Alternatively, see my note in the answer.

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.