PHP doesnt read sha1-hash (string) from array - erratic behaviour - php

I am trying to access certain subarrays of an array depending on a sha1 code saved in the subarray.
I have the following code working for me:
//I take a sha1 string of 40chars from GET
if(isset($_REQUEST['sha1'])){
//then I iterate through the array and check if the subarrays sha1 matches
for($i = 0; $i < count($scomplaints); $i++) {
if($scomplaints[$i]['sha1'] == $_REQUEST['sha1']){
//once a match is found I delete the match...
unset($scomplaints[$i]);
//... reserialize the array and update it in the database
$new_c = serialize($scomplaints);
// ( add new array to database )
break;
}
}
}
Pretty straight forward. Only problem is: In some random cases the loop doesnt read the sha1 value of the subarray. Instead it reads an empty string from the array.
But when i var_dump the array the sha1 code is clearly saved in the array and also identical to the one I get from GET (I did a by hand comparison).
I can't figure out what the problem is, maybe some encoding error? Maybe one of you guys can help me with this?
Thanks for all advice.

I got it solved. The issue was the array not being reindexed by unset($array). Once I reindex it with array_values($array) it works again.

Related

Length of array saved in entry 0?

i read information out of a MSSQL database and save this into arrays with a length of 2000.
When i echo the entries i get the length of these arrays in the first entry (e.g. amount[0] = 2000).
Does php do this by default or is there no default value?
(The cells in my database which go into these arrays are empty in the first entry)
Thank You
If i understand your question very well. PHP uses a function called count() to determine the length of an array ,you can do somethiing like this,
$x = [12,11,334,56,788,999,45,56,23];
$y = count($x);
then change the value of the first array element like this,
$x[0] = $y;
So whenever you call $x[0] you will get the length of the array.

Get a single value from a nested associative array

For a setup script I write in PHP (for CLI, not web) I use a settings file as well as other other "content" files.
The settings file is built up like a regular ini-file
[header.subheader.moreheaders...]
keyA = value
keyB = value1|value2|...
"header.subheader.moreheaders..." and "keyX" will form a nested associative array with "value" as a string or "value1|value2|..." as a simple array (0-...).
Thanks to this accepted answer, I got so far that I can split the headers into a recursive array recursively. So far, so good.
However, as the content files shall contain references to these variables, I would like to be able to read out single values from that multi-dimensional array with string placeholders like $#R[header.subheader.moreheaders.key] or $#R[header.subheader.moreheaders.key.0] depending on them being a string or an array.
In the script, $#R[header.subheader.moreheaders.key.0] should convert into $SettingsVar[header][subheader][moreheaders][key][0] to return the appropriate value.
Neither the script nor the content files will know what is inside the settings file. The script just knows the general structure and placeholder $#R[...].
This answer appears to know what value will be in order to search for it.
Since I do not fully understand this answer, I am not sure if that would be the right way.
Is there a similar easy way to get the reverse from building that array?
After some contemplation, I found a decent enough solution, which works for me (and hopefully others):
function GetNestedValue($aNestedKeys, $aNestedArray)
{
$vValue = $aNestedArray;
for($i = 0; $i < count($aNestedKeys); $i++)
{
if(array_key_exists($aNestedKeys[$i], $vValue))
{
$vValue = $vValue[$aNestedKeys[$i]];
}
else
{
$vValue = null;
break;
}
}
return $vValue;
}
Depending on what $aNestedKeys contain, it will either return a sub-array from $aNestedArray, a single value from it or null if any of the specified keys were not found.

Check entire array for single if operation

I have this code to check some input data to be sure it fits a proper format. However input_28 is a list field that is sent as an array. Is there a shortcut to check each item in the array for the filter instead of looping through each item?
I dont care what it wrong or which line does not match. I just need to know if any line does not match the filter so I can return the form with an error.
if($_POST['input_28'] != filter_var($_POST['input_28'], FILTER_VALIDATE_REGEXP,array("options"=>array("regexp"=> "/(\d\d\d\d-\d\d\d\d)|()/" )))){
If I understand correctly, you are looking for array_search(), you can use it like this
if(array_search($value, $array)){
// Do stuff
}
Note that array_search() only works if the array is NOT multi-dimensional, you can take a look on the documentation for more references and examples
Some searching on functions that #Alfonso mentioned in his answer led me to another function: preg_grep That led to this little function which worked great.
$input_28=preg_grep("/^(\d\d\d\d-\d\d\d\d)$/", $_POST['input_28'], PREG_GREP_INVERT);
if(count($input_28) > 0){
#Do Something
}
The PREG_GREP_INVERT makes it return an array of entries that do NOT match the filter. Then we count the array, if any did not match the array will have an entry. Thus greater than 0

PHP - Not getting the element I expect from an array

I am paging through the elements of an array.
I get the total number of elements in the array with:
$total = count($myarray);
My paging function loads the current element on the page and provides "Previous" and "Next" links that have urls like:
http://myapp.com?page=34
If you click the link I grab that and load it onto the page by getting (I sanitize the $_GET, this is just for example):
$element = $myarray[$_GET['page']];
This should grab the element of the array with a key == $_GET['page'] and it does. However, the problem is that my total count of elements doesn't match some keys because while there are 100 elements in the array, certain numbers are missing so the 100th item actually has a key of 102.
How should I be doing this? Do I need to rewrite the keys to match the available number of elements? Some other method? Thanks for your input.
If you have gaps in the indices, you should reindex the array. You can do that before you generate the links, or probably easier on the receiving page:
$myarray = array_values($myarray);
$element = $myarray[$_GET['page']];
This would give you the 100th element, even if it previously had the key 102. (You could use a temporary array of course, if you need to retain the original indexing.)
you can use
$array = array_values($array);
How should I be doing this? Do I need
to rewrite the keys to match the
available number of elements? Some
other method?
No you don't need to worry about them not matching. Php arrays are associative containers, like dictionaries in other languages. If you define something at 98 and 100, 99 isn't sitting there in memory, the data structure behind the associative container only stores whats there. You're not wasting space by not "filling it up" up to count.
So the practice you describe is fine. If there is no page "99" nothing need show up in your array. It may be nice, however, to see that your array doesn't have anything for the 'page' parameter and display an error message.
But then why, when I access $total =
count($myarray); $myarray[$total]
where $total = 100 I do not get the
last element? I can put page=101 and
get one more record. I should not be
able to do this
Because count is counting how many things are in the array. If you have an array with only the even elements filled in, ie:
$myArray[0] = "This";
$myArray[2] = "is";
$myArray[4] = "even";
Here count($myArray) is 3. There's nothing in [1] or [3]. Maybe this is easier to see when you take numbers out of the equation. Arrays can have string indexes
$myArray = array();
$myArray["Hello"] = "A Bunch of";
$myArray["World"] = "words";
Here count($myArray) is 2.
In the first case, it wouldn't make sense to access $myArray[3] because nothing is there. Clearly in the second example, there's nothing at 2.

How do I print array values in a range when values are supplied?

My php reads in xml and I want to ouput the values within a given range. I have no way of knowing the size of the array or the range. However, I do know where to start; I have a $key that holds my current location. I also know where to stop; I have the word "ENDEVENTS" between each set. I want to get the values from my current position ($key) to my end position ("ENDEVENTS").
for example i may have an array set like this:
Array(
[0]=1
[1]=apple
[2]=straw
[3]=bike
[4]=ENDEVENTS
[5]=15
[6]=hair
[7]=shirt
[8]=nose
[9]=kiwi
[10]=ENDEVENTS
)
My code knows when I'm on 15 (in this example $key=5). I want to print 6 through 9. I tried using foreach but it's thats causing issues with what I'm trying to produce.
Any ideas?
Not so sure if i understood all ok but, ill give a try :-D
while(array[$key]!="ENDEVENTS"){
echo array[$key];
$key++;
}
Not entirely sure if I understand your question, but maybe this is helpful:
$stuff_to_print = array_slice($my_array,$key,array_search('ENDEVENTS',$my_array));
This should return an array with key values from the $key to the next 'ENDEVENTS' value

Categories