0

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.

4
  • 3
    explode and base64_decode? I wonder why you are having problems if you managed to do it the one way Commented Jun 13, 2014 at 17:53
  • @knittl I tried this :- echo explode('|', $_GET['set']); , but this did not help me. Commented Jun 13, 2014 at 17:55
  • 1
    Well yes, that will only output Array, since you cannot echo arrays directly in php Commented Jun 13, 2014 at 17:56
  • stackoverflow.com/questions/3745338/… php.net//manual/en/function.explode.php Commented Jun 13, 2014 at 17:58

2 Answers 2

5
$data = 'QnVzaW5lc3M=|RmluYW5jZQ==';
$result = array_map(
    'base64_decode',
    explode('|', $data)
);
var_dump($result);
Sign up to request clarification or add additional context in comments.

Comments

1

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
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.