How can I return data that is inside arrays?

Asked

Viewed 115 times

0

I am using this code to pull characteristics of a particular product:

<?php

$array = array(
    'key'       => '46dfg456465g4d654d65f4564dfg',
    'module'    => 'imoveis',
    'method'    => 'listar_origens' 
);

$client = new SoapClient(null, array (
    'uri'       => 'http://soap.imo.bi/',
    'location'  => 'http://soap.imo.bi/soap.dll',
    'trace'     => 'trace'
));

$res = $client->get($array);

echo "<pre>";
    print_r($res);
echo "</pre>";

?>

One of these characteristics is the field [field] which I need to recover 250 times because there are 250 different values for this field. The result of the script is this:

Array
(
    [0] => Array
        (
            [alias] => imo.DATA
            [field] => DATA
            [description] => Data cadastro
            [type] => Data
            [field_site] => 
        )

When placing $res[0]['field'] it returns me the first value that is DATE and so on as I change the values. I want to dynamically within a loop recover the 250 values at once. I ask for help!!!

  • 1

    I have no knowledge of PHP, but I believe that a loop would solve this situation, no?

  • 2

    Forehead foreach($res as $item){ echo $item['field']; }

  • Is this result complete? Or is it still 0.1.2...?

2 answers

1

Just make a loop, for example:

Using FOR

for($i = 0; $i < count($res); $i++){
echo $res[$i]['field'];
}

Using FOREACH

foreach($res as $field){
echo $field['field'];
}

In both ways, it will result in the gift value in $res[0]['field'], $res[1]['field'], $res[2]['field']...

1


If you have an array that has within each element another array associative, you can loop the first one and within the loop echo that key from the element of the first array that is being iterated.

Example:

echo "<pre>";
foreach($res as $item){
    echo $item['field'];
}
echo "</pre>";

Browser other questions tagged

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