if (in_array($form['#submit'], 'search_box_form_submit')) {
$key = array_search('search_box_form_submit', $form['#submit']);
unset($form['#submit'][$key]);
}
array_unshift($form['#submit'], 'mymodule_search_box_submit');
What does the code do? I don't follow it well; I expect someone can explain it to me, line by line.
If the submitted form contains a variable named "search_box_form_submit", delete it, and then add a new variable called "mymodule_search_box_submit".
Perhaps somebody wanted to override the drupal search function and didn't want the default processor to fire at all. Thanks kiamlaluno in the comments.
if (in_array($form['#submit'], 'search_box_form_submit')) {
If the value 'search_box_form_submit' is present in the array $form['#submit']
$key = array_search('search_box_form_submit', $form['#submit']);
Then set the variable $key to the array key for the value 'search_box_form_submit' in the array $form['#submit']
unset($form['#submit'][$key]);
Then unset (delete) that array element
array_unshift($form['#submit'], 'mymodule_search_box_submit');
Put a new element at the beginning of the array $form['#submit'] with the value 'mymodule_search_box_submit'
is the text "search_box_form_submit" in the array $form['#submit']
if so find the key for search_box_form_submit
then remove from array
put the value mymodule_search_box_submit in the front of the array $form['#submit']
i recommend reading the manual page for the functions used.
Related
I have an array. Say example
array('x'=>1);
this is having only one element. This array can change. Key change but only one element will be there. e.g. next time the array can be like --
array('y'=>1);
Now the problem is - I don't know the key name, I need the key name and the value as well.
But as it is having only one element I don't want to run a foreach.
Is it possible ?
For that you can use key function
Try this here snippet here
$array=array('x'=>1);
echo key($array);//x
You should try with: array_search()
Hope this will help you.
$key = array_keys($array)[0];
$value = $array[$key];
As the question says i need to validate a multidimensional array, just so you know this is my first time actually using arrays so it might be a pretty bad script but it works and that's all I'm after at the moment. Okay so it's working I have two sessions displaying in this array, when i remove one of the sessions I get this error
"Notice: Undefined index: pop in C:\inetpub\wwwroot\dropdown\test.php on line 30"
I think i know how to fix it but I don't actually know how to implement it. this is me talking through what im after
$myarray (
IF isset session cityname
add the value to my array
ELSE
add a blank value in its place (or just remove it from the array altogether)
IF isset session pop
add the value to my array
ELSE
add a blank value in its place (or just remove it from the array altogether)
echo myarray
please note cityname is mandatory whereas pop is not
That's essentially what I'm trying to achieve but i haven't the slightest how to actually go about doing it, here is my current code
if(isset($_SESSION['cityname'])){
$myarray = array(array($_SESSION['cityname']),
array($_SESSION['pop'])
);
foreach($myarray as $key=>$value){
echo $myarray[$key][0];
}
Any help MUCH appreciated I've lost to much hair to this problem the last couple of weeks!
That notice is telling you that you're using $_SESSION['pop'] that has never been set.
In fact in your code you just checked for $_SESSION['cityname'] but then you add $_SESSION['pop'] to your array.
EDIT
If you want $_SESSION['pop'] to be optional and you want to get rid of that notice, just check if $_SESSION['pop'] is set or not:
if(isset($_SESSION['cityname'])){
$myarray = array(array($_SESSION['cityname']));
if(isset($_SESSION['pop'])) { $myarray[] = array($_SESSION['pop']); }
foreach($myarray as $key=>$value){
echo $myarray[$key][0];
}
How to get next array element from session array on next button click?
I tried next($_SESSION['qid']) it didn't work
if((int)$_SESSION['qn']<=20) {
$_SESSION['qn']=$_SESSION['qn']+1;
$_SESSION['qid']++;
}
I also tried
$_SESSION['qid']=next($_SESSION['qid']);
But this neither worked. Can someone help me?
_SESSION array is an associative array. You can't access it by numeric index, but you must specify the index name (e.g. in your code, $_SESSION['qid']). Anyway, you can still use next() function, passing the array $_SESSION (see here: http://php.net/manual/en/function.next.php). The correct way to use it is:
$element = next($_SESSION)
you'd likely want to put this code in a cycle.
Additionally, your code:
$_SESSION['qn']=$_SESSION['qn']+1;
means: assign to $_SESSION['qn'] the value of $_SESSION['qn'] plus 1, which is not what you want.
In case you'd want the next element in a NON associative array, you should use:
$arr = $arr[$i+1]
where $i is a integer value.
Update: regarding your comment, why don't you save a regular array (non associative) inside $_SESSION['questions']? This way you'd have the questions accessible this way:
$_SESSION['questions'][0], $_SESSION['questions'][1]...
Now you can use it within a cycle, or whatever you want. E.g.:
echo $_SESSION['questions'][$current_question_id+1];
where $current_question_id would be the current question index, which will be updated (+1) every clic on the next button
I've created an array from a PHP session variable, and now I'm trying to use ajax (within jQuery) to remove an element from the array.
I've got the following code so far:
$val = $_SESSION['enquiry-basket'];
$array = explode($val);
foreach ($enquiries as $a => $q) {
if ($q == $_POST['product_id']) {
unset($array[$a]);
}
}
The only problem is, it doesn't remove the item.
Can anyone explain why, and tell me how to fix it?
Edit
Sorry guys. The reason I mentioned jQuery is because I use a jQuery ajax call to process the PHP I displayed above.
The ajax query runs fine because it processes some javascript goodies (remove's a div from the HTML) once the ajax returns a success.I've added the delimiter (can't believe I missed it) but the element doesn't get removed from the array still.
I've never been good at multi-dimensional arrays, so here's the array printed:
Array ( [0] => 6 [1] => 8 )
It looks right to me, but I'm an amateur in arrays. (6 and 8 are of course my strings I inserted)
explode is missing the first argument:
explode(',', $val);
You are removing item from $array, not from $_SESSION['enquiry-basket'].
The explode function should have two parameters. But you given only the name of the array.
explode(separator,string,limit);
If I understand correctly what you are trying to do, the problem is that JQuery runs client side, which means that your PHP arrays on the server side disappear between each request from Ajax. The only array that remains is $_SESSION.
If you want to use AJAX, you need to remove from $_SESSION directly. Anything else is just useless because the arrays and variables "disappear" between each call.
Mostly an issue with the explode function, the second parameter is missing:
Change from:
$array = explode($val);
To:
$array = explode('~',$val); // ~ is a delimiter
When I send over post data I do a print_r($_POST); and I get something like this...
Array ( [gp1] => 9 )
Is there a way to get the "gp1", the name sent over as a value? I tried doing.
echo key($_POST["gp1"]);
But no luck there, I figured it would echo gp1. Is there a way to do this?
you need
print_r(array_keys($_POST));
check this for more details http://php.net/manual/en/function.array-keys.php
You could use foreach to see each key-value pair, or use array_keys to get a list of all keys.
foreach ($_POST as $key => $value) {
// Do whatever
}
Well, if you can write $_POST["gp1"] you already have the key anyway ;)
key() works differently, it takes an array as argument:
The key() function simply returns the key of the array element that's currently being pointed to by the internal pointer. It does not move the pointer in any way. If the internal pointer points beyond the end of the elements list or the array is empty, key() returns NULL.
So if you have not done anything with the array (no traversing), key($_POST) would give you the key of the first element of the array.
Maybe you want a foreach loop?
foreach($_POST as $key => $value) {
}
There are other methods to retrieve keys as to well. It depends on what you want to do.