user3605847
user3605847

Reputation: 65

Getting array values from an imploded function

so I have this variable $_GET which receives the value such as

set=QnVzaW5lc3M=|RmluYW5jZQ==

The values are base64 enconded using base64_encode() and then separated by a delimiter '|'. I'm using implode function to generate the value of the set variable.

Now, the question is, how can I get the values from the set variable into an array and base64 decode them as well ?

Any suggestions are welcome. I tried this :-

$test = array();
$test = explode('|', $_GET['set']);
var_dump($test);

This throws the value which is not usable. But this made no difference.

Upvotes: 0

Views: 58

Answers (2)

Giacomo1968
Giacomo1968

Reputation: 26066

This should work using foreach:

// Set the test data.
$test_data = 'QnVzaW5lc3M=|RmluYW5jZQ==';

// Explode the test data into an array.
$test_array = explode('|', $test_data);

// Roll through the test array & set final values.
$final_values = array();
foreach ($test_array as $test_value) {
  $final_values[] = base64_decode($test_value);
}

// Dump the output for debugging.
echo '<pre>';
print_r($final_values);
echo '</pre>';

The output is this:

Array
(
    [0] => Business
    [1] => Finance
)

Upvotes: 1

Mark Baker
Mark Baker

Reputation: 212412

$data = 'QnVzaW5lc3M=|RmluYW5jZQ==';
$result = array_map(
    'base64_decode',
    explode('|', $data)
);
var_dump($result);

Upvotes: 5

Related Questions