PHP foreach loop on dynamic array - php

I have to execute function "one" and function "two" at different places in my code.
I set the function one variables in a dynamic array in order to use them in the function 2 by setting the array as a global variable.
Then, when I call the function two by using the 1st value of the array, I can get the variable of each key.
This works well and it is ok when I have a limited number of arrays...
Now, I'll have to call many function one and I would like to automatize the execution of the function two.
I thought to use a foreach loop for this.
Unless, I am not familiar with foreach structure, I can not find the way to do it.
Thank you for your help.
Etienne
Here is my code :
function one($nom,$a,$b,$c)
{
global $$nom;
$$nom = array($a,$b,$c);
}
function two($nom)
{
global $$nom;
echo ${$nom}[0].'</br>'.${$nom}[2].'</br>';
}
one("D1",10,20,30);
one("D2",100,200,300);
two("D1"); // works but i do not want to write it manually
two("D2"); // I want to use the foreach loop for that
foreach(${$nom} as $value) {
two($nom);
}

Related

PHP: Can't use function return value in write context

Can't use function return value in write context.
All the search result responses say its something to do with the empty function, but I'm not using that?
foreach ($permission as explode(',', $permissionString)) { // line 44
if ($this->hasPermission($permission))
$count++;
}
In a foreach the expression on the left of the as should be the array that you want to iterate through, and the expression on the right is a variable that gets overwritten with the value of each element inside the array.
The reason that you get an error is because php is trying to write an element of $permission into explode(',', $permissionString), but this returns an error because explode(',', $permissionString) is a function call, not a variable, and only variables can be written to.
To fix this, try reversing the order of the as, like this:
foreach (explode(',', $permissionString) as $permission) {

When I run PHP function to append parameter to array, it only shows the most recent parameter passed as the only element within the array

$needleArray = array();
function filter(){
$x = $_POST['data'];
global $needleArray;
$filterArray = arraycompare($x, $needleArray);
echo "<ul id='suitCat'>";
foreach ($filterArray as $key) {
echo $key;
}
echo "</ul>";
die();
}
function arraycompare($x, $arrayParam){
echo $arrayParam;
array_push($arrayParam, $x);
return $arrayParam;
}
So what I want to happen is to pass in a parameter into the first function filter(), which is $x and then have that append to the global array $needleArray. So every time this function is called, it just keeps appending other elements into the already existing array. However, currently when I call the function multiple times, it says the only element currently in the array is the one I most recently passed into it. I'm sure it has something to do with global scope or something but I can't figure it out.
I also have die(); in there due to the fact that this is a AJAX function running in wordpress, which requires the function to have it.

Is that possible to get the reference to an array from the referent of its element?

I currently have a php function, in which the 1st parameter is a reference to an array and the 2nd parameter is one of its keys,
function fun(&$a, $k) {
......
}
I want to modify the function so that I just need to pass one parameter $a[$k]. Inside the function , $a can be extracted from $a[$k] and then I can call array_search($a[$k], $a) to get $k. Is that possible in PHP?
function fun(&$ak) {
// $ak is from $a[$k]
// a php utility to extract $a from $ak? ...
$k = array_search($ak, $a);
}
Short answer: No, there's no way to "extract" that information, because that information doesn't exist in the scope of your function.
Long answer:
As people have pointed out in the comments, you simply cannot do this. If you have a function like this:
function fun(&$foo) {
...
}
there is no information passed to that function about where $foo came from. It could be a standalone variable, an array element ($bar[1]), an object property ($baz->bingo), or anything else (think SomeClass::$bar->baz[$bingo->boingo]). There's no way to tell.
To verify this, try var_dump($ak); in your function; it won't contain any information about what array or object it's in (or its array index or property name). It's just like any other variable.

using arrays from one file in nested functions in another file

I have a PHP file which contains associative arrays. I have them all in one file for ease of editing/updating. For example:
$ass_first = array( "title"=>"Title",
"first_name"=>"First Name",
"surname"=>"Surname",
"phone"=>"Phone Number",
"email"=>"Email Address",
"add1"=>"House Name/Number",
"add2"=>"Street",
"add3"=>"Town",
"add4"=>"City",
"add5"=>"Post Code"
);
In another file, I have functions which are passed variables, which in turn call other functions.
For example I have a function which is called to create a table containing form inputs. Inside this function, there is a test to see what form input is needed and the necessary function is called.
function create_table($titles, $id) { //$titles is the relevant array from lists.php, $id is the id of the containing div
$select = array('timescale','bus_route','train_stat'); //'select' id list
foreach ($titles as $k=>$v) { //$k is the database/id/name $v is the description text
if (in_array($k,$select)){
select_box($k,$v);
}
else if //input is a text area
text_box($k,$v);
}
else{ //input is a checkbox
check_box($k,$v);
}
}
}
Inside these three 'nestled functions' I want to refer to some arrays contained in the lists.php file.
I want to know the best way to add these arrays to the functions scope. I have been told variable variables arent neccessarily the best way to go, neither is declaring global variables. I cant think of any other way to do it!
Also passing them as a variable into the functions isnt really an option because the three different nestled functions need a different set of arrays meaning three sets of arrays will be passed but only one would be used at any one time!
You can pass the arrays to your create_table function:
function create_table($titles, $id, $arrays) {
...
}
You can go ahead and use a global variable, maybe add a comment above it saying what file sets the variable:
function create_table($titles, $id) {
//$arrays gets set in lists.php
global $arrays;
...
}
Or you can require your lists.php file like so:
function create_table($titles, $id) {
require 'lists.php';
...
}

how to delete item from an array with filter?

I want to filter and delete an item from an array. is it possible to do it with array_filter() ?
//I want to delete these items from the $arr_codes
$id = 1223;
$pin = 35;
//Before
$arr_codes = Array('1598_9','1223_35','1245_3','1227_11', '1223_56');
//After
$arr_codes = Array('1598_9','1245_3','1227_11', '1223_56');
Thanks!
You can find the index of the value you are interested in with array_search and then unset it.
$i = array_search('1223_35',$arr_codes);
if($i !== false) unset($arr_codes[$i]);
array_filter does not take userdata (parameters). array_walk() does. However, none of the iterator function allow modifying the array structure within the callback.
As such, array_filter() is the appropriate function to use. However, since your comparison data is dynamic (per your comment), you're going to need another way to obtain comparison data. This could be a function, global variable, or build a quick class and set a property.
Here is an example using a function.
array_filter($arr, "my_callback");
function my_callback($val) {
return !in_array($val, get_dynamic_codes());
}
function get_dynamic_codes() {
// returns an array of bad codes, i.e. array('1223_35', '1234_56', ...)
}

Categories