-1

Here is my code.

I am trying to get inspect link for steam item. I have tried to use preg_replace but no luck either.

    $API_link =     sprintf("http://steamcommunity.com/id/*steamid*/inventory/json/730/2?trading=1");
    $json = file_get_contents($API_link);
    $json_output = json_decode($json);
    $result = $json_output;
    $link = array();
    $id = array();
    foreach($result->rgDescriptions AS $item){  
    $empty = array();   
    $newstring = $item->actions[0]->link;
    if($newstring == NULL){
        continue;
    } else {
    $empty['link'] = $newstring;

    array_push($link, $empty);
    }       
    }       
    foreach($result->rgInventory AS $inventory){

        $empty = array();
        if($inventory->instanceid == 0){
            continue;
        } else {
            $empty['id'] = $inventory->id;
            array_push($id, $empty);        
        }
    }

    $array = array_merge($id, $link);

    foreach($array AS $final){

        $assetid = "%assetid%";

        echo str_replace($assetid, $final['id'], $final['link']);

    }

}

But its not working. Please see if you can help.

  • May I ask what "not working" means in this case? – Joachim Isaksson Feb 05 '16 at 22:34
  • so you really need 3 loops through the same data? –  Feb 05 '16 at 22:36
  • You cannot get all the required data from just one $result. I need the asset id which is only in rgInventory and pass it to rgDescriptions with an array. This is my logical solution but there might be another way. I can echo $final['link'] and $final['id'] seperately but not in str_replace – Oartia Free Feb 05 '16 at 22:54

2 Answers2

0

Description not enough. Maybe try dump some variables?

foreach($array AS $final){

    $assetid = "%assetid%";

    //Check what is in $final
    var_dump($final);

    echo str_replace($assetid, $final['id'], $final['link']);

}
LuckyLue
  • 158
  • 9
  • The information is inside the variables e.g I can echo them separately but they don't work in str_replace. Thanks – Oartia Free Feb 05 '16 at 22:55
  • I wan to see **var_dump output** for one record and corresponding result of str_replace. So can you show me for one row otuput from vad_dump and st_replace? And I hope that you know => these symbols are not wildcards here. – LuckyLue Feb 05 '16 at 23:06
  • array(1) { ["id"]=> string(10) "4980712180" } array(1) { ["id"]=> string(10) "4980712003" } – Oartia Free Feb 05 '16 at 23:31
  • array(1) { ["link"]=> string(112) "steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20S%owner_steamid%A%assetid%D2450073139943621757" } steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20S%owner_steamid%AD2450073139943621757array(1) { ["link"]=> string(113) "steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20S%owner_steamid%A%assetid%D16582832876545164542" } steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20S%owner_steamid%AD16582832876545164542 – Oartia Free Feb 05 '16 at 23:31
  • Something wrong is here. Is it **var_dump($final)**? Output should be array with min. **2** elements, not 1. *array(1) {* .. should be *array(2) {* – LuckyLue Feb 05 '16 at 23:36
  • Look for answer @Wizard - every elem your $final array has to be a array contains 2 elements, with keys 'id' and 'link' in this foreach. I asked at the beginning of the dump variables to rest a lot of time.. I suggest read more about arrays in PHP and arrays functions. And remember => always check value of yours variables when debuging, not once, not double, but more times.. – LuckyLue Feb 06 '16 at 00:15
0

As I can see you have array of arrays:

// bracket squares equivalent of array() keyword PHP >=v5.4
// here is
// $link = array(['link'=>'url'],['link'=>'url'])
// $id   = array(['id'=>'id'],['id'=>'id'])

// result will be:
//   array(['link']=>'url'],['link'=>'url'],['id'=>'id'],['id'=>'id'])
$array = array_merge($id, $link);

foreach($array AS $final){
    // here is the first $final
    // array('link'=>'url')

    $assetid = "%assetid%";

    // but here is we try to get
    // 'id' and 'link'
    echo str_replace($assetid, $final['id'], $final['link']);
}

I think it's some kind of mistake.

Ok, some test script:

<?php

$a = array( array('link'=>'hello1'), array('link'=>'hello2'));
$b = array( array('id'=>'id0'), array('id'=>'id1'));
$c = array_merge($a, $b);
var_dump($c);

result:

array(4) {
  [0] =>
  array(1) {
    'link' =>
    string(6) "hello1"
  }
  [1] =>
  array(1) {
    'link' =>
    string(6) "hello2"
  }
  [2] =>
  array(1) {
    'id' =>
    string(3) "id0"
  }
  [3] =>
  array(1) {
    'id' =>
    string(3) "id1"
  }
}

array_merge doesn't mix your associative arrays between them nether all nor particular item (I hope I explain it correct)

of course

foreach ($c as $item) {
  var_dump($item);
}

will enumerate all the items one by one

  array(1) {
    'link' =>
    string(6) "hello1"
  }

  array(1) {
    'link' =>
    string(6) "hello2"
  }

  array(1) {
    'id' =>
    string(3) "id0"
  }

  array(1) {
    'id' =>
    string(3) "id1"
  }

and there is no array that has both of them (link and id) in the item

This script can't associate link and id properly, cause some of links can be skipped by continue, some of id also can be skipped. And it will be just a random list of available information. You can stuck in the next situation:

 - $links has first 10 links
 - $id has 3,4,5,7,9,11

It's just a list. Even if you have only this pure info (no other details), you can't properly associate it between of them by using shown source.

Here is minimum 1 simple solutions:

don't skip, don't merge, just add an empty array, and your final loop will be like this:

$assetid = "%assetid%";
for ($link as $key=>$final) {
  if (count($final) && count($id[$key])) {
    echo str_replace($assetid, $id[$key]['id'], $final['link']);
  } else {
     // some of needed arguments absent
  }
} 
Wizard
  • 862
  • 6
  • 9