Sunnyisles Posted January 4, 2022 Share Posted January 4, 2022 Hi Can someone help me with this code? <?php //DOLLAR// $json_dolar=llamar_api("https://mercados.ambito.com//dolar/informal/variacion"); $dolar=$json_dolar; $compra=$dolar->compra; $venta=$dolar->venta; $variacion=$dolar->variacion; $fecha=$dolar->fecha; $var2=$dolar->class-variacion; ?> This is OK <?php echo ($compra); ?> This is Not, probably 'cause de Hyphen <?php echo ($class-variacion); ?> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/314388-help-with-json/ Share on other sites More sharing options...
gw1500se Posted January 4, 2022 Share Posted January 4, 2022 First, please use the code icon (<>) in the menu and specify PHP for your code. You don't say what error your are getting but that syntax is probably wrong. You don't show how $class is defined but as a guess given almost no information from you I'd say you want this: <?php echo ($class->variacion); ?> Quote Link to comment https://forums.phpfreaks.com/topic/314388-help-with-json/#findComment-1593170 Share on other sites More sharing options...
Sunnyisles Posted January 4, 2022 Author Share Posted January 4, 2022 PHP <> is OK What I am not getting right is this If yo go to this url https://mercados.ambito.com//dolar/informal/variacion you will see this key: class-variacion It has a Hyphen so it does not work $var=$dolar->class-variacion; <?php echo ($var); ?> Quote Link to comment https://forums.phpfreaks.com/topic/314388-help-with-json/#findComment-1593172 Share on other sites More sharing options...
Barand Posted January 4, 2022 Share Posted January 4, 2022 Of course, the correct code could equally be echo ($class - $variacion); or echo ($class_variacion); but who can say as we have no idea what the line is supposed to do. "'cause de Hyphen" doesn't really cut it as an explanation. Quote Link to comment https://forums.phpfreaks.com/topic/314388-help-with-json/#findComment-1593173 Share on other sites More sharing options...
Sunnyisles Posted January 4, 2022 Author Share Posted January 4, 2022 I just need to echo class-variacion from this url https://mercados.ambito.com//dolar/informal/variacion Thanks Quote Link to comment https://forums.phpfreaks.com/topic/314388-help-with-json/#findComment-1593174 Share on other sites More sharing options...
dodgeitorelse3 Posted January 5, 2022 Share Posted January 5, 2022 (edited) 5 hours ago, Sunnyisles said: I just need to echo class-variacion from this url https://mercados.ambito.com//dolar/informal/variacion This doesn't use your api but if page is always formatted that way then possibly <?php //DOLLAR// $json_dolar=file_get_contents("https://mercados.ambito.com//dolar/informal/variacion"); $dolar=explode("\",", substr_replace(trim($json_dolar, '{}') ,"",-1)); echo trim(explode("\":\"", $dolar[4])[1])."<br><br>"; foreach($dolar as $key => $value){ echo $value." OR ".trim(explode("\":\"", $dolar[$key])[1])."<br>"; } ?> Which displays up "compra":"202,50 OR 202,50 "venta":"206,50 OR 206,50 "fecha":"04\/01\/2022 - 16:45 OR 04\/01\/2022 - 16:45 "variacion":"0,24% OR 0,24% "class-variacion":"up OR up Edited January 5, 2022 by dodgeitorelse3 Quote Link to comment https://forums.phpfreaks.com/topic/314388-help-with-json/#findComment-1593178 Share on other sites More sharing options...
Solution Phi11W Posted January 5, 2022 Solution Share Posted January 5, 2022 15 hours ago, Sunnyisles said: $var2=$dolar->class-variacion; You can work with hyphenated attribute names, but you have to wrap them up a bit more: $var2=$dolar->{'class-variacion'}; Quote Link to comment https://forums.phpfreaks.com/topic/314388-help-with-json/#findComment-1593183 Share on other sites More sharing options...
Sunnyisles Posted January 5, 2022 Author Share Posted January 5, 2022 This is it, thanks Phi11W Quote Link to comment https://forums.phpfreaks.com/topic/314388-help-with-json/#findComment-1593184 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.