Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Mainul Islam's avatar

How to Select all index of an array in laravel PHP

Hello, I am very new to PHP programming and Laracast. I have an array and I want to select all its indexes with * or something. I want to save the same value to all its indexes. I am trying it in laravel.

For example: public funciton arrayFunction() { $sampleArray = [ 'one' => true, 'two' => true, 'three' => true, ]; return $sampleArray; }

public function one() { $sampleArray = $this->arrayFunction(); // code to sell array index value False except one }

public function two() { $sampleArray = $this->arrayFunction(); // code to sell array index value False except two }

how can I do it without using foreach loop. Is it possible? If yes, how can I do it?

This is my first post on Laracast. Sorry for my bad English and if anything goes wrong through this post.. Thanks in advance.

0 likes
3 replies
jlrdw's avatar
$sampleArray = [ 'one' => true, 'two' => true, 'three' => true, ];
$sampleArray['one'];  would be true
Mainul Islam's avatar

thanks for the quick replay #jlrdw. I tried like this, but I am willing to do this:

public function one() {
 $sampleArray = $this->arrayFunction(); 
$sampleArray['one']	= false;  // returns false -- works fine

}
public function two() {
 $sampleArray = $this->arrayFunction(); 
$sampleArray['two']	= false; // index two and one return false , but I want index one should return true here when function two() runs

}

how can I do it?. thanks

jlrdw's avatar

Probably reset array to initial state first:

$sampleArray = [ 'one' => true, 'two' => true, 'three' => true, ];

Please or to participate in this conversation.