简体   繁体   中英

how to add elements to an array inside a function php

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM