Jake Wilson
Jake Wilson

Reputation: 91213

Push variable to array using name as key?

I have the following variables:

$something = 'whatever';
$hello = array();
$test = 123;

and I want to push them into an array like this:

$data['something'] = 'whatever';
$data['hello'] = array();
$data['test'] = 123;

I was wondering if there was some sort of PHP function (similar to array_push()) that can push a variable into an array, using the name of the variable as the key value?

Upvotes: 3

Views: 1263

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324750

Yes there is. compact() will do exactly this.

Upvotes: 6

Related Questions