Looping queries to an API is really slow

I’ve heard that it is possible to loop JSON datas by using only one query and not doing querries for every loop. Any ideas how to make this from this code?
I really need to do it because it takes up to 3 minutes to successfully load the page..

* PHP *

<?php foreach ($return as $user) {

                        $link = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=$devkey&steamids=$user->SteamID";
                        $json = file_get_contents($link);
                        $decoding = json_decode($json);
                        $avatar = $decoding->response->players[0]->avatar;
                        ?>
                        <td id="addme"><img src="<?php echo $avatar; ?>" width="32" height="32"></td>
                        <td><?php echo $user->DisplayName; ?></td>
                        <td><?php echo $user->SteamID; ?>  <a href="http://steamcommunity.com/profiles/<?php echo $user->SteamID; ?>/" target="_blank"><span class="glyphicon glyphicon-new-window" id="gotosteamprofile"></span></a></td>
                        <td><?php
                        $ipredc = substr($user->Address, 0, -6);
                        echo $ipredc; ?></td>
                        <td><?php 
                        $toconver = $user->ConnectedSeconds;
                        $hours = floor($toconver / 3600);
                        $mins = floor($toconver / 60 % 60);
                        $secs = floor($toconver % 60);
                        $timeFormat = sprintf('%02d:%02d:%02d', $hours, $mins, $secs);
                        echo $timeFormat;
                        ?></td>
                        </tr>

                    <?php
                    }
                    ?>


Source: stackoverflow-php

How can I order a table in a loop depending on the highest number in a specific <td>

How can I order lines of a table depending on the highest number in a loop like this :

<?php
while ($d = $data->fetch()) {
$champs_files = fopen("champions/".$champion."_".$d['user_id'].".txt", "r");
$mastery_points = fread($champs_files,filesize("champions/".$champion."_".$d['user_id'].".txt"));
fclose($champs_files);
?>
<tr>
    <td><?= $rank += 1 ?></td>

        <td><?= $d['user_name'] ?></td>

        <td><?= $mastery_points ?></td>

        <td><?= $d['user_region'] ?></td>

    </tr>
<?php
}
?>

I want to put at the top the line who has the biggest $mastery_points .

I can’t use MySql cause I have limited number of tables thats why I use files .


Source: stackoverflow-php

PHP, for <= not expected results with decimals

When I run this PHP (5.4.31, running on 32-bit windows xp) code I get the result I’d expect:

for($i = 0; $i <= 10; $i = $i + 0.5)
    echo $i .' ';

But if I move the decimal point over as so:

for($i = 0; $i <= 1; $i = $i + 0.05)
    echo $i .' ';

despite the <= clause, the loop quits at 0.95 instead of at 1.

I’m aware that there is an imprecise nature to floating point numbers, but this seems kind of extreme.


Source: stackoverflow-php

How can I submit form data from a foreach loop?

This is my form:

<form action="update.php" method="post">
    
<button type="submit"> Submit</button> </form>

I insert data via Ajax:

 $.ajax({
     url: "data.php",
     data: {
         id: id,
     },
     type: "POST",
     success: function(data) {
         $("#data").html(data);
     }
 })

data.php:

<?php
$pdo = $db->prepare("SELECT * FROM data WHERE id = ?");
$pdo->execute(array($id));
while ($row = $pdo->fetch(PDO::FETCH_ASSOC))
    {
   ?>
   <input name="id[]" type="text" value="<?php echo $row['id'];?>">
   <input name="name[]" type="text" value="<?php echo $row['name'];?>">
<?php
}

As an Output I have for example:

2  Jim
3  Alan
5  Bob

Now in my update.php I want to update my data in the mySQL database:

<?php
foreach($_POST['id'] as $key => $value) {
    $id = $_POST['id'][$key];
    $name = $_POST['name'][$key];
    $pdo = $db->prepare("UPDATE data SET name=?, WHERE id=?;");
    $pdo->execute(array(
        $name,
        $id
    ));
        echo $id." ".$name;
}

As a result I expect to get again:

2  Jim
3  Alan
5  Bob

But what happens, I get only:

2  Jim


Source: stackoverflow-php

How to dynamically loop through different arrays in PHP?

If I want to loop through both of these arrays:

array (size=3)
  0 => 
    array (size=1)
      'email' => string 'testr@example.com' (length=17)
  1 => 
    array (size=1)
      'name' => string 'Joe' (length=3)
  2 => 
    array (size=1)
      'lastName' => string 'Doe' (length=3)

and

array (size=2)
  'email' => string 'taylor@example.com' (length=18)
  'name' => string 'Jay' (length=3)

The reason I am showing both arrays is I am trying to write a function to handle both arrays. I understand how to loop through the second array as I have been using in my function:

foreach($inserts as $colName => $colValue)
{
  //...
}

However, its the first array that I am struggling with, how can I handle looping through the first array while still supporting looping through the second array as my function could be passed both types of arrays that I showed above?

I did try and have a go with two foreach loops for the first array but I am wondering if there is a way where I can just have one loop to handle both types of arrays and if there is a way to detect the first type of array as I need to do something if the first type of array is passed in?


Source: stackoverflow-php