Jump to content

[SOLVED] match an array with a date and print the result


galayman

Recommended Posts

Hi All,

 

i have this array

$ary = array(

"Today date is 1, our breakfast is 2 eggs and salad",

"Today date is 13, our breakfast is 3 breads and beff",

"Today date is 18, our breakfast is 2 toasts with egg",

"Today date is 21, our breakfast is 5 wafels with honey",

"Today date is 30, our breakfast is a bowl of sereal"

);

 

i need to search and match the date (that i have bolded it) and then compare it to today date using this fuction date("j");

 

to find the date in the string i'm using this regex ([1-3][0-9]?)(?=, our)

 

i have try everything but i cannot solve it

so in the end, if today date is 18 i want to display only this string

 

Today date is 18, our breakfast is 2 toasts with egg

 

can i do this?

Thank You , Best Regards

You can try this.

 

<?php

$ary = array(
"Today date is 1, our breakfast is 2 eggs and salad",
"Today date is 13, our breakfast is 3 breads and beff",
"Today date is 18, our breakfast is 2 toasts with egg",
"Today date is 21, our breakfast is 5 wafels with honey",
"Today date is 30, our breakfast is a bowl of sereal"
);

$date = date("j");

foreach($ary AS $ara){
$ex = explode(" ", $ara);
$ex = str_replace(",", NULL, $ex);

if($ex[3] == $date){
	$what = $ara;
}
}

echo $what;

?>

 

tested to some extent.

Can you change your array? If you can change it to:

<?php
$ary = array(1 => '2 eggs and salad',
                  13 => '3 breads and beff',
                  18 => '2 toasts with egg',
                  21 => '5 wafels with honey',
                  31 => 'a bowl of cereal');
?>

 

The the solution is very easy.

 

<?php
$date = date('j');
echo 'Today is ' . $date. ', our breakfast is ' . $ary($j);
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.