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.
Related
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?
I get this error on PHP7 every about 100 requests for some odd reason and I cannot get rid of it until I restart the fpm demon, however, the real problem is I cannot explain what the error is to start diagnosing this.
I took a look at the documentation http://php.net/manual/en/function.json-last-error.php which was not very useful and there does not seem to be any real links hanging about.
I know this error is not actually related to recursion depth (JSON_ERROR_DEPTH) so what does this error actually mean?
This is the var_dump() of the array that is failing:
array (
'ns' => 'user',
'where' => '{"_id":"MongoDB\\\\BSON\\\\ObjectID(5505a4f647ac1824618b4567)","status":10}',
'projection' =>
array (
),
'sort' =>
array (
),
'limit' => NULL,
'skip' => NULL,
)
JSON_ERROR_RECURSION indicates that the data passed to json_encode() contains one or more recursive references.
$data = array();
$data['foo'] = &$data; // <-- recursive reference here
var_dump(json_encode($data)); // bool(false)
var_dump(json_last_error_msg()); // string(18) "Recursion detected"
var_dump(json_last_error() === JSON_ERROR_RECURSION); // bool(true)
Code like this will work though:
$data['foo'] = 'hello';
$data['bar'] = &$data['foo'];
But not like this (another recursive reference):
$data['foo'] = [1, 2, 3];
$data['foo'][] = &$data['foo'];
Recursive reference means that the reference points to a variable that in turn contains the same reference again.
Most likely need to update mongodb driver (or maybe another extension). Extension with bug may write to shared memory (immutable arrays) - it is not allowed.
It is can be detected with opcache.protect_memory (http://php.net/manual/en/opcache.configuration.php#ini.opcache.protect-memory)
http://news.php.net/php.internals/98210
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
I have an array of arrays, each array containing details of a scan by a medical device. I'm getting this data from text logs that are dumped nightly. The format of which is this:
$this->scans = array(
array(
'patientid' => (int),
'patientname' => 'John Skeet',
'reviewed' => 0 or 1
//plus more irrelevant
),
array(
//same as above
), //etc
)
The important array key here is reviewed, as each scan may be reviewed if it is of high enough quality. However, the text logs dump out EVERY scan that is acquired, then goes back through and re-lists the ones that are reviewed.
Now in order to prevent duplicates , I figured I could just use an array_filter to filter out scans that have been both acquired and reviewed (keeping the reviewed version). However, the filter function is filtering out the entire array (except in some rare cases). If someone could take a look and let me know why they think it's happening that would be much appreciated.
$this->scans = array_filter($this->scans, array($this, "scan_cleanup"));
.
private function scan_cleanup($scan) {
//only if the scan was not reviewed
if ($scan['reviewed'] == 0) {
//change reviewed status to see if there is a duplicate
$scan['reviewed'] == 1;
//return false to remove this copy (and keep reviewed)
if (in_array($scan, $this->scans)) {
return false;
}
}
return true;
}
$scan['reviewed'] == 1;
vs
$scan['reviewed'] = 1;
One is a conditional, that does nothing in this context, the other is not there.
You are also not running the return false very often. I'd change the logic a little to make it a little clearer, and simpler by a little refactoring (pulling out a condition-check).
if ($scan['reviewed'] and hasDupe($scan)) {
return false; // filter out
}
return true; // it is passed back, and is output
hasDupe() does the best checks you know for a duplicate record and returns true/false.
Simple case of "==" vs. "=" as far as I can see.
$scan['reviewed'] = 1;
That oughta do the trick. Sometimes the simplest problems are the hardest to spot ;-)