Pass data Between functions in Codeigniter - php

I have two functions and Im trying to pass an array of data to the function that have the view like this:
function one()
{
$data['array'] = $array;
$this->load->view('etc/asdf', $data);
}
function two()
{
$array[];
return $array;
}
What Im doing wrong?
Thank you in Advance!

Try:
$data['array'] = $this->two(); // Instead of $array, as it is undefined
$this->load->view('etc/asdf', $data);
And also to define array you have to do following $array = array();, not $array[];
Note: You have to invoke function rather than specify $array, as it is returned from function called two

function one()
{
$data['array'] = $this->two();
$this->load->view('etc/asdf', $data);
}
function two()
{
$array= array('1','2','2');
return $array;
}
instead of $data['array'] = two();
use $data['array'] = $this->two();

Related

PHP array_map callback function inside function

I have a function called createCost, and inside that function, I have an array_map that takes in an array and a function called checkDescription that's inside that createCost. Below is an example:
public function createCost{
$cost_example = array();
function checkDescription($array_item)
{
return $array_item;
}
$array_mapped = array_map('checkDescription', $cost_example);
}
When I run this I get a
array_map() expects parameter 1 to be a valid callback, function 'checkDescription' not found or invalid function name
Which to my understanding is that it looked for the function checkDescription outside that createCost but how can I call checkDescription from inside?
Do like this
public function createCost(){
$cost_example = array();
$array_mapped = array_map(function ($array_item){
return $array_item;
}, $cost_example);
}
Why not assign the function to a variable?
public function createCost{
$cost_example = array();
$checkDescription = function ($array_item) {
return $array_item;
}
$array_mapped = array_map($checkDescription, $cost_example);
}
Isn't this more readable too?

Failed Using array_merge inside of PHP class

class Test
{
public $data = array();
public function addData($data = array())
{
array_merge($data, $this->data);
return $this;
}
public function showData()
{
print_r($this->data);
}
}
$test = new Test;
$test->addData(array("halo", "zaki"))->showData();
i tried to merging 2 array, but it doesn't work, maybe someone can explain to me?
array_merge does not modify the arrays passed to it, but rather returns the result.
Try this:
public function addData($data = array())
{
$this->data = array_merge($data, $this->data);
return $this;
}
You forgot to assign the resulting array to member variable $data. It should be,
$this->data = array_merge($data, $this->data);

How I can grab all public function methods in a controller?

I use __remap() function to avoid any undefine method and make it redirect to index() function.
function __remap($method)
{
$array = {"method1","method2"};
in_array($method,$array) ? $this->$method() : $this->index();
}
That function will check if other than method1 and method2.. it will redirect to index function.
Now, how I can automatically grab all public function methods in that controller instead of manually put on $array variable?
You need to test if method exists and is public. So you need use reflection and method exists. Something like this:
function __remap($method)
{
if(method_exists($this, $method)){
$reflection = new ReflectionMethod($this, $method);
if($reflection->isPublic()){
return $this->{$method}();
}
}
return $this->index();
}
Or you can use get_class_methods() for create your array of methods
OK, I was bored:
$r = new ReflectionClass(__CLASS__);
$methods = array_map(function($v) {
return $v->name;
},
$r->getMethods(ReflectionMethod::IS_PUBLIC));
I've modified the codes and become like this.
function _remap($method)
{
$controllers = new ReflectionClass(__CLASS__);
$obj_method_existed = array_map(function($method_existed)
{
return $method_existed;
},
$controllers->getMethods(ReflectionMethod::IS_PUBLIC));
$arr_method = array();
//The following FOREACH I think was not good practice.
foreach($obj_method_existed as $method_existed):
$arr_method[] = $method_existed->name;
endforeach;
in_array($method, $arr_method) ? $this->$method() : $this->index();
}
Any enhancement instead of using foreach?

Use Array From Function 1 in Function 2

I have a (simplified) function that uses in_array() to check if a value is in an array:
function is($input) {
$class = array('msie','ie','ie9');
$is = FALSE;
if (in_array($input, $class)) {$is = TRUE;}
return $is;
}
if (is('msie'))
echo 'Friends don\'t let friends use IE.';
I want to break this into two separate functions, where the first defines the array:
function myarray() {
$class = array('msie','ie','ie9');
}
and the second runs the check—either like this:
function is($input) {
myarray();
$is = FALSE;
if (in_array($input, $class)) {$is = TRUE;}
return $is;
}
Or this:
function is($input) {
global $class;
$is = FALSE;
if (in_array($input, $class)) {$is = TRUE;}
return $is;
}
But both of the above cause this error:
Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/vanetten/temp.ryanve.com/PHP/airve.php on line 73
What is the proper way use an array from one function in another? Can an array be a global variable? How do I make this work? Is it more efficient to use a global variable or to call the first function within the second function. Any help is definitely appreciated.
Return the array from the first function:
function myarray() {
return array('msie','ie','ie9');
}
function is($input) {
$array = myarray();
return in_array($input, $array);
// or even just
// return in_array($input, myarray());
}
function is($input) {
$class = myarray();
$is = false;
...
Easiest way (which also negates the use of global variables, which is a bad practice since using $class somewhere else down the line may result in unexpected behavior) is something like
function myarray() {
return array('msie','ie','ie9');
}
function is($input) {
$array = myarray();
$is = FALSE;
if (in_array($input, $array)) {$is = TRUE;}
return $is;
}
if (is('msie'))
echo 'Friends don\'t let friends use IE.';
In this example, we just make myarray() return the needed array. In is(), add the line $array = myarray(), which will save the array from myarray(), so it is useable from is() as the alias $array. Then simply change $class to $array, and it should work fine.

how to travel through the array in php?

i am having one key list
for example
$key_list=array("list"=>array("task","duration"));
function array_key_fun($key_list,$test_input){
//(is_array($test_input)){
return array_map('myfunction',$test_input,$key_list);
//}
}
//$va=array_map("myfunction",$test_input);
//print_r(array_key_fun($key_list,$test_input));
function myfunction($arr)
{
if(is_array($arr))
{
$get_array= get_childs($arr);
return $get_array;
}
}
function get_childs($arr){
$newarr=array();
$newarr_en='';
foreach($arr as $key=>$value)
{
if(is_array($value)){
$newarr[$key]=get_childs($value);
}else{
if (in_array($key,$key_list)) //here im facing the problem with key_list
{
..............
}
else
{
...............
}
}
}
return $newarr;
}
Either pass in function or declare as global
function abc($a,$key_list){
OR
function abc($a){
global $key_list;
//rest of code
EDIT:
When you pass the array as parameter of function you have to pass the value in call as well
when you call this function this should be
//array should be declared before calling function
$key_list=array("list"=>array("task","duration"));
abc($a,$key_list); //pass this array
http://php.net/manual/en/function.array-walk.php
array_walk
try this
You have to bring the variable into scope, within your code you have ......... if you replace that with global $key_list this will allow the function to Read / Write to that stack.

Categories