how to add elements to a global array from inside a function if element not exist in array?
my main code will call to function multiple times.but each time different elements will create inside the function
my sample current code is,
$all=[];
t(); // 1st call
t(); //2nd call
function t(){
$d='2,3,3,4,4,4'; //this is a sample.but element will different for each function calling
$d=explode(',',$d);
foreach($d as $e){
if(!in_array($e,$all)){
array_push($all, $e);
}
}
}
print_r($all);
output is empty,
Array()
but i need it like this
Array
(
[0] => 2
[1] => 3
[2] => 4
)
thank you
If you look at the variable scope in PHP http://php.net/manual/en/language.variables.scope.php
You'll see that functions don't have access to the outer scope.
Therefore you'll need to do either pass the array by reference:
function t(&$myarray)
Create an array inside of the function and returning that one
function t(){
$all = [];
$d='2,3,3,4,4,4';
$d=explode(',',$d);
foreach($d as $e){
if(!in_array($e,$all)){
array_push($all, $e);
}
}
return $all;
}
Or if you want to keep adding to the array you can do
function t($all){
$d='2,3,3,4,4,4';
$d=explode(',',$d);
foreach($d as $e){
if(!in_array($e,$all)){
array_push($all, $e);
}
}
return $all;
}
Then calling the function with $all = t($all);
Your code will show errors as $all isn't in the scope of the function, you need to pass the value in to have any effect...
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$all=[];
t($all); // 1st call
t($all); //2nd call
function t( &$data){
$d='2,3,3,4,4,4'; //this is a sample.but element will different for each function calling
$d=explode(',',$d);
foreach($d as $e){
if(!in_array($e,$data)){
array_push($data, $e);
}
}
}
print_r($all);
Result
Array
(
[0] => 2
[1] => 3
[2] => 4
)
You can use global, but this is generally discouraged.
To add on Alexy's response regarding the use of 'array_unique($d)', which I recommend as it eliminates the need for the loop. You can pass the filtered array to array_values($d) to index your elements as shown by the results you want to achieve. FYI: array_unique will preserve the original keys: http://php.net/manual/en/function.array-unique.php
Your case will require removing duplicates a few times its best to have a separate function for that:
$all = [];
function t(){
global $all;//Tell PHP that we are referencing the global $all variable
$d='2,3,3,4,4,4';
$d=explode(',',$d);
$d=rmvDuplicates($d);
$all = array_merge($all,$d);//Combine the new array with what we already had
$all = rmvDuplicates($all);
}
function rmvDuplicates(array){
$array=array_unique($d);
$array=array_values($d);
return $array;
}
Related
<!DOCTYPE html>
<html>
<body>
<?php
$array_task=array();
fillArray($array_task);
function fillArray($array){
$array1=array();
$array2=array();
for ($i=0;$i<8;$i++){
$array1[$i]=$i+1;
$array2[$i]=rand(0,100);
}
$array=array_combine($array1,$array2);
echo"Array before any editions <br>";
print_r($array);
echo"<br>Array after adding sum and multiplication <br>";
addSumMulti($array);
print_r($array);
echo"<br>Array after adding last element after each array element <br>";
#addAfterEach($array);
}
function addSumMulti($array){
$sum=0;
$multiplication=1;
for ($i=1;$i<=10;$i++){
$sum+=$array[$i];
if ($i==3 || $i==4){
$multiplication*=$array[$i];
}
}
$array[9]=$sum;
$array[10]=$multiplication;
print_r($array);
echo "<br>";
}
?>
</body>
</html>
first print_r ($array) shows 8elements, then in the next function i add 2 elements. And print_r in the function addSumMulti shows 10 elements, but print_r after function addSumMulti inside the fillArray shows only 8 elements. What is wrong, what should I change so that i could see 10 elements in print_r after addSumMulti (21 line)?
In PHP ordinary variables are passed by value*. So, when you pass your array to your function a new copy is made which your function code manipulates. Your function doesn't return any values, so the new values of your array are lost, and the calling code sees only the original copy of the array.
There are two ways to make the changes to the original array:
return a value from the function. e.g.:
function addLine($arr) {
$arr[] = "Additional Line";
return $arr
}
$arr = ["First line"];
$arr = addLine($arr);
or pass in the variable by reference. E.g:
function addLine(&$arr) {
$arr[] = "Additional Line";
return $arr
}
$arr = ["First line"];
addLine($arr);
See Variable scope and Passing By Reference
*Objects are always passed by reference
Your $array has not been updated due to variable scope.
$array declared inside fillArray() cannot be referenced in another function. To do so It should be global variable.
Either modify the function addSumMulti to return an array and use the same in fillArray OR make $array as a global variable.
Option 1:
function fillArray($array){
.....
$arrayAfterSum = addSumMulti($array);
.....
}
function addSumMulti($array){
.....
.....
return $array;
}
Option 2:
$array = array(); // its a gloabal variable
function fillArray($array){
global $array; // in order to access global inside function
.....
.....
}
function addSumMulti($array){
global $array; // in order to access global inside function
.....
.....
}
I've a function which returns an array. To return the value of that array I do something like this:
$obj->methodname()[keyvalue];
This works in php 5.4 only. I want to make this code work in lower php versions.
My code:
class ObjectTest {
public $ar;
function __construct() {
$this->ar = array(
1 => 'beeldscherm',
2 => 'geluidsbox',
3 => 'toetsenbord',);
}
public function arr(){
return $this->ar;
}
}
$obj = new ObjectTest();
//by calling the method and putting square brackets and the key of the element
var_dump($obj->arr()[2]);
I've rewritten the code for lower versions like this:
public function arr($arg = null){
if(is_null($arg)){
return $this->ar;
}
return $this->ar[$arg];
}
I'm doubting if this solution is an elegant one. What would you say? Any better solutions?
You can do like, store array in a variable and than access particular array index.
$arrList = var_dump($obj->arr());
echo $arrList[2];
how do I call a function from within a function?
function tt($data,$s,$t){
$data=$data;
echo '[[';
print_r($data);
echo ']]';
if($t==0){
$data[]=$s;
tt($data,'two',1);
}else{
$data[]=$s;
}
return $data;
}
print_r(tt('','one',0));
I want 'two' to be shown within the array like
$o[]='one';
$o[]='two';
print_r($o);
function tt($s, $t, array $data = array()) {
$data[] = $s;
if ($t == 0) {
$data = tt('two', 1, $data);
}
return $data;
}
print_r(tt('one', 0));
This is all that's really needed.
Put the array as the last argument and make it optional, because you don't need it on the initial call.
When calling tt recursively, you need to "catch" its return data, otherwise the recursive call simply does nothing of lasting value.
No need for the else, since you're going to append the entry to the array no matter what and don't need to write that twice.
Try this one (notice the function signature, the array is passed by ref &$data):
function tt(&$data,$s,$t){
echo '[[';
print_r($data);
echo ']]';
if($t==0){
$data[]=$s;
tt($data,'two',1);
}else{
$data[]=$s;
}
return $data;
}
$array = [];
tt($array,'one',0);
print_r($array);
/**
Array
(
[0] => one
[1] => two
)
*/
try this
function tt($data,$s,$t){
global $data;
if($t==0){
$data[]=$s;
tt($data,'two',1);
}else{
$data[]=$s;
}
return $data;
}
print_r(tt('','one',0));
OUTPUT :
Array
(
[0] => one
[1] => two
)
DEMO
I have a question about a recursive PHP function.
I have an array of ID’s and a function, returning an array of „child id’s“ for the given id.
public function getChildId($id) {
…
//do some stuff in db
…
return childids;
}
One childid can have childids, too!
Now, I want to have an recursive function, collecting all the childids.
I have an array with ids like this:
$myIds = array("1111“,"2222“,"3333“,“4444“,…);
and a funktion:
function getAll($myIds) {
}
What I want: I want an array, containing all the id’s (including an unknown level of childids) on the same level of my array. As long as the getChildId($id)-function is returning ID’s…
I started with my function like this:
function getAll($myIds) {
$allIds = $myIds;
foreach($myIds as $mId) {
$childids = getChildId($mId);
foreach($childids as $sId) {
array_push($allIds, $sId);
//here is my problem.
//what do I have to do, to make this function rekursive to
//search for all the childids?
}
}
return $allIds;
}
I tried a lot of things, but nothing worked. Can you help me?
Assuming a flat array as in your example, you simply need to call a function that checks each array element to determine if its an array. If it is, the function calls it itself, if not the array element is appended to a result array. Here's an example:
$foo = array(1,2,3,
array(4,5,
array(6,7,
array(8,9,10)
)
),
11,12
);
$bar = array();
recurse($foo,$bar);
function recurse($a,&$bar){
foreach($a as $e){
if(is_array($e)){
recurse($e,$bar);
}else{
$bar[] = $e;
}
}
}
var_dump($bar);
DEMO
I think this code should do the trick
function getAll($myIds) {
$allIds = Array();
foreach($myIds as $mId) {
array_push($allIds, $mId);
$subids = getSubId($mId);
foreach($subids as $sId) {
$nestedIds = getAll($sId);
$allIds = array_merge($allIds, $nestedIds);
}
}
return $allIds;
}
In PHP, I have created a user defined function. Example:
<?php
function test($one, $two) {
// do things
}
?>
I would like to find the names of the function parameters. How would I go about doing this?
This is an example of what I would like:
<?php
function test($one, $two) {
// do things
}
$params = magic_parameter_finding_function('test');
print_r($params);
?>
This would output:
Array
(
[0] => one
[1] => two
)
Also this is very important that I am able to get the user defined function parameter names outside the scope of the function. Thanks in advance!
You can do this using reflection...
$reflector = new ReflectionFunction('test');
$params = array();
foreach ($reflector->getParameters() as $param) {
$params[] = $param->name;
}
print_r($params);
you can use count_parameter a php built in function