Guest Posted November 3, 2020 Share Posted November 3, 2020 Hello I wanted to query something from an API, but now I have 2 times money see example below. How can I only output one of them I have already looked for a solution but cannot find any or I am looking wrong { "result": [ { "Money": 45674569.88764 }, { "Money": 546.6544356456 } ], "test": 0 } <?php $myData = file_get_contents(""); $myObject = json_decode($myData); $myObjectMap = $myObject->result; ?> <?php foreach($myObjectMap as $key => $item): ?> <?PHP echo $item->Money; ?> Quote Link to comment https://forums.phpfreaks.com/topic/311672-help-with-file_get_contents/ Share on other sites More sharing options...
kicken Posted November 3, 2020 Share Posted November 3, 2020 28 minutes ago, Endrick said: How can I only output one of them Which one? Why? Quote Link to comment https://forums.phpfreaks.com/topic/311672-help-with-file_get_contents/#findComment-1582217 Share on other sites More sharing options...
Guest Posted November 3, 2020 Share Posted November 3, 2020 37 minutes ago, kicken said: Which one? Why? i would like to spend the first one so "Money": 45674569.88764 Quote Link to comment https://forums.phpfreaks.com/topic/311672-help-with-file_get_contents/#findComment-1582218 Share on other sites More sharing options...
requinix Posted November 3, 2020 Share Posted November 3, 2020 You can use array_rand() to pick one of them randomly. Quote Link to comment https://forums.phpfreaks.com/topic/311672-help-with-file_get_contents/#findComment-1582219 Share on other sites More sharing options...
Guest Posted November 4, 2020 Share Posted November 4, 2020 (edited) Is there a possibility to display only "Money": 45674569.88764? Quote You can use array_rand() to pick one of them randomly. @requinix Edited November 4, 2020 by Guest Quote Link to comment https://forums.phpfreaks.com/topic/311672-help-with-file_get_contents/#findComment-1582228 Share on other sites More sharing options...
kicken Posted November 4, 2020 Share Posted November 4, 2020 $myObject->result That is a simple array. The first element in a simple array is at index zero, so just display index zero if you want the first element. var_dump($myObject->result[0]); Quote Link to comment https://forums.phpfreaks.com/topic/311672-help-with-file_get_contents/#findComment-1582234 Share on other sites More sharing options...
Guest Posted November 5, 2020 Share Posted November 5, 2020 $myObjectMap = ($myObject->result[0]); ? Quote Link to comment https://forums.phpfreaks.com/topic/311672-help-with-file_get_contents/#findComment-1582244 Share on other sites More sharing options...
Solution Guest Posted November 6, 2020 Solution Share Posted November 6, 2020 I have found a solution <?php $firstObj = $myObjectMap[0]; echo $firstObj->Money; ?> Quote Link to comment https://forums.phpfreaks.com/topic/311672-help-with-file_get_contents/#findComment-1582252 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.