Pass each result of an array to a variable

Asked

Viewed 981 times

2

I have this tie that lists all the lines of array, but I only need a few lines. How to pass each result to a variable?

$WS->key='00000000000';  // Atenção: Este parâmetro possui valores diferentes 

// Chamada do método e retorno da resposta.
$response=$WS->getInfo($param); // Esta função da classe WS_API retorna um objeto  
$control_sign=array();
$control_sign=$WS->getResponse('getInfo',$response);

foreach ($response as $name => $value) {
    echo $name .''. $value;
}

// Resultado
//   shopId         33015842
//   paymentMethod  E_COMMERCE
//   contractNumber 1026194250
//    Nome          Fabio

// Código com as Sugestões do Amigos
foreach ($response as $name => $value) {
    echo $linha = "{$name}{$value}";
    echo"<br>";
}

extract($response);
echo $amount;

I want to return only the lines shopId and paymentMethod.

2 answers

3


You can filter information from array imposing conditions, only if the key is shopId or paymentMethod, you print. See an example:

$response = array('shopId' => 33015842,
                  'paymentMethod' => 'E_COMMERCE',
                  'contractNumber' => 1026194250,
                  'Nome' => 'Fabio');

foreach ($response as $chave => $valor) {
    if ($chave == 'shopId' or $chave == 'paymentMethod'){
        echo "{$chave} {$valor} <br>";
    }
}

DEMO

Updating: The foreach was not necessary. To obtain the values of a object, access using the syntax ->. Take an example:

$response = (object) array('shopId' => 33015842,
                  'paymentMethod' => 'E_COMMERCE',
                  'contractNumber' => 1026194250,
                  'Nome' => 'Fabio');

$shopId = $response->shopId;
$paymentMethod = $response->paymentMethod;
$contractNumber = $response->contractNumber;

echo "{$shopId}\n";
echo "{$paymentMethod}\n";
echo "{$contractNumber}\n";

DEMO

  • OK almost that.... but I need to house line, move to a variable to be able to manipulate each one.

  • @Fabiohenrique Ali where the echo .. you can use a variable and concatenate the key and value like this: $linha = "{$chave}{$valor}";, then just use the variable linha.

  • Hello Friend excuse the ignorance but I’m new to it hasn’t worked for being in a loop I can’t declare the variables I changed the initial code showing as this my original array

  • What I need is something like $var1 = $line . " <br>"; $var2 = $line . " <br>";... or $var1 = $line["os"];

  • I would need variableness could be up to foreach

  • In this model the code would be very large because it must have more than 30 lines. I only need the values in case something like Extract($reply); would solve more ta not working

  • how I needed to http://pastebin.com/sJ1LLuHK

  • @Fabiohenrique I understand now. = ) See if this is it: http://ideone.com/5pDqGi

  • That’s right this is the most idea gave error Cannot use Object of type stdClass as array in

  • @Fabiohenrique $response is a object right? try to do so: $shopId = $response->shopId;, $paymentMethod = $response->paymentMethod;, etc..

  • 1

    Perfect... I can’t even put my thanks here....

Show 6 more comments

3

What you can do is use the function Extract of PHP.

This function treats keys as variable names and values with variable values. For each key/value pair it creates a variable in the current symbol table, following the extract_type and prefix parameters.

extract($resposta);
echo $shopId;
echo $paymentMethod; //...

Well, I believe this is the solution you need to send the array values to variables. It is more convenient than foreach for both implementation convenience and execution speed.

EXAMPLE

  • It would really look better off the foreach but it still didn’t work I changed the initial code showing like this my original array.

  • Interesting function. +1. In this case as $resposta is an object, it is necessary to convert it to array, example.

  • I just saw the update... Just cast a cast extract((array)$resposta) the same way you did on the object.

Browser other questions tagged

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