I have a $_POST array and I would like to add a key at the very end.
So what I am doing is,
array_push($_POST['ques_15'] , '');
it works but I am getting a warning
Warning: array_push() expects parameter 1 to be array, null given
How can I remove this warning without turning off display errors.
I think you simply want to do:
$_POST['ques_15'] = '';
It will add at the end of $_POST array value '' with key ques_15
$_POST is an associative array.
Both array_push() and the directly providing key:value to your array will work.
Instead of array_push() , I would go like this :
<?php
$_POST['ques_15'] = '';
?>
It's simple as:
$_POST[] = 'value';
or
$_POST['yourkey'] = 'value'
it will be array_push($_POST , '');
as $_POST['key'] is not an array. $_POST is an array.
As #billyonecan mentioned in comment simply using $_POST['ques_15'] = '' can solve your problem ..:)
Related
I'm trying to work out how to get values from one of three arrays based on the array name.
$ABC001 = array('A'=>'10','B'=>'2','C'=>'1.0');
$ABC002 = array('A'=>'20','B'=>'4','C'=>'1.1');
$ABC003 = array('A'=>'30','B'=>'6','C'=>'1.2');
I have a variable passed to my script it will be contain something like ABC#001 or ABC#002
I'm removing the # so the var value now matches the array name/
$test = str_replace('#','',$var);
If I do var_dump ( $$test ) I get all the values from the correct array, but if I do echo $$test['A'] or echo $$test[0] I don't get the value from the first key in the correct array.
Can someone advise how to do this.
Thanks
try this ${$test} to get the values of the array
<?php
$ABC001 = array('A'=>'10','B'=>'2','C'=>'1.0');
$ABC002 = array('A'=>'20','B'=>'4','C'=>'1.1');
$ABC003 = array('A'=>'30','B'=>'6','C'=>'1.2');
$var = "ABC#002";
$test = str_replace('#','',$var);
var_dump(${$test}['A']);
In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$test['A'] then the parser needs to know if you meant to use $test['A'] as a variable, The syntax for resolving this ambiguity is: ${$test}['A'] . Please check the documention here PHP Variable Variable
I have a variable define
$commsIP = ['192.168.1.1'];
I am trying to add it to a url
$commsDisplay = file_get_contents("http://www.dangergaming.com/comms/$commsIP");
but I get the following error
Notice: Array to string conversion
but if I put the link like so
$commsDisplay = file_get_contents("http://www.dangergaming.com/comms/192.168.1.1");
It displays fine.
$commsDisplay = file_get_contents("http://www.dangergaming.com/comms/".$commsIP[0]);
or you could not declare it as array
$commsIP ='192.168.1.1';
You defined the variable as an Array, which is why it's saying it can't convert from an Array to a string. Placing []'s around the variable signifies that it's an array.
Just remove the []'s and it will work fine.
$comssIP = '192.168.1.1';
You put brackets around the IP address, when you do this it has the same functionality as an array.
If you change this:
$commsIP = ['192.168.1.1'];
To this:
$commsIP = '192.168.1.1';
It will work.
Alternativly you can also do this:
$commsDisplay = file_get_contents("http://www.dangergaming.com/comms/{$commsIP[0]}");
When you do that it will get the first result out of the $commsIP array.
I'm getting this error:
'( ! ) Warning: Cannot use a scalar value as an array in C:\xampp\htdocs\or2LAST\admin\adminData.php on line 5`
the code for this error (line 5):
$admin['push']=array(
1=>"yes",
2=>"no"
);
i tried to use "1" & "2" for the key but it didn't solve the problem.
where is the problem and why this error occurred?
The problem is with $admin. You need to declare it as an array before using it:
$admin = array();
Whatever it is now, it's not an array. Possibly on a previous line you're overwriting it with some new (scalar) value instead of appending to it.
You need to declace $admin to an array first
like
$admin=array();
then try
$admin['push']=array(
1=>"yes",
2=>"no"
);
Your problem is probably with variable $admin, following should work just fine (assuming $admin is not previously declared).
$admin = array(); //<--!!
$admin['push']=array(
1=>"yes",
2=>"no"
);
Imagine this code:
$array1 = "20";
$array2 = "40";
$array3 = "";
$arraydate = array($array1,$array2,$array3); //In this case would be array("20","40","0")
So what I want is that when there is a variable that is null, 0 or empty, then do not make part of the array. The solution to this is to pass from:
array("20","40","0")
to:
array("20","40")
Is there anyway to do this? Sorry for my bad english. Thank you :D.
Use $arraydate = array_filter($arraydate);
According to the manual, if no callback is given, it will remove all items that equal to false.
Have you actually looked in the PHP Manual? They provide a one line solution with array_filter...
$newAray = array_filter($arraydate);
Wrong Imagination this would never result to array("20","40","0"), it would be instead
array ( "20", "40", "")
And even of you are getting that then use array_filter function to filter value
after submited form i want to check array if array is empty alert error for user. but i get error when submited form:
PHP
$errors = array_filter($_POST['session']);
if (!empty($errors)) {
foreach ($_POST['session'] as $value) {
$session.=$value.',';
}
$session=substr($session, 0 , -1);
}
Warning: array_filter() expects parameter 1 to be array, null given in C:\inetpub\wwwroot\manage\test_bank\index.php on line 729
You need to check wheather it is an array or not, before doing any array operation.
if(is_array($_POST['session'])){
$errors = array_filter($_POST['session']);
}
The warning occurs because array_filters() requires an array to be passed to it. Before passing $_POST['session'] to this function, very if that it is an array:
if(is_array($_POST['session'])) {
$errors = array_filter($_POST['session']);
// continue on
}
use is_arrayfor checking weather it is array or not.
echo is_array($_POST['session']);
This is because $_POST is not an array I guess you are looking for this :
$errors = array_filter($_POST);
The following would most simply check for empty errors or not
!empty($_POST['session'])
Would work provided you are not stuffing empty entries in the $_POST['session'] in no error cases. Why do you need the array_filter?
$_POST is an array, but here $_POST['session'] is not.
You can smply try this:
if(isset($_POST['session']))
{
//do your stuff
}
Change it to array_filter($_POST) because $_POST is an assoc array, or check if $_POST['session'] is an array using this line is_array($_POST['session']) before the array_filter().
You should check first if the variable you are working with is an array before using array functions.