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.
Related
I am attempting to write a simple wrapper class to get the value of a global variable. I was thinking to use it like this:
print_r($class->session()->getAll());
print_r($class->cookie()->getAll());
Here's what I have:
class GlobalVars() {
private $current;
public function session() {
$this->current = 'SESSION';
return $this;
}
public function cookie() {
$this->current = 'COOKIE';
return $this;
}
public function getAll() {
return $_{$this->current}; // Obviously wrong
}
public function get($key) {
if (!isset($_{$this->current}[$key])) { // Obviously wrong
return false;
}
return $_{$this->current}[$key]; // Obviously wrong
}
public function set($arr) {
if (is_array($arr)) {
foreach ($arr as $k => $v) {
$_{$this->current}[$k] = $v;
}
}
}
}
$class = new GlobalVars();
print_r($class->session()->getAll());
With this example, I get a Notice: Undefined variable: _ message. What do I need to modify to get this to work?
In my opinion this is just a simple syntactical error you have made. What you did:
public function getAll() {
return $_{$this->current}; // Obviously wrong
}
But the correct way to emulate a variable from string is:
public function getAll() {
return ${"_".$this->current};
}
I have tested it. Similar behaviour for the other variables. More information on variable variables in the docs: http://php.net/manual/en/language.variables.variable.php
It's not gonna work like this. You need variable variables:
$var = "_{$this->current}";
var_dump($$var['rnd']);
Example
It's very bad way to use varVars, because it's not readable and usually IDE does not know what are you using and it's easy to get buggy code.
I have created a recursive function which returns the array value. So I am calling that function from another function. but it doesn't return any values. The function is given below,
public function sitemapAction() {
$sites = self::getNavigation();
foreach(self::recursiveSitemap($sites) as $url) {
...
...
}
}
public function recursiveSitemap($sites)
{
$result = array();
foreach ($sites as $site) {
$result[]= array($site['DFA']);
if (is_array($site['items'])) {
return recursiveSitemap($site['items']);
}
}
}
Please help me on this.
If you look carefully at your recursive method, you will see that it actually does not return anything at all:
public function recursiveSitemap($sites)
{
$result = array();
foreach ($sites as $site) {
$result[]= array($site['DFA']);
if (is_array($site['items'])) {
return recursiveSitemap($site['items']);
}
}
}
If your variable $site['items'] is an array you call your method again, going deeper, without doing anything with the returned value.
And if not?
It would seem you would need to add the result you get back from your recursive function call to your $result array and return that, but I don't know exactly what output you expect.
Apart from that you have a small typo, you need self::recursiveSitemap($site['items']) if you want to call the same method recursively.
A simple example:
public function recursiveSitemap($sites)
{
$result = array();
foreach ($sites as $site) {
if (is_array($site['items'])) {
// do something with the result you get back
$result[] = self::recursiveSitemap($site['items']);
} else {
// not an array, this is the result you need?
$result[]= array($site['DFA']);
}
}
// return your result
return $result;
}
Assuming that $result is the thing you want to get back...
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();
I have a problem trying to run a code inside the loop, my loop consist of a function.
Here is my coding:
$new = array(1,2,3,4);
for($i=0;$i<=3;$i++){
$val = $new[$i];
function myfunction($value) {
//Do something
}
echo $val;
}
The problem is the code outputs only the 1st value in my array. I am very confused, am I not suppose to declare a function inside the loop?
Your code ends up with Fatal error, since at the second iteration it tries to redeclare function myfunction. That's why it is printing only first value of array.
In order to avoid that fatal error you can check if that function has been already defined using function_exists() function like this:
$new = array(1,2,3,4);
for($i=0;$i<=3;$i++)
{
$val = $new[$i];
if(!function_exists('myfunction'))
{
function myfunction($value) {
//Do something
}
}
echo $val;
}
PHP is a scripting language and it is syntactically correct to declare a function inside for loop or inside if statement, but it is a bad practice and can cause a lot of errors afterwards.
The best way is to declare a function outside loop, and, if needed, call it from within a loop like this:
<?php
function myfunction($value) {
//Do something
}
$new = array(1,2,3,4);
for($i=0;$i<=3;$i++)
{
$val = $new[$i];
myfunction($value); //may you was intended to pass $val here?
echo $val;
}
Don't declare the function inside the loop, declare it before the loop and then call to it inside the loop with myFunction($value);
the function should be in a separate procedure
$new = array(1,2,3,4);
for($i=0;$i<=3;$i++)
{
$val = $new[$i];
myfunction($val)
echo $val;
}
then this is your function
function myfunction($value)
{
//Do something
}
Declare the function outside of the loop
either return a value from the function, or let the function output data
For example:
function myfunction($value) {
//Do something
echo $value;
}
$new = array(1,2,3,4);
for($i=0;$i<=3;$i++) {
myfunction($new[$i]);
}
I am assuming you want to print out the first 4 elements of the array.
do something like this
function myfunction() {
$new = array(1,2,3,4);
for($i=0;$i<=3;$i++){
$val = $new[$i];
echo $val;
}
}
myfunction();
You should declare the function outside the loop
function myfunction($value) {
return ($value + 25); // an example
}
$new = array(1,2,3,4);
for($i = 0; $i < count($new); $i++){
echo myfunction($new[$i]);
}
Also you should set the loop from 0 to the end of the array, so if you'll have more than 4 entries in the array the code should be ok
You can declare an anonymous function instead:
for ($i=0; $i<=3; $i++) {
// code
$myFunction = function($value) { /* code */ }
$myFunction($val);
// code
}
that is not the right way to do it...
first declare the function outside the loop, then call the function in the loop
function myfunction($value) {
//Do something
}
$new = array(1,2,3,4);
for($i=0;$i<=3;$i++){
$val = $new[$i];
myfunction( $val); //call function where u wanted... here (in your case)
echo $val;
}
You are not suposed to declare the function inside a loop...
So I am building a helper class that would store all get variables from a url, remove trailing spaces and return it so that other methods can use them.
The problem is that only the first value gets stored.
The url looks like:
https://pay.paymentgateway.com/index.php?name=xyz&amount=10.30&checksum=abcd
My code outputs:
Array
(
[name] => xyz
)
My code:
class helperboy
{
protected $cleanvariables = array();
public function store_get_variables($_GET)
{
foreach ($_GET as $key => $value)
{
return $this->cleanvalues[$key] = trim($value);
}
}
protected function display_variables()
{
echo "<pre>";
print_r($this->cleanvalues);
}
}
I know I am doing something silly and I would appreciate any help.
Also, how can I access specific variables like this in my other methods.:
$this->cleanvalues['name'];
$this->cleanvalues['amount'];
$this->cleanvalues['checksum'];
your return statement is the problem....
class helperboy
{
protected $cleanvariables = array();
public function store_get_variables($_GET)
{
foreach ($_GET as $key => $value)
{
$this->cleanvalues[$key] = trim($value);
}
return $this->cleanvalues;
}
protected function display_variables()
{
echo "<pre>";
print_r($this->cleanvalues);
}
}
Well, the problem is that...
public function store_get_variables($_GET)
{
foreach ($_GET as $key => $value)
{
return $this->cleanvalues[$key] = trim($value);
}
}
... the loop here will be executed just once. As soon as function hits return statement, it will abort this loop - and return immediately.
Yet I think there are some bigger problems here. First, I don't buy the idea of some omnipotent helper class that knows everything about everyone. If you intend to work with some cleaner request params, why don't just 'objectize' this instead:
class My_Http_Request
{
private $request;
protected function fillGetParams() {
$this->request['get'] = array_map('trim', $_GET);
}
public function getTrimmedParam($name) {
return $this->request['get'][$name];
}
public function __construct() {
$this->fillGetParams();
}
}
That's just an idea, not ready-made implementation (no checks for missing elements, no returning all params if 'getTrimmedParam' method is called without any arguments, etc.