fatkatie Posted February 10, 2017 Share Posted February 10, 2017 Is it possible to do something like this? $pt_overall = array('A' => 5, 'B' => 4, 'C' => 3, 'D' => 2, 'E' => 1)[$overall] || 0; Thanks Quote Link to comment Share on other sites More sharing options...
ginerjm Posted February 10, 2017 Share Posted February 10, 2017 What are you trying to ask? What kind of structure are you trying to create? Quote Link to comment Share on other sites More sharing options...
fatkatie Posted February 10, 2017 Author Share Posted February 10, 2017 I'm retrieving a number from an assoc array, indexed by [$overall]. The array is 'created' and used on the spot; assigned to no token. Just used and forgotten. Can I do this in php? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted February 10, 2017 Share Posted February 10, 2017 I don't recognize this at all. You say "an assoc array indexed by $overall. Isn't the assoc array already indexed by 'A', 'B', 'C', 'D' & 'E'? Quote Link to comment Share on other sites More sharing options...
fatkatie Posted February 10, 2017 Author Share Posted February 10, 2017 It's being indexed by... (an assoc. array with keys A, B ....) Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted February 10, 2017 Share Posted February 10, 2017 Can I do this in php? This may sound crazy to you, but have you considered trying it? A more interesting question would be: Is this good programming? I don't think so. Strange arrays with magic numbers coming out of nowhere are usually the last thing you want. A properly named variable or constant, possibly even with inline documentation is far bette than those code-golf-style one liners. Your use of the boolean || operator doesn't make sense either. I guess you want a shorthand for a default value. PHP 7 has the null coalescing operator: $pt_overall = $a_meaningful_array_name[$overall] ?? 0; Otherwise you'll have to use the ternary operator: $pt_overall = isset($a_meaningful_array_name[$overall]) $a_meaningful_array_name[$overall] : 0; Quote Link to comment Share on other sites More sharing options...
ginerjm Posted February 10, 2017 Share Posted February 10, 2017 When I test this line: $overall = 0; $pt_overall = array('A' => 5, 'B' => 4, 'C' => 3, 'D' => 2, 'E' => 1)[$overall]; I get 'undefined index 0.....' message. IF you did assign this construct to a var, can you show me what that var would look like? This whole thing is so foreign to me I am surprised PHP even tries to interpret it. Quote Link to comment Share on other sites More sharing options...
Solution kicken Posted February 10, 2017 Solution Share Posted February 10, 2017 array('A' => 5, 'B' => 4, 'C' => 3, 'D' => 2, 'E' => 1)[$overall]That works fine for PHP >=5.5. Adding || 0 at the end would give you a boolean result, which I don't think is what you are intending. I'm guessing you want the javascript behavior where $pt_overall would be assigned either the value of the array or 0 if undefined. || does not work that way for PHP, you always get a boolean true or false instead. If you want to lookup a value or default to 0, then you'll need to use as ternary statement, and you need to use isset() to avoid undefined index errors. You'll want to just assign your array to a variable in that case. $values = ['A' => 5, 'B' => 4, 'C' => 3, 'D' => 2, 'E' => 1]; $pt_overall = isset($values[$overall])?$values[$overall]:0; 1 Quote Link to comment Share on other sites More sharing options...
fatkatie Posted February 10, 2017 Author Share Posted February 10, 2017 Thanks kicken. I thought it would, but I have an earlier php version and it does not. I wonder if could do a ... || int(0). Quote Link to comment Share on other sites More sharing options...
ginerjm Posted February 10, 2017 Share Posted February 10, 2017 array('A' => 5, 'B' => 4, 'C' => 3, 'D' => 2, 'E' => 1)[$overall] Help an ignorant soul to visualize this construct. would it look like an array of the 5 indexed elements themselves under an index of $overall? Or would $overall be an element under each of the 5 elements? Or something else? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted February 10, 2017 Share Posted February 10, 2017 (edited) I wonder if could do a ... || int(0). If you read the other replies, you can. Edited February 10, 2017 by Jacques1 1 Quote Link to comment Share on other sites More sharing options...
kicken Posted February 10, 2017 Share Posted February 10, 2017 Help an ignorant soul to visualize this construct. It's the same as doing $values = array('A' => 5, 'B' => 4, 'C' => 3, 'D' => 2, 'E' => 1); $values[$overall]; You define an array then immediately index into it. It can be useful if you need to map one value to another but don't need the map in more than one place. function getWeekdayName($day){ return ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'][$day]; } Quote Link to comment Share on other sites More sharing options...
ginerjm Posted February 10, 2017 Share Posted February 10, 2017 Doh! Thank you for that slap upside my head. Quote Link to comment Share on other sites More sharing options...
fatkatie Posted February 10, 2017 Author Share Posted February 10, 2017 ?? - null coalescing operator - Thanks. Learn something every day. 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.