0

I have one array which is mixedtype both associative and indexed based elements, i want to remove all the values and keys from the array and make that array as empty,i tried unset function but it's not removing last element of the array, can you suggest some other efficient ways.

$myArray=['id','name'=>'=','address'=>['home'=>'dummydata','ofc'=>'ffff'],'status']; //declared in another class
$this->repository->myArray();
foreach($this->repository->myArray() as $key =>$value){
  unset($key);
  unset($value);
}

i want to make an array empty $myArray is declared in parent class i want to make the myArray empty

usersuser
  • 167
  • 12
  • You must unset the variables in the 'repository' class. – daniel Jan 31 '22 at 11:37
  • Maybe you can expand on why you want to do that just in case it's important? How does emptying that array help you, instead of just not doing anything with it? Possible solutions to your current question could be that you could make `myArray()` do return a reference or add a method to repository, something like `clearMyArray()`. – Hendrik Jan 31 '22 at 11:48

1 Answers1

0

It's a bit unclear what you're doing here (myArray() looks like a method, I'm not sure why, is it a method returning an array?... Pls make it a bit clearer), but if I understand correctly, you just need the array to have zero elements, so assigning an empty array would be the easiest:

$myArray = [];

If this is not the answer, let's refine the question :) You may want to include the other class (or a minimal relevant part of it) to help others understand what's going on.

dkellner
  • 8,726
  • 2
  • 49
  • 47
  • it's in another calss myArray () returns array that array looks like what i posted in question, i want to make the myArray array to be empty – usersuser Jan 31 '22 at 11:49
  • You can't modify it in the caller function. You need access to the variable itself, or do the emptying in the class itself. – dkellner Jan 31 '22 at 11:56