This question already has answers here:
How to use return inside a recursive function in PHP
(4 answers)
Closed 9 months ago.
Below recursive function i am trying to get array of codes. For example input 'bme4', output should be like [0]=>'bme'[1]=>'bm'[2]=>'b'. But the return value is null eventhough i can get the correct return value with var_dump().
function get_parent_cat_code($code, $category_codes) {
$parent_cat_code = substr($code, 0, -1);
if ($parent_cat_code != ''){
$category_codes[] = $parent_cat_code;
get_parent_cat_code($parent_cat_code, $category_codes);
} else {
var_dump($category_codes);
return $category_codes;
}
}
Solved!
function get_parent_cat_code($code,$category_codes){
$parent_cat_code=substr($code, 0, -1);
if($parent_cat_code!=''){
$category_codes[]=$parent_cat_code;
return get_parent_cat_code($parent_cat_code,$category_codes); //i used return for calling recursive function.
}else{
var_dump($category_codes);
return $category_codes;
}
}
Related
This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
How does 'function' and 'use' and 'array_filter' work in PHP?
(2 answers)
Closed 12 months ago.
I tried to use for-loop/foreach inside array_filter(). but the for loop/foreach doesn't work inside array_filter(). How can I use for-loop/foreach inside array_filter()?
$bookingData = array_values(array_filter($jsonBooking, function($bookingItem) {
foreach ($orderData as $orderItem) {
if ($bookingItem['order_id'] === $orderItem['id']) {
return $bookingItem;
}
}
}
Callback inside array_filter must returns bool (as condition to filter), be but you return an object.
This should work.
$bookingData = array_values(array_filter($jsonBooking, function($bookingItem) use ($orderData) {
foreach ($orderData as $orderItem) {
if ($bookingItem['order_id'] === $orderItem['id']) {
return true;
}
}
return false;
}
UPDATED: don't forget use statement
This question already has answers here:
How do I pass undefined vars to functions without E_NOTICE errors?
(4 answers)
Closed 3 years ago.
So i have a function that I am running all variables through so that it returns "" if the variable is not set. It looks like this:
function ck($str){
if(!isset($str) || empty($str)){
$str = "";
} else {
$str = $str;
}
return $str;
}
I have a call on my page:
echo ck($var);
No where on my page is $var defined. This should return a blank string and avoid throwing an error - right???
instead, i am still getting undefined variable error. Can some please explain to me why my function isn't working. Thanks.
$var is not defined. If you want the function to return an empty string you will need to set the parameter to a default value...
function ck($str = ''){
if(!isset($str) || empty($str)){
$str = "";
} else {
$str = $str;
}
return $str;
}
echo ck();
This question already has answers here:
Is there a better PHP way for getting default value by key from array (dictionary)?
(8 answers)
Closed 3 years ago.
I'm fetching information from a unofficial API. This API is very large and sometimes doesn't have all elements in it. I'm trying to display values from this API on my site without any errors.
What I've done is check the JSON values like so, to prevent errors:
echo (isset($json['item'])) ? $json['item'] : '';
Works, but it looks very unorganized. I've thought about creating a function to handle safe output, like so:
public function safeoutput($input, $fallback = '') {
if(isset($input)) {
return $input;
}
if(empty($input) || !isset($input)) {
return $fallback;
}
}
and then doing:
echo $engine->safeoutput($json['item'], 'Unavailable');
That unfortuanlly still outputs the Undefined variable error.
I was wondering if there's a better way to handle such information like I showed in the example.
Problem is that the key might not be set, so you would have to check it:
public function safeoutput($input, $key, $fallback = '') {
if(isset($input[$key])) {
return $input;
}
if(empty($input[$key]) || !isset($input[$key])) {
return $fallback;
}
}
Or you can have a shorter version:
public function safeoutput($input, $key, $fallback = '') {
if(array_key_exists($key, $input) && !empty($input[$key])){
return $input[$key];
}
return $fallback;
}
And call the method with the array and the key:
echo $engine->safeoutput($json, 'item', 'Unavailable');
This question already has answers here:
How to use return inside a recursive function in PHP
(4 answers)
Closed 9 months ago.
I am using a recursive function in codeigniter but is not working properly while returning the std class object but if i am using print_r() then print_r() is printing exactly what i want.
I am not getting any warning error message.
public function get_base_id($id){
$query = $this->db->query("SELECT * FROM `m_ecommerce_category` WHERE status ='0' and id = '$id'");
$data = $query->row();
if($data->parent != 0 ){
$this->get_base_id($data->parent);
}else{
//print_r($data);
return $data;
}
}
You should return your recursive call:
if($data->parent != 0 ){
return $this->get_base_id($data->parent);
}else{
//print_r($data);
return $data;
}
This question already has answers here:
Return always return false
(3 answers)
Closed 9 years ago.
I'm having some problems with a Return statement in PHP. The thing is that, no matter what happend inside my function I always get a false value out of the function. I really think that is because of the Return statement because I try to use it in other functions and I don't get a diferent value.
public function valid_token ()
{
if (!isset($_SESSION['token']) || $this->token != $_SESSION['token'])
{
$this->errors[] = "Formulario incorrecto";
}
return count($this->errors)? 0 : 1;
}
Out of this function I always get a false value (0). The same happends when i call:
public function valid_data ()
{
if (empty($this->sectorName) || empty($this->sectorInfo) || empty($this->sectorCat))
{
$this->errors [] = "Datos incorrectos";
}
return count($this->errors)? 0 : 1;
}
Of course, I call both functions when I have already sent the form and have set the token.
Because you are just counting not checking,Try this
return count($this->errors) > 0 ? 0 : 1;
More simply,
return !$this->errors;