Arrays in a single index

Asked

Viewed 60 times

0

I made this question today, helped me a lot, now I have the code:

foreach ($tokens as $row) {
    $token1[] = $row['oauth_token'];
    $token2[] = $row['oauth_token_secret'];
}

var_dump($token1);
var_dump($token2);

And he returns to me:

inserir a descrição da imagem aqui

I wanted to turn these tokens all into 1 index only how can I do this?

Example:

0 => string 'todos aqui';

========================================

EDIT:1

I want to extract the contents, from arrays for example, I now have this:

inserir a descrição da imagem aqui

using this code:

while ($tokens = $this->selectTokens()) {
    foreach ($tokens as $row) {
        $tokenList[] = [
            'oauth_token'               => $row['oauth_token'],
            'oauth_token_secret'    => $row['oauth_token_secret'],
        ];
    }

    #$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $tokenList['oauth_token'], $tokenList['oauth_token_secret']);

    var_dump($tokenList);

    break;
}

Well I guess I figured it out by now, now I want you to understand this:

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $tokenList['oauth_token'], $tokenList['oauth_token_secret']);

I mean, get them all oauth_token and oauth_token_secret, that comes from a banco de dados, to make a looping. But in doing so I get this:

inserir a descrição da imagem aqui

Right here $tokenList['oauth_token'], $tokenList['oauth_token_secret']

  • But all together in one string ? or a string for the oauth_token and another to the oauth_token_secret ?

  • That, I need to go to Twitteroauth see this print of how it looks now: http://prntscr.com/g09nwb, the problem is to recover them now. With the answer code below, I did everything, but only returns 1, I need to return all, example tokenList[1] returns http://prntscr.com/g09ohu, I need to separate this. for example tokenList['oauth_token'] and tokenList['oauth_token_secret']

  • But then I’d stay for the oauth_token with 1 token 500 characters long? That’s the idea?

  • The idea is to return, all tokens both token, secret understood? in a loop and pass Twitteroauth: see if you can understand with this print http://prntscr.com/g09r6p, but doing so from the Undefined index: oauth_token

  • Obs: these tokens come from Banco de Dados

  • As far as I have seen in the Twitter documentation this class can only receive 1 oath_token and 1 oath_token_secret. Then putting more in a string probably wouldn’t work, but I may have gotten the wrong idea.

  • Isac works, I have a similar code but using mysql_* with PDO I’m having problems.

  • That instruction has to be within the for at the end of it, and will create a connection for each token/$row

  • Do you have an example? I don’t mean to be ignorant, but I don’t understand :(

  • I edited my answer with an example of what I was trying to say

Show 5 more comments

1 answer

2


By adjusting the code that has now shown connection the connection has to pass into the for:

while ($tokens = $this->selectTokens()) {
    foreach ($tokens as $row) {
        $tokenList[] = [
            'oauth_token'               => $row['oauth_token'],
            'oauth_token_secret'    => $row['oauth_token_secret'],
        ];

        //uma conexao para cada registo aqui
        $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $tokenList['oauth_token'], $tokenList['oauth_token_secret']);
    }
}

//agora aqui tem uma lista de conexões estabelecidas em $connection

print_r($connection);

Simpler is to use the $row directly:

while ($tokens = $this->selectTokens()) {
    foreach ($tokens as $row) {  
        $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, 
                                         $row['oauth_token'], $row['oauth_token_secret']);
    }
}
  • Yes I tried that too, I’ll edit my topic and explain it right.

  • @Willbb Lol well it seemed to me that it was not the intended one. But unfortunately I could not realize what the same goal was. Try to exemplify a result to make it clear.

  • edited the topic see if you can understand now.

  • Undefined index: oauth_token in C:\wamp64\www\megafollowapi\api\class\TwitterOAuthHelper.php on line 40 well I think you gave me a light I’ll try to fix.

  • @Willbb Line 40 is which?

  • "Simplest is to use the $Row directly:", now it’s returning me certain guy, thank you.

  • Only one correction, do not use $connection[] and yes $connection if it doesn’t double the tokens. and I only want 1 of each ^_^

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.