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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.