1

I'm trying to create a web panel for a client who parses information with base64 encoding. I have set up the way to decode it, and if I echo the decoded part in the loop ($data[1]), it will write the correct information. However, I need to be able to put the information into an SQL table and perform work on it before I do that. So, I'm trying to put the data into an array, but it is acting like the array is empty for some reason.

$postData has the base64 decoded information, and it was exploded using & as the exploding agent.

$tokens = array ();
for ($i = 0; count($postData) > $i; $i++) {
    $data = explode("=", $postData[$i]);
    $tokenAdd = array();
    $tokenAdd[] = $data[1];
    array_push($tokens, $tokenAdd);
}

var_dump($tokenAdd);
1
  • You should use $tokens[][] = $data[1]; it does the same but with less function calls because you don't use array_push(). Commented Jun 19, 2014 at 12:39

2 Answers 2

2

Are you shure you are dumping correct variable? Because $tokenAdd is rewritten on each iteration.

$tokens = array ();
for ($i = 0; count($postData) > $i; $i++) {
    $data = explode("=", $postData[$i]);
    // token is empty array;
    $tokenAdd = array();
    $tokenAdd[] = $data[1];
    // push array with one string element into $tokens
    array_push($tokens, $tokenAdd);
}
// dump $tokens, not $tokenAdd
var_dump($tokens);

To simplify code you may try this

$tokens = array();
foreach ($postData as $postDataItem) {
    $data = explode("=", $postDataItem);
    $tokens[] = array($data[1]);
}
var_dump($tokens); // array(array('containing'), array('some'), array('strings'))

or even more simple, if you are ok dealing not with array of array and with array of strings

$tokens = array();
foreach ($postData as $postDataItem) {
    $data = explode("=", $postDataItem);
    $tokens[] = $data[1];
}
var_dump($tokens); // array('containing', 'some', 'strings')
Sign up to request clarification or add additional context in comments.

1 Comment

Well I had code like the 3rd snippet, or exactly that I could have sworn, but it did not work. However, I think using the foreach made it work. Either way, thanks for the help!
0

Here is some code:

$count = count ( $postData );
$tokens = array ();
for($i = 0; $i < $count; $i ++) {
    $data = explode ( "=", $postData [$i] );
    $tokens [$i] = $data [1];
}

var_dump ( $tokens );

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.