Search code examples
phpconcatenationecho

Build a known variable name in PHP with echo and concatenation


Having a bit of trouble building a variable name in PHP. In the code below, the while loop does not produce the values. The second section of code works. I can access all of the records if I use static numbers to build the name. For example, $myresults1100[id] works, and provides my 1100th result. How does one make this work with a variable? I've looked at other posts but, everyone seems to want to find the name of the variable. I know the name, I just need to build it with the right syntax. Please tell me what I am doing wrong. Thanks!

`enter code here`
<?php

$curl = curl_init();

curl_setopt_array($curl, [
CURLOPT_URL => "https://l4chsalter-alternative-me-crypto- 
v1.p.rapidapi.com/v2/listings/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"x-rapidapi-host: l4chsalter-alternative-me-crypto-v1.p.rapidapi.com",
    "x-rapidapi-key:
],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
   echo "cURL Error #:" . $err;
} else {

$cleaned = $response;
$cleaned1 = str_replace('{', "", $cleaned);
$cleaned2 = str_replace('}', "", $cleaned1);
$cleaned3 = str_replace('[', "", $cleaned2);
$cleaned4 = str_replace(']', "", $cleaned3);
$cleaned5 = str_replace('"', "", $cleaned4);
$cleaned6 = str_replace(' ', "", $cleaned5);
        
# Removing some unique stuff from the start of the response
$mysearchstring = 'data: ';

$cleaned6=preg_replace('/\s+/', '', $cleaned6);

$i=0;
#Turn the string into an array  
$last_half = explode(',', $cleaned6);
foreach($last_half as $item){
list($k, $v) = explode(':', $item);
$result[$k] = $v;  
${"myresults$i"} = $result;
${"result[name]myresults$i"} = $result; 
$i++;
}

$mine = 1;
while($mine < 4){
echo "--------------------------------------------------------</br>";
echo "The id is "; echo ${"myresults$mine"}[id]; echo "</br>";
echo "The id is ";  echo "$myresults . $mine[id]";echo "</br>";
echo "The name is ";    echo "$myresults . $mine[name]";echo "</br>";
echo "The symbol is ";  echo "$myresults . $mine[symbol]";echo "</br>";
echo "The website_slug is ";    echo "$myresults . 
mine[website_slug]";echo "</br>";
echo "-----------------------------------------------------</br>";
$mine++;
}
echo "-----------------------------------------------------</br>-";

echo "The id is " . $myresults1100[id];echo "</br>";
echo "The name is " . $myresults1100[name];echo "</br>";
echo "The symbol is " . $myresults1100[symbol];echo "</br>";
echo "The website_slug is " . $myresults1100[website_slug];echo "</br>";
echo "------------------------------------------------------</br>";
}

Solution

  • echo "id is " . ${"myresults$i"}[id];
    

    This is the syntax that worked. It allowed me to build the variable name instead of having to give it static numbers in place of the i$. It had to be moved inside the foreach loop to work. Using this I am able to print all 1420 records in the array.