How to show in while an array with several things

1

Well, I'll try to explain my question to you 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 want to echo my array items, showing all the parameters of it, however it has to be in while, because I have several items, how can I do this?

Thank you.

    
asked by anonymous 04.11.2016 / 12:00

1 answer

2

Let's suppose that it ends (after this while it 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'];
}
    
04.11.2016 / 12:14