JonnyEnglish Posted March 3, 2017 Share Posted March 3, 2017 Hi guys, How can I modify the code below to only echo $val where "num_owned" is greater than 0? Unfortunately, “if (empty($val))” echos the values where “"is_locked": true” is present as one which is something I’d like to avoid. json data: { "user_cards": { "1": { "num_owned": "9", "num_used": "0" }, "2": { "num_owned": "0", "num_used": "0" }, "3": { "num_owned": "0", "num_used": "0", "is_locked": true }, "4": { "num_owned": "8", "num_used": "0" }, "5": { "num_owned": "11", "num_used": "0" } } } PHP script <?php error_reporting(E_ALL|E_STRICT); ini_set('display_errors', true); include_once('db.txt'); $json = file_get_contents("TU.json"); $json = json_decode($json, true); foreach ($json['user_cards'] as $row => $v) { foreach ($v as $key => $val){ if (empty($val)) {continue;} echo $d[$row][0] . "-" . $d[$row][1] . "(" . $val . ")" . PHP_EOL; echo "<br>"; } } ?> Quote Link to comment Share on other sites More sharing options...
Solution requinix Posted March 3, 2017 Solution Share Posted March 3, 2017 Seems you're a little confused here. $row is the keys in the user_cards object ("1", "2", etc.), $v is the corresponding object; $key is "num_owned/num_used/is_locked", and $val is the corresponding value. So looking at $val doesn't make sense. Get rid of the inner foreach loop. It gets you $key and $val and those are worthless. Instead, decide what to do with $v if $v['num_owned'] > 0. Quote Link to comment Share on other sites More sharing options...
JonnyEnglish Posted March 3, 2017 Author Share Posted March 3, 2017 Hi, thanks for taking the time to reply – I appreciate it but I’m still confused so I thought it best to describe what I’m after in full as I think it’ll be more beneficial. I want to produce an output that looks like this: Infantry-1(9) Bazooka Marine-1( Medic-1(11) But the code above produces : Infantry-1(9) Infantry-3(1) Bazooka Marine-1( Medic-1(11) I don’t want to display “Infantry-3(1)” because the value in "num_owned” is 0. I’m substituting the keys in the user_cards object ("1", "2", etc.) with the values in db.txt and then appending the "num_owned” value in brackets. db.txt <?php $d[1] = array("Infantry",1); $d[2] = array("Infantry",2); $d[3] = array("Infantry",3); $d[4] = array("Bazooka Marine",1); $d[350] = array("Bazooka Marine",2); $d[351] = array("Bazooka Marine",3); $d[5] = array("Medic",1); ?> Do I still need to get rid of the inner for loop? Quote Link to comment Share on other sites More sharing options...
requinix Posted March 3, 2017 Share Posted March 3, 2017 Yes. Yes you do. Did you understand what I said? Do you know what those two loops are doing? I'm confused too because if you knew what that inner foreach did then I'd expect you would understand why you need to remove it. Try what I said, and if it still doesn't work then post your code and explain what's happening. Quote Link to comment Share on other sites More sharing options...
JonnyEnglish Posted March 3, 2017 Author Share Posted March 3, 2017 Thanks for your patience. Your solution worked I was just being stupid. Quote Link to comment 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.