implode() :invalid argument passed - php

function submit_data()
{
$st_value='';
$ft_value='';
$mt_value='';
$otr_value='';
$st_details= $this->input->post('check_list');
$ft_details= $this->input->post('ft_check_list');
$mt_details= $this->input->post('mt_check_list');
$otr_details= $this->input->post('otr_check_list');
//print_r($st_details);
$st_value=implode(",",$st_details);
$ft_value=implode(",",$ft_details);
$mt_value=implode(",",$mt_details);
$otr_value=implode(",",$otr_details);
$index= $this->register->insert_details($st_value,$ft_value,$mt_value,$otr_value);
//$this->register->update_details($st_value,$ft_value,$mt_value,$otr_value);
$this->session->set_flashdata('success_message',$success_message);
redirect(base_url().'new_register/index/'.$index);
}
Here is my controller function and i am getting an error message of implode(): invalid argument passed while submiting,How can i intercept the error.

implode() method accept second parameter as array. I think you are providing it a string.
Check by var_dump($st_details);

As others said, implode() uses second parameter as array. You can test your varriable to see if it's an array using is_array() or using var_dump() to see its details.
implode()
is_array()
var_dump()

Related

php function with variable and array

I am trying to create a PHP function with 2 parameters. One parameter is a variable and the second is an array like this
<?php
function Myfunction($variable, $array=array()){
foreach($array as $item){
echo $variable;
echo $item;
}
}
?>
I want a call like this:
<?php
Myfunction(blue, 1,3,6,10,5);
?>
"blue" is the variable
"numbers" insert in an array.
I tried something but it does not work.
Who can help me with this?
Well, there are two possibilities:
You can wrap your values in an array (ie: []), which I believe is what you intended:
Myfunction(blue, [1,3,6,10,5]);
Or you could take advantage of PHP's variable argument list and have your function parameters listed like so:
Myfunction($variable, ...$array);
Note the ... before $array, this signifies that this parameter will accept a variable number of arguments. Keep in mind that parameter using ... must be the last parameter in your argument list.
With this, you may call your function like so:
Myfunction(blue, 1,3,6,10,5);
Hope this helps,

error on form submit if no second parameter

When i submit my form and if the second parameter is empty.
It throws error.
Message: in_array() expects parameter 2 to be array, null given
Filename: user/Users_groups.php Line Number: 250
I know there is no second parameter. $this->session->userdata('modify')
if (!in_array('user/users_groups', $this->session->userdata('modify'))) {
$this->error['warning'] = 'You do not have permission to modify';
}
Is there away to make it stop throwing that error when user or I try to submit form if no second parameter $this->session->userdata('modify')
But still have that code.
Try to check first if parameters are valid:
<?php
if(is_array($this->session->userdata('modify')) && !empty($this->session->userdata('modify')))
{
if (!in_array('user/users_groups', $this->session->userdata('modify')))
{
$this->error['warning'] = 'You do not have permission to modify';
}
}else{/* handle errors */}
?>
Replace the null value with an empty array before the if statement:
Could use a ternary operator like this:
$array = (is_array($this->session->userdata('modify')))
? $this->session->userdata('modify')
: Array()
It will use the 'modify' array if it's an array, else will pass an empty array
I would also check array_search() function because it can be compared with NULL wich is returned if irregular parameters set.
5.3.0 As with all internal PHP functions as of 5.3.0, array_search() returns NULL if invalid parameters are passed to it.

why pass-by-reference doesn't change the value of a variable?

$string='string';
function change(&$str){
$str='str';
}
call_user_func('change',$string);
echo $string;
output is 'string'. but $str refer to the $string , so change in $str should also change $string? why the value is still same?
It's not possible with call_user_func():
From the PHP documentation:
Note that the parameters for call_user_func() are not passed by reference.
That said you still can use call_user_func_array(). Using the reference becomes now possible. Here's the code you want:
function change(&$str)
{
$str='str';
}
$string='string';
$parameters = array(&$string);
call_user_func_array('change',$parameters);
echo $string;
However this solution is now Deprecated.
You still can get rid of call_user_func(), and simply do:
function change(&$str)
{
$str='str';
}
$string='string';
change($string);
echo $string;
http://php.net/manual/en/function.call-user-func.php
It clearly mention that the parameters for call_user_func() are not passed by reference.
Here's your error message:
<br />
<b>Warning</b>: Parameter 1 to change() expected to be a reference, value given on line <b>5</b>
because call_user_func does not pass parameters by reference.
Try run it like this:
$string='string';
function change(&$str){
$str='str';
}
change($string);
echo $string;
call_user_func doesn't pass by reference. From the PHP docs:
Note that the parameters for call_user_func() are not passed by reference.
(change($string), on the other hand, gives "str" as you would expect).
Further reading: Why does PHP's call_user_func() function not support passing by reference?

Use vsprintf with non array as arguments

echo vsprintf('%s', 'word');
According to manual, second parameter for vsprintf() function must be array.
But this works, its normal? this may cause some error sometime?
The $args argument is automatically cast to an array:
$args = (array)$args; // = array('word');

php check empty array

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.

Categories