I am trying to find the occurrence of a substring in an array.
Here is the string:
$string = "π₯π₯π₯π₯π₯Watch the game at #TheGoalTX on #GoaltimeSaturdays!"
Here is the array:
$venues = array (
[ "name" => "McDonalds", "slogan" => "Im Lovin It" ],
[ "name" => "The Goal", "slogan" => "GoaltimeSaturday" ],
[ "name" => "McDonalds", "slogan" => "McRibs are back" ],
);
Im running an array search in which I want to run a script if either search condition is true:
foreach ($venues as $venue) {
if ((stristr($string,$venue['name'])) OR (stristr($string,$venue['slogan'])) !== false) {
include "do.stuff.php";
}
}
The two array rows with McDonalds obviously fails. No problem.
But for some reason, the search is failing for the "slogan" element "GoaltimeSaturday" and Im not quite sure why. It should meet the "if" criteria but "do.stuff.php" is never reached.
A little help here / any ideas? Are the ""π₯π₯π₯π₯π₯" emotes or the "#" or "#" characters in the string the issue perhaps? Im at a loss as to why Im not getting a true/positive search result in the foreach loop...
I appreciate the assistance on this...
If include "do.stuff.php" never reached, but you await for reaching it, it means your condition is incorrect.
I think the problem is extra braces, try this:
if ((stristr($string,$venue['name']) OR stristr($string,$venue['slogan'])) !== false)
Also i don't know about difference between OR logic operators 'OR' or '||'.
UPD
May you show do.stuff.php file content?
Is there are any errors or notices in logs or console output?
Related
Lets assume, the return value of an search-fuction is something like this
// If only one record is found
$value = [
'records' => [
'record' => ['some', 'Important', 'Information']
]
]
// If multiple records are found
$value = [
'records' => [
'record' => [
0 => ['some', 'important', 'information'],
1 => ['some', 'information', 'I dont care']
]
]
]
what woul'd be the best way to get the important information (in case of multiple records, it is always the first one)?
Should I check something like
if (array_values($value['record']['records'])[0] == 0){//do something};
But I guess, there is a way more elegant solution.
Edit:
And btw, this is not realy a duplicate of the refered question which only covers the multiple records.
If you want the first element of an array, you should use reset. This function sets the pointer to the first element and returns it.
$firstValue = reset($value['record']['records']);
Edit.. after reading your question again, it seems, you dont want the first element.
You rather want this
if (isset($value['record']['records'][0]) && is_array($value['record']['records'][0])) {
// multiple return values
} else {
// single return value
}
Doing this is kind of error proun and i wouldn't suggest that one function returns different kinds of array structures.
check like this..
if(is_array($value['records']['record'][0])) {
// multiple records
} else {
// single record
}
i am using the "medoo"-php/mysql-class (http://www.medoo.in) for my latest project. I like the quiet easy way to work with my SQL-stuff.
But i wondered if it is possible to change the search term according to an option from an input-select-form. Lets say we got search-option "user by name", we would go with:
$data = $database->select("account", [
"user_name",
"user_id",
"phone",
"email",
"age"], [
"user_name" => $_POST['user_name']]);
Fine so far. Now we get the search option "user by ID". Every thing else stays just the same. So i need only the
["user_name" => $_POST['user_name']]);
to be like
["user_id" => $_POST['user_id']]);
Is there a way to change that without writing the whole statement again?
It is just a example. Moreover for my project i will need to change other options of the query like update to insert oder different join-options. But for the moment i am fine with an answer to that.
Anyway, what do you think about the Medoo-Class. Got any cool alternative Class-solutions for me? Basically i know how to work with SQL querys but getting stuff to the array at the end is always driving me crazy. So i would love to get stuff faster and easier with a class.
Thx a lot for your help!
Best, Lox
You can just change the $where array before running the query like that:
if ($type == 'id')
{
$where = ["user_id" => $_POST['user_id']];
}
if ($type == 'user_name')
{
$where = ["user_name" => $_POST['user_name']];
}
$data = $database->select("account",
[
"user_name",
"user_id",
"phone",
"email",
"age"
],
$where
);
I been trying to make a PHP path finder from examples in other languages and tweaked it for my game's need but its not returning any values.
I should note - I get no errors in my error log file either so I'm guessing its a logic mistake?
Hope some one can spot it cos i cannot see the mistake =/
Code:
http://www.paste.to/NDI3MTE1
Just took a really short look, but:
function findpath($sx, $sy, $sdata) {
if( in_array($sx, $sdata) && in_array($sy, $sdata($sx) ) ){
...
}
}
$sdata = array(
"13" => ...,
"14" => ...,
"15" => ...
);
$astar = findpath(13,15,$sdata);
in_array checks the array values, not the keys... as such, your whole function doesn't even do anything. Use array_key_exists for this.
In the building process of an array I'm trying to call a function, checkIfNodeExists(). PHP executes this function and gives me the expected result but he still gives me a "Notice", and I don't want any kind of errors in my code.
function checkIfNodeExists($input) {
if (isset($input)) {
return (string) $input;
} else {
return 'null';
}
}
$array['sections'][] = array (
'ident' => $section->attributes()->ident,
'title' => $section->attributes()->title,
'objective' => checkIfNodeExists($section->objectives->material->mattext)
);
Notice: Trying to get property of non-object in /var/www/OLAT_Connection/QTI-XML-Parser.php on line xx
Now, if I check if "objective" exists OUTSIDE the array, PHP doesn't give me a notice. But this results in more lines of code because I have to work with an extra variabele and more IF-structures and so on...
Is there any other possibility without adding too much lines of extra code?
The problem is that when you call checkIfNodeExists you send it a value, and by sending it the value you also execute the value. So isset() will work on the result of the expression $section->objectives->material->mattext and not the expression itself.
This would work:
$array['sections'][] = array (
'ident' => $section->attributes()->ident,
'title' => $section->attributes()->title,
'objective' => isset($section->objectives->material->mattext) ? (string)$section->objectives->material->mattext : 'null'
);
It seems that $section->objectives->material->mattext is not properly defined. I would start looking there to see if you have errors in the initialisation of the object.
It would be better if you posted more code up so we can see what exactly is going on in this script.
The solution may require more code (albeit unlikely in this case), this is by no means a bad thing. Less code is not necessarily better or more efficient! Obviously it goes without saying that fewer lines of code will (most of the time) execute faster, but this does not make it more secure or efficient
UPDATE
You may be able to simply do this instead of calling another function:
'objective' => isset($section->objectives->material->mattext) ? (string)$section->objectives->material->mattext : null
I have not tested this and cannot remember whether you can place conditional statements inline, so cannot be sure whether it will work but if it does then this will be more efficient, and it is less code!
if you add "#" when you call the function the error aren't show
function checkIfNodeExists($input) {
if (isset($input)) {
return (string) $input;
} else {
return 'null';
}
}
$array['sections'][] = array (
'ident' => $section->attributes()->ident,
'title' => $section->attributes()->title,
'objective' => #checkIfNodeExists($section->objectives->material->mattext)
);
Define $section->objectives->material->mattext
I have the below array..
<?php $arrLayout = array(
"section1" => array(
"wComingEpisodes" => array(
"title" => "Coming Episodes",
"display" => ""
)
));
?>
Then I want to check if wComingEpisodes is in the array so..
<?php if (in_array( "wComingEpisodes" , $arrLayout )) {
echo "CHECKED";}
?>
However its returning nothing even though it is in the array. Do I need to do something different because of the multiple arrays or where is my mistake?
in_array tests for values, not keys. It also does not test nested values.
$arrLayout = array(
"section1" => array(
"wComingEpisodes" => array(
"title" => "Coming Episodes",
"display" => ""
)
));
echo array_key_exists(
"wComingEpisodes",
// this is the array you're actually looking for
$arrLayout['section1'] )?'exists':'does not exist';
you're getting the correct result according to the documentation.
if you want to search an multidimensional array recursive, take a look at the user contributed notes, where you can find examples like this, that show how to do a recursive search (which seems to be what you're looking for).
EDIT:
i just noticed you're trying to find out if a specific key exists: in this case, you'll have to use array_key_exists (like in_array, this also doesn't do a recursive search, so you'll have to do something like this).
in array searches array values.
the string, "wComingEpisodes", is the key of the value
you may want to try using array_key_exists()
In_array isn't recursive by nature so it will only compare the value you give it with the first level of array items of the array you give it.
Thankfully you're not the first with the problem so heres a function that should help you out. Taken from php.net in_array documentation
function in_arrayr($needle, $haystack) {
foreach ($haystack as $v) {
if ($needle == $v) return true;
elseif (is_array($v)) return in_arrayr($needle, $v);
}
return false;
}
http://www.php.net/manual/en/function.in-array.php#60696