How to show a while array of various things

Asked

Viewed 289 times

1

Well, I’ll try to explain my doubt to myself in the best way:

    <?php
    $steamid = 76561198337627631;
        $prices = file_get_contents('prices.txt');
        $prices = json_decode($prices, true);
        $inv = curl('https://steamcommunity.com/profiles/'.$steamid.'/inventory/json/730/2/');
        $inv = json_decode($inv, true);
        $items = array();
        foreach ($inv['rgInventory'] as $key => $value) {
            $id = $value['classid'].'_'.$value['instanceid'];
            $trade = $inv['rgDescriptions'][$id]['tradable'];
            if(!$trade) continue;
            $name = $inv['rgDescriptions'][$id]['market_hash_name'];
            $price = $prices['response']['items'][$name]['value']*10;
            $img = 'http://steamcommunity-a.akamaihd.net/economy/image/'.$inv['rgDescriptions'][$id]['icon_url'];
            $items[] = array(
                'assetid' => $value['id'],
                'bt_price' => "0.00",
                'img' => $img,
                'name' => $name,
                'price' => $price,
                'reject' => $reject,
                'sa_price' => $price,
                'steamid' => $steamid);
        }


        // curl

function getTemplate($name, $in = null) {
    extract($in);
    ob_start();
    include "template/" . $name;
    $text = ob_get_clean();
    return $text;
}

function curl($url) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}


 ?>

I have the following code, and I intend to echo my array items, showing all parameters of it, however it has to be in while because I have several items, how can I do this?

Thank you.

  • But yours with several items will only be composed at the end of that right while? Why not foreach after the while and there you can print each parameter of each item

  • Could you give me an example for kindness?

1 answer

3


Soponhamos que acaba (after that while is finished) with these 2 items:

$items[] = array(
    'assetid' => 1,
    'bt_price' => "0.00",
    'img' => '123.jpg',
    'name' => 'item1',
    'price' => '12.00',
    'reject' => 'Sim',
    'sa_price' => '10.00',
    'steamid' => 'steam1'
);
$items[] = array(
    'assetid' => 2,
    'bt_price' => "0.00",
    'img' => '456.jpg',
    'name' => 'item2',
    'price' => '2.00',
    'reject' => 'Não',
    'sa_price' => '1.00',
    'steamid' => 'steam2'
);

Then you can do:

Way 1:

foreach($items as $item) {
    echo '<b>Detalhes do item ' .$item['name']. '</b>:<br>';
    echo implode(', ', $item). '<br><br>';
}

Way 2 (here we associate the key with the value):

foreach($items as $item) {
    echo '<br><br><b>Detalhes do item ' .$item['name']. '</b>:<br>';
    echo implode(', ', array_map(
        function ($v, $k) { return $k. ':' .$v; }, $item, array_keys($item))
    );
}

Way 3 (here we print only the keys/values we want):

foreach($items as $item) {
    echo '<br><br><b>imagem do item ' .$item['name']. '</b>:<br>';
    echo 'img: ' .$item['img'];
}
  • That’s right Miguel, but in case I want to show only for example the img of each item as I would do? As I would mention in the array?

  • Ha this is simpler, I’ll put way 3 with this example

  • Okay Thank you.

  • As I put in way 3 to access img suffice $item['img']

  • Thanks Miguel for the help That’s just what I needed!

Browser other questions tagged

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