i have got the following recursive function:
private function myRecursiveFunction()
{
$results = [];
//stuff related to $results
...
if(!$done){
.....
$this->myRecursiveFunction();
}
return $results;
}
when i do var_dump($results) inside function i got the all array results
but when i call the function from another one i will got only the first element in $results array.
public function myFunction()
{
$results = $this->myRecursiveFunction();
}
I am not 100% sure what you wish to accomplish here, but recursive functions usually send their "workload" all the way to the innermost function, then the results are "added" on each other at the way back. If this is indeed what you want, you may need to change your code to something like this:
private function myRecursiveFunction()
{
$results = [];
//stuff related to $results
...
if(!$done){
.....
// Add the computed results of the recursive call to our data stack
$results[] = $this->myRecursiveFunction();
}
// Return the entire result array
return $results;
}
Related
This seems simple but I can not get a result of a function and take it to another function as I can do
function Operation() {
$result=5; return $result;
}
Going out on a limb here...
invoke (call) function Operation(), you get 5 returned, store that in $result.
invoke function Operation2($result) with argument $result.
Output: 6
<?php
function Operation()
{
$result = 5;
return $result;
}
function Operation2($result)
{
$all = $result +1;
echo $all;
}
$result = Operation();
Operation2($result); // output: 6
live demo
You might be a bit confused about functions and variable scope; I'd recommend you take a look at this
I want to return an array from function but it return nothing. If i declare any string that return fines but i need to return.
class someclasss {
function somefunction {
$arr = array();
if(condition){
array_push($arr, array("name"=>"john"));
return $arr;
}
}
}
I tried to return an simple array also but that also didnt worked!
Can anyone help to know how to return an array from function??
Your current code has two problems:
It is not returning the variable that contains the array. It is returning the result of the array_push() function, which is probably not what you want.
It is only calling return when inside the conditional. If the conditional doesn't fire, no return statement is encountered. This essentially is equivalent to doing return null; if the condition doesn't fire.
You probably want something more like:
function somefunction {
$arr = array();
if(condition){
array_push($arr, array("name"=>"john"));
}
return $arr;
}
Which can be short-cutted to:
function somefunction {
return condition ? ['name' => 'john'] : [];
}
I have a function that returns a multidimensional array, but I also need to return a single value. I am currently using the single value via the global keyword, so I can modify it inside the function.
But I'm wondering if there is another/better way to return 2 values from a function?
Current (pseudo) code:
global $iNumber;
$arrResult;
{do some calculations and queries}
$iNumber = 73;
return $arrResult;
The function that called this function can use the array of arrays, and also the global variable which has been updated to 73.
But is there another/better way to combine or pass these two different values?
You can do it in two ways: to return an array or an object.
Array solution:
function calculate(...) {
//do some stuff
return ['result' => $arrResult, 'iNumber' => $iNumber];
}
Object solution:
function calculate(...) {
//do some stuff
$object = new stdClass();
$object->result = $arrResult;
$object->iNumber = $iNumber;
return $object;
}
stdClass is just an example and you can create your own class for this purpose.
You can make a class and use a class variable.
Something like this:
class Test
{
public $iNumber;
function __construct()
{
# code...
}
public function yourCal()
{
// you calculations
$this->setNumber(73);
return $arrayOfArrays;
}
public function getNumber()
{
return $this->iNumber;
}
public function setNumber($var)
{
$this->iNumber = $var;
}
}
// Use the class in another php file
include 'Test.php';
$test = new Test();
// do calculations
$arrayOfArrays = $test->yourCal();
// get the number
$iNumber = $test->getNumber();
echo $iNumber; // returns 73
print_r($arrayOfArrays); // print your array
I'm having issue trying to bring an array filled with user information in library to show it in view.
I have this, in the library, here is generated the array.
$filter = $this->login_attribute.'='.$username;
$filter = "(&(objectCategory=person)({$filter}))";
$fields = array("cn","samaccountname","mail","memberof","department","givename","sn","usercedula","telephonenumber","phisycaldeliveryofficename");
$search = ldap_search($this->ldapconn,$this->search_base,$filter,$fields);
$entries = ldap_get_entries($this->ldapconn, $search);
$entries is the array with the user information. ¿How I move the array to view page?
Edit: The function where $entries is, return other value, $entries is use to reach that value.
A library is just a class. You load it:
$this->load->library('my_library');
Then you can use any functions in that library:
$this->my_library->my_function('some_data');
If you are expecting any return values you would use them for whatever, in an if statement, to assign to a variable, etc etc.
if ($this->my_library->my_function('some_data')) // Expecting a return of TRUE or FALSE
$my_variable = $this->my_library->my_function('some_data');
In your library, your function does whatever you need, and simply returns the data, just like any other function.
public function my_function($data)
{
// do some stuff
// return some results - variable or array or whatever
return $results;
}
Does that help at all? In your case you just need to return $entries.
You can pass data to view by doing something likes this:
in controller:
$data['entries']=$entries;
$this->load->view('view_name',$data);
in library:
class library_name
{
private $ci;
public function __construct()
{
$this->ci = &get_instance();
}
public function your_function(){
//$entries=...
$data['entries']=$entries;
$this->ci->load->view('view_name',$data);
}
}
in view_name:
print_r($entries);
How can I chain multiple methods together without knowing how many there will be? For instance how can I call this addMultiLink method more than once like a loop?
(new EntryField('products'))->addMultiLink($product_ids[0])
Basically I would want the result to be like this:
(new EntryField('products'))->addMultiLink($product_ids[0])->addMultiLink($product_ids[1])->addMultiLink($product_ids[2])
In your addMultiLink return $this:
public function addMultiLink($argument)
{
// your code here
return $this;
}
But as I can see you pass elements of array in your function per call.
Maybe it's better to rewrite addMultiLink and consider it's argument as array? Or check if it is array or some integer value:
public function addMultiLink($argument)
{
if (is_array($argument)) {
// do a foreach loop for example
} else {
// do something else
}
}
$product_entry_field = (new EntryField('products'));
foreach($product_ids as $product_id) {
$product_entry_field->addMultiLink($product_id);
}