Jump to content

Problem with Array


Go to solution Solved by adam_bray,

Recommended Posts

Hello, I have come across a small snag that I am not sure how to get around. I am getting this error. 

 

Warning: in_array() expects at most 3 parameters, 7 given in /home4/mesquiu0/public_html/inc-weather_radio_alert.php on line 78

 

 

Now, I know why I am getting this error. I am parsing and XML file to create the variable to get the values for the array. Now this is where the problem comes in. Since there is 6 possible values I am trying to pull there is only 3 values being produced at the time by the variable from the XML file at the time. 

 

So where I am stuck is how can I set this array up to look for these values when they are present and otherwise ignore it when they aren't as they won't always be present so it doesn't throw an error?

 

What I am trying to do is if the variable from the XML file produces said values in the array then show this. This meaning echo a message. 

 

Here is the array

//If alerts are in the source, set alert message

$event = array(
    $event => "Flash Flood Watch",
    $event => "Flash Flood Warning",
    $event => "Severe Thunderstorm Watch",
    $event => "Severe Thunderstorm Warning",
    $event => "Torndao Watch",
    $event => "Torndao Warning"
);

if (in_array("Flash Flood Watch", "Flash Flood Warning", "Severe Thunderstorm Watch", "Severe Thunderstorm Warning", "Torndao Watch", "Torndao Warning", $event))
  {
  echo "Match found";
  }

the variable $event is created from parsing the XML file which has the possible values but, if the XML doesn't contain those values at the time then I get that error. So how would I need to address this array so I don't get the error but the array still looks for those values if present and echo the statement? 

 

-Thanks

Link to comment
https://forums.phpfreaks.com/topic/287881-problem-with-array/
Share on other sites

i think your code should be like this

$event = array("Flash Flood Watch", "Flash Flood Warning", "Severe Thunderstorm Watch", "Severe Thunderstorm Warning", "Torndao Watch", "Torndao Warning");
$searchvalue = "Flash Flood Watch"; //as example

if (in_array($searchvalue, $event))
{
    echo "Match found";
}

also go through in_array() function properly

http://www.php.net/in_array

Link to comment
https://forums.phpfreaks.com/topic/287881-problem-with-array/#findComment-1476649
Share on other sites

i think your code should be like this

$event = array("Flash Flood Watch", "Flash Flood Warning", "Severe Thunderstorm Watch", "Severe Thunderstorm Warning", "Torndao Watch", "Torndao Warning");
$searchvalue = "Flash Flood Watch"; //as example

if (in_array($searchvalue, $event))
{
    echo "Match found";
}

also go through in_array() function properly

http://www.php.net/in_array

 

Thank you but I am not sure what you did. You added....

 

$searchvalue = "Flash Flood Watch"; //as example

 

but I don't see what that does or the purpose of that. How do you get the value? I don't understand the logic. 

Link to comment
https://forums.phpfreaks.com/topic/287881-problem-with-array/#findComment-1476670
Share on other sites

"I am parsing an XML file to create the variable to get the values for the array."

 

Would you give us an abridged XML string that represents the source of your parsing?

 

For example:

<notice><time></time><type></type><event>Flash Flood Watch</event></notice>

 

You have parsed this string and the result from having found the <event> node, and assigning its value to a variable, would be as:

$searchValue = "Flash Flood Watch";

 

$searchValue is considered the 'needle', and an array of possible values is considered the 'haystack'. So, the task is to determine whether this 'needle' is in the 'haystack'.

in_array('needle', 'haystack')

Edited by bsmither
Link to comment
https://forums.phpfreaks.com/topic/287881-problem-with-array/#findComment-1476706
Share on other sites

Ok, let me see if I can explain this better as I am just not the best at explaining things but, I will do my best. 

 

 

This is just a small snipped but the primary core of the XML parsing. 

 

//Set initial output to false
$tData = false;
$entries = simplexml_load_file($data);
if(count($entries)):
    //Registering NameSpace
    $entries->registerXPathNamespace('prefix', 'http://www.w3.org/2005/Atom');
    $result = $entries->xpath("//prefix:entry");


    foreach ($result as $entry):
        $event = $entry->children("cap", true)->event;


        include ('inc-alert-colors.php');


       endforeach;
endif;
Now from the snippet I am only parsing this from the XML
 
$event = $entry->children("cap", true)->event;

That gives me the $event variable. Now the event variable could have 120+ possible values for event. But at the most only 1 event would be show maybe 2-3 depending on the situation. 

 

So I created an array for the list of the possible values I want to find and if it finds it then display a message which is this. 

//If alerts are in the source, set alert message

$event = array(
    $event => "Flood Watch",
    $event => "Flood Warning",
    $event => "Flash Flood Watch",
    $event => "Flash Flood Warning",
    $event => "Severe Thunderstorm Watch",
    $event => "Severe Thunderstorm Warning",
    $event => "Tornado Watch",
    $event => "Tornado Warning"
);

if (in_array("Flood Watch", "Flood Warning", "Flash Flood Watch", "Flash Flood Warning", "Severe Thunderstorm Watch", "Severe Thunderstorm Warning", "Tornado Watch", "Tornado Warning", $event))
  {
  echo "Match Found!";
  }

So basically that means out of the possible 120+ values from $event only look for those and if those are found echo a message. 

 

That leads me to these errors which I have already explained in my first post so look back at it to get the details. 

 

 

Warning: Illegal offset type in /home4/mesquiu0/public_html/inc-weather_radio_alert.php on line 73

 

Warning: in_array() expects at most 3 parameters, 9 given in /home4/mesquiu0/public_html/inc-weather_radio_alert.php on line 83

 

 

 

So my objective is out of the 120+ possible events find those certain ones and if those certain ones are found then display a message. If not, do nothing. 

 

It is pretty simple and straight forward but not sure how to get around this small snag. 

Link to comment
https://forums.phpfreaks.com/topic/287881-problem-with-array/#findComment-1476722
Share on other sites

How is something like this?

 

 

//Set initial output to false
$tData = false;
$entries = simplexml_load_file($data);
if(count($entries)):
    //Registering NameSpace
    $entries->registerXPathNamespace('prefix', 'http://www.w3.org/2005/Atom');
    $result = $entries->xpath("//prefix:entry");

//no need to include files over and over in a loop, keep outside the loop
include ('inc-alert-colors.php');
//create an array of desired warnings
$warnings = array("Flash Flood Watch", "Flash Flood Warning", "Severe Thunderstorm Watch", "Severe Thunderstorm Warning", "Torndao Watch", "Torndao Warning");
    foreach ($result as $entry){
        $event = trim($entry->children("cap", true)->event);
 
  if (in_array($warnings, $event)){
 
  //need to set it's colors?
  switch ($event) {
    case "Flash Flood Watch":
        $color = "limegreen";
        break;
    case "Flash Flood Warning":
        $color = "darkred";
        break;
    case "Severe Thunderstorm Watch":
        $color = "palevioletred";
        break;
case "Severe Thunderstorm Warning":
        $color = "orange";
        break;
case "Torndao Watch":
        $color = "yellow";
        break;
case "Torndao Warning":
        $color = "red";
        break;
        } 
 
            echo "<p style='color:".$color.";'>".$event."</p>";
     }      

    }
}
Link to comment
https://forums.phpfreaks.com/topic/287881-problem-with-array/#findComment-1476729
Share on other sites

Thanks for the suggestion. That doesn't work. The XML parsing part is fine, there is not problem with that. I just need to be able to search for a certain alert and if that alert is presence in the event variable from the XML then display a message.

 

Now this partly works. 

$radiowarning = "<div class='{$bannerStyle}'><b>SEVERE WEATHER ALERT - {$event}</b> ... Listen now to our LIVE NOAA Weather Radio Stream <a href='{$feed}'>[Click here to listen]</a></div>";


//If alerts are in the source, set alert message


$alerts = array($event);
$searchvalue = "Flood Warning";


if (in_array($searchvalue, $alerts))
{
    echo $radiowarning;
}
Problem is with this line "$searchvalue = "Flood Warning"; " I can't enter more than one value I need for it to look for in the event variable otherwise it gives me this error. 
 
I need it to be like this...

$searchvalue = "Flood Watch", "Flood Warning", "Flash Flood Watch", "Flash Flood Warning", "Severe Thunderstorm Watch", "Severe Thunderstorm Warning", "Tornado Watch", "Tornado Warning";

I have tried the whole needle and haystack method but the problem is as above. It will only look for one value. I can't have it look for multiple values. 

 

I just need a method to search for a certain alert which is created from the $event variable that is created from parsing the XML. If any of those alerts are present. I.E. in this case...

"Flood Watch", "Flood Warning", "Flash Flood Watch", "Flash Flood Warning", "Severe Thunderstorm Watch", "Severe Thunderstorm Warning", "Tornado Watch", "Tornado Warning"

Then display this message, echo $radiowarning;

 

Which that variable is this. 

$radiowarning = "<div class='{$bannerStyle}'><b>SEVERE WEATHER ALERT - {$event}</b> ... Listen now to our LIVE NOAA Weather Radio Stream <a href='{$feed}'>[Click here to listen]</a></div>";

It should be something really easy in logic but for some reason I am really struggling with it. If X statement exists then echo X, is pretty much the gist of it where $events supplies the values to look for but, keep in mind, the values might now always be there because it is an XML file and changes constantly. 

 

Here is a test page. Works with one value but anymore it doesn't. 

 

http://www.mesquiteweather.net/wxtest.php

Edited by Texan78
Link to comment
https://forums.phpfreaks.com/topic/287881-problem-with-array/#findComment-1476794
Share on other sites

I am confused about the XML source. Are you saying that after having parsed the XML source, the results of that parsing could contain more than one 'event'?

 

So, after parsing, you end up with an array of found 'events':

print_r($events):

$events: Array(

[0] => "Flood Watch"

[1] => "Severe Thunderstorm Watch"

[2] => "Tornado Watch"

)

 

Now, you need to compare the $events array against the $warnings array. If any element in $events can be matched to any element in $warnings, then return those matches.

 

If this is the case, please read array_intersect.

Link to comment
https://forums.phpfreaks.com/topic/287881-problem-with-array/#findComment-1476795
Share on other sites

  • Solution

Can't you use a foreach?

$radiowarning = '
		<div class="'.$bannerStyle.'">
			<strong>SEVERE WEATHER ALERT - '.$event.'</strong> ... Listen now to our LIVE NOAA Weather Radio Stream <a href="'.$feed.'">[Click here to listen]</a>
		</div>';


	//If alerts are in the source, set alert message
	
	
	$alerts = array($event);
	
	$searchvalue = array('Flood Watch', 'Flood Warning', 'Flash Flood Watch', 'Flash Flood Warning', 'Severe Thunderstorm Watch', 'Severe Thunderstorm Warning', 'Tornado Watch', 'Tornado Warning');
	
	foreach( $searchvalue as $alert )
	{
		if (in_array($alert, $alerts))
		{
			echo $radiowarning;
		}
	}
Link to comment
https://forums.phpfreaks.com/topic/287881-problem-with-array/#findComment-1476798
Share on other sites

 

Can't you use a foreach?

$radiowarning = '
		<div class="'.$bannerStyle.'">
			<strong>SEVERE WEATHER ALERT - '.$event.'</strong> ... Listen now to our LIVE NOAA Weather Radio Stream <a href="'.$feed.'">[Click here to listen]</a>
		</div>';


	//If alerts are in the source, set alert message
	
	
	$alerts = array($event);
	
	$searchvalue = array('Flood Watch', 'Flood Warning', 'Flash Flood Watch', 'Flash Flood Warning', 'Severe Thunderstorm Watch', 'Severe Thunderstorm Warning', 'Tornado Watch', 'Tornado Warning');
	
	foreach( $searchvalue as $alert )
	{
		if (in_array($alert, $alerts))
		{
			echo $radiowarning;
		}
	}

 

Yes, that seems to work perfect. I completely over looked the foreach.

 

The last thing I can't seem to figure out which looks right is the variable for the CSS styling isn't applying. Does anyone see anything wrong?

$bannerStyle = 'color: #FFF; width:100%; border: #fff 1px solid; font-size:11px; font-weight:normal; font-family: Arial,Helvetica,sans-serif; text-align: center; background-color:' .$alertColor. '; margin-bottom:5px; padding: .2em 0em .2em 0em';

$radiowarning = '
		<div class="'.$bannerStyle.'">
			<strong>SEVERE WEATHER ALERT - </strong>Listen now to our LIVE NOAA Weather Radio Stream <a href="'.$feed.'" onclick="return popup(this, "noaa")" title="Live NOAA Radio For Dallas County">[Click here to listen]</a>
		</div>';
Link to comment
https://forums.phpfreaks.com/topic/287881-problem-with-array/#findComment-1476806
Share on other sites

Nevermind, I got it sorted. It should be style instead of class like so. All is good now. 

 

$bannerStyle = '"color: #FFF; width:100%; border: #FFF 1px solid; font-size:11px; font-weight:normal; font-family: Arial,Helvetica,sans-serif; text-align: center; background-color:' .$alertColor. '; margin-bottom:5px; padding: .2em 0em .2em 0em"';


$radiowarning = '
<div style='.$bannerStyle.'>
<strong>SEVERE WEATHER ALERT - </strong>Listen now to our LIVE NOAA Weather Radio Stream <a href="'.$feed.'" onclick="return popup(this, "noaa")" title="Live NOAA Radio For Dallas County">[Click here to listen]</a>
</div>';
Link to comment
https://forums.phpfreaks.com/topic/287881-problem-with-array/#findComment-1476807
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.