I have this array
Array
(
[result] => Array
(
[status] => nok
[reason] => Character not found.
)
[code] => 404
[content_type] => application/json;charset=utf-8
)
I want to check the [status], if it is "nok" it should appear "Match found".
My code:
$info = $r['result']['status'];
if (in_array("nok", $info))
{
echo "Match found";
}
else
{
echo "Match not found";
}
?>
the function in_array check if the value exists in an array, you don't give an array but a string.
You have two options:
change you code to this:
if (isset($r['result']) && in_array("nok", $r['result'])) { //add isset to not raise a warning when it doesn't exists
Or if the result is always the same you can do this:
if (isset($r['result']['status']) && $r['result']['status'] === 'nok') { //add isset to not raise a warning when it doesn't exists
In your case:
$r['result']['status']
is not an array but a string.
So you can ask for:
if(in_array("nok", $r['result']))
or
if($r['result']['status'] == "nok")
$info is not an array, it's a string. Try this instead:
if (in_array("nok", $r['result']))
{
echo "Match found";
}
else
{
echo "Match not found";
}
Or
if ($r['result']['status'] == "nok"){
You don't need to use in_array in this code. in_array searches a linear array for string values... so this is how you would use it:
$arr = array("dog", "cat", "mouse");
if(in_array("mouse", $arr)) echo "Eek, a mouse!";
You can just compare using normal logic since you are comparing a string to a string:
$info = $r['result']['status'];
if ($info == "nok")
{
echo "Match found";
}
else
{
echo "Match not found";
}
Related
I have this main sting.
S,SR,DSR,DS,FX,FXS,SR,DS,S,SR,DS,FX,S,SR,DS,FX,FXS
and i want to find each of the following strings ..
DSR and FXS
i have tried by following code but it can not given me perfect result.
code...
<?php
$mainstring ="S,SR,DSR,DS,FX,FXS,SR,DS,S,SR,DS,FX,S,SR,DS,FX,FXS";
$needed = "DSR,FXS";
if( strpos( $mainstring, $needed ) !== false ) {
echo "Found";
}else{
echo "Not match";
}
?>
One solution would be to explode those strings by comma and verify if the resulted arrays intersection count is the same as your search:
$mainstring ="S,SR,DSR,DS,FX,FXS,SR,DS,S,SR,DS,FX,S,SR,DS,FX,FXS";
$needed = "DSR,FXS";
$mainStringArr = explode(',', $mainstring);
$neededArr = explode(',', $needed);
if (count(array_unique(array_intersect($mainStringArr, $neededArr))) == count($neededArr)) {
echo 'found';
} else {
echo 'not found';
}
Explode the $needed string by command and traverse the array to and compare each value of array into $mainstring using strpos() function. If found then put that value into $arrResut with 'Found' or 'not found' value and finally print the $arrResult to view which value of $needed is found and which is not.
Also, we increment $cntNeeded variable if value if found. at the end of foreach loop compare value of $cntNeeded & $arrNeeded are same then all values are found into $mainstring else not.
$mainstring ="S,SR,DSR,DS,FX,FXS,SR,DS,S,SR,DS,FX,S,SR,DS,FX,FXS";
$needed = "DSR,FXS";
$arrNeeded = explode(",", $needed);
$arrResult = array();
$cntNeeded = 0;
foreach($arrNeeded as $index => $needed) {
if( strpos( $mainstring, $needed ) !== false ) {
$arrResult[$needed] = "Found";
$cntNeeded++;
}
else{
$arrResult[$needed] = "Not match";
}
}
print("<pre> :: arrResult ::");
print_r($arrResult);
print("</pre>");
if($cntNeeded == count($arrNeeded)) {
echo "Found";
}
else {
echo "Not match";
}
This attamept is to create a search condition where eventually the output will be processed to show results based on increasing order of matches
"PHP implode function in if and foreach condition [closed]" is a different question !
Why is the following code failing ?
$terms = array("HELLO", "HI", "HOWDY");
$row = array("HELLO", "HI", "Hey");
$chkcond = "in_array('".implode("',$"."row".")"." && in_array"."('",$terms)."',$"."row)";
echo "$chkcond<br/><br/>";
if ($chkcond) {
echo "All Found in Array !<br>";}else{echo "Not Found !<br/>";
}
The echo result is
in_array('HELLO',$row) && in_array('HI',$row) && in_array('HOWDY',$row)
And the if condition outputs "All Found in Array !"
When the if condition says that all three terms have to be in the row array to be "All Found in Array", then why is it returning True when "Howdy" doesn't exist in the row array ?
juste use array_diff http://php.net/array_diff
<?php
$terms=array("HELLO","HI","HOWDY");
$row=array("HELLO","HI","Hey");
$diff = array_diff($terms, $row);
if (0 === count($diff)){echo "All terms Found in Array row !<br>";}else{echo "Not all terms Found in Array row !<br>";}
$terms=array("HELLO","HI","HOWDY");
$row=array("HELLO","HI","Hey","HOWDY");
$diff = array_diff($terms, $row);
if (0 === count($diff)){echo "All terms Found in Array row !<br>";}else{echo "Not all terms Found in Array row !<br>";}
You could use array_diff
Something like:
<?php
$terms=array("HELLO","HI","HOWDY");
$row=array("HELLO","HI","Hey");
$chkcond=array_diff($terms, $row);
var_dump($chkcond);
if(empty($chkcond)) {
echo "All Found in Array !<br>";
} else {
echo "Not Found !<br>";
}
?>
Not sure what you are attempting but you can use array_diff.
$terms = array("HELLO","HI","HOWDY");
$row = array("HELLO","HI","Hey");
$differences = array_diff($terms, $row);
if ($differences) {
echo "All Found in array";
} else {
"Not Found !<br>";
}
https://secure.php.net/manual/en/function.array-diff.php
If you would like to stick with in_array you need to loop your terms:
<?php
$terms=array("HELLO","HI","HOWDY");
$row=array("HELLO","HI","Hey");
$chkcond = true;
foreach($row as $needle){
if(!in_array($needle, $terms)){
$chkcond = false;
break;
}
}
if($chkcond){
echo "All Found in Array !<br>";
} else {
echo "Not Found !<br>";
}
?>
Heck re-reading the manual http://php.net/manual/en/function.in-array.php you do not even need to loop:
<?php
$terms=array("HELLO","HI","HOWDY");
$row=array("HELLO","HI","Hey");
if(in_array($row, $terms)){
echo "All Found in Array !<br>";
} else {
echo "Not Found !<br>";
}
?>
I Have A json Array from hxxp://best1st.info/Moviedb/json.php?m=tt2015381&o=json
Here is sample of the array output :
. and so on
.
.
[STORYLINE] => On planet Earth in 1988, young Peter Quill ( ) sits in the waiting room of a hospital...
[ALSO_KNOWN_AS] => Array
(
[0] => Guardianes de la galaxia = Argentina
[1] => Qalaktikanin Mühafizeçileri = Azerbaijan
[2] => Пазителите на Галактиката = Bulgaria (Bulgarian title)
[3] => Guardiões da Galáxia = Brazil
)
[RELEASE_DATES] => Array
(
.
.
. and so on
I want to check if in "ALSO_KNOWN_AS" Element, have "Argentina" word , If "ALSO_KNOWN_AS" Element have the "Argentina" word, then display it (the value).
I have try to do it (search with google and here on stackoverflow), but seem my code dont work, Can some one here help me to fix it , here is my code
$url = 'http://best1st.info/Moviedb/json.php?m=tt2015381&o=json';
$newdata = json_decode(file_get_contents($url));
$alsoKnownAs = $newdata->ALSO_KNOWN_AS;
if (in_array('Argentina', $alsoKnownAs)) {
echo "Match found";
// echo the-array-value
}
else
{
echo "Match not found";
return false;
}
Thanks You
try this
$url = 'http://best1st.info/Moviedb/json.php?m=tt2015381&o=json';
$newdata = json_decode(file_get_contents($url));
$found = false;
foreach($newdata->ALSO_KNOWN_AS as $value)
{
if(strpos($value, "Argentina") !==false)
{
echo $value;
echo "<br/>";
$found = true;
}
}
if($found===false)
{
echo "Not found";
}
function searchArray($search, $array)
{
foreach($array as $key => $value)
{
if (stristr($value, $search))
{
return true;
}
}
return false;
}
Here in the above function first argument is the string you like to search and second argument is the array and it will iterate through entire array with the function stristr whose work is to return portion on found else false.
Note: if you need case sensitive search instead of stristr use strstr or strchr
if (searchArray('Argentina', $alsoKnownAs)) {
echo "Match found";
// echo the-array-value
}
else
{
echo "Match not found";
return false;
}
Your entries in the ALSO_KNOWN_AS array are not single words like "Argentina" but rather comparisons with country names and other text. This is why in_array() won't find it. in_array() needs precise matches. So you have to check all items.
I suggest you replace the if statement by a foreach loop:
...
...
$found = false;
foreach ($alsoKnownAs as $item) {
if (strpos($item, 'Argentina') !== false) {
$found = true;
break;
}
}
if (!$found) {
echo "Match not found";
return false;
}
Hope this helps :-)
No doubt it has been discussed few million times here and yes once again I am posting something related with it.
I have the following code.
$address = "#&#&#&";
$addr = explode("#&",$address);
it returns
Array
(
[0] =>
[1] =>
[2] =>
[3] =>
)
Now the values are empty.So I am doing a check.
If(!empty($addr))
{
echo 'If print then something wrong';
}
And it still prints that line.Not frustrated yet but will be soon
Run array_filter() to remove empty entries, then check if the array itself is empty()
if (empty(array_filter($addr)))
Note that array_filter() without a callback will remove any "falsey" entries, including 0
To check if the array contains no values you could loop the array and unset the no values and then check if the array is empty like the following.
$addresses = "#&#&#&";
$addresses = explode("#&", $addresses);
foreach ( $addresses as $key => $val ) {
if ( empty($val) ) {
unset($addresses[$key]);
}
}
if ( empty($addresses) ) {
echo 'there are no addresses';
}
else {
// do something
}
This is not an empty array, empty array has no elements, your have four elements, each being an empty string, this is why empty is returning false.
You have to iterate over elements and test whether they are all empty, like
$empty=True;
foreach ($addr as $el){
if ($el){
$empty=False;
break;
}
}
if (!$empty) echo "Has values"; else echo "Empty";
You are checking if the array contains elements in it. What you want to do is check if the elements in the array are empty strings.
foreach ($addr as $item) {
if (empty($item)) {
echo 'Empty string.';
} else {
echo 'Value is: '.item;
}
}
The array is not empty, it has 4 items. Here's a possible solution:
$hasValues = false;
foreach($addr as $v){
if($v){
$hasValues = true;
break;
}
}
if(!$hasValues){
echo 'no values';
}
i have this array:
include("config.php");
$start = "2014-06-20 08:00:00";
$data = mysql_query ("select * from evenement WHERE start = '$start'");
$zaznam = mysql_fetch_array ($data);
while($zaznam = mysql_fetch_array ($data))
{
$arr2[] = $zaznam["resourceId"]; //store query values in second array
}
If i echo $arr2 i get this:
Array ( [0] => STK1 )
now i make condition for array_search:
if (array_search('STK1', $arr2)) {
echo "Arr2 contains STK1 <br>";
}
else {
echo "Arr2 not contains STK1 <br>";
}
but i get this Arr2 not contains STK1
how it is possible? What im doing wrong?
That is totally correct behaviour for PHP.
The documention for the return value says:
Returns the key for needle if it is found in the array, FALSE otherwise.
In your case you are getting 0 which also evaluates to false in an if.
You have to check if the value is not false using the !== operator.
if (array_search('STK1', $arr2) !== false) {
echo "Arr2 contains STK1 <br>";
}
else {
echo "Arr2 not contains STK1 <br>";
}
From the array_search documentation:
This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
array_search is returning 0 because it found a match at index 0 of the array. This is evaluating to false.
Instead try:
if (array_search('STK1', $arr2) !== false) {
echo "Arr2 contains STK1 <br>";
}
else {
echo "Arr2 not contains STK1 <br>";
}