Pass each result of an array to a variable

2

I have this loop that lists all the rows in the array , but I only need some rows. 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 only the

asked by anonymous 03.05.2015 / 20:00

2 answers

3

You can filter the information in the array condition, only if the key is shopId or paymentMethod , you print. Here's 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

Update : foreach was not required. To get the values of a object , go to the syntax < a href="http://php.net/manual/en/sdo.sample.getset.php"> -> . Here's 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

    
03.05.2015 / 20:10
3

What you can do is use PHP's extract function.

  

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 get array values into variables. It is more convenient than the foreach for both the practicality of implementation and the speed of execution.

EXAMPLE

    
03.05.2015 / 22:45