PHP Compact() function returning a empty array -


i have array containing have created local variables using extract() function extr_prefix_all flag set. afterwards, called compact() on new prefixed variables created extract() displaying array created compact() using print_r() gives empty array(). sample code follows:

<?php $cities = array('city1' => "chicago", 'city2' => "boston");  extract($cities, extr_prefix_all, "new"); echo "city 1: {$new_city1} city 2: {$new_city2}" . "<br><br>";  $new_cities = compact($new_city1, $new_city2); print_r($new_cities); ?> 

i using php version 5.6. what doing wrong here?

in current code, you're providing values of each variables you're trying compact, not name of variables. acts variable variables behavior. in order make work properly, provide variable names strings or in array form:

$new_cities = compact(array('new_city1', 'new_city2')); // or $new_cities = compact('new_city1', 'new_city2'); 

here's excerpt manual:

parameters

varname1

compact() takes variable number of parameters. each parameter can either string containing name of variable, or array of variable names. array can contain other arrays of variable names inside it; compact() handles recursively.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -