Jump to content

"anonymous" array?


Go to solution Solved by kicken,

Recommended Posts

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;
Link to comment
https://forums.phpfreaks.com/topic/303155-anonymous-array/#findComment-1542561
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/303155-anonymous-array/#findComment-1542562
Share on other sites

  • Solution

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;
  • Like 1
Link to comment
https://forums.phpfreaks.com/topic/303155-anonymous-array/#findComment-1542563
Share on other sites

 

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?

Link to comment
https://forums.phpfreaks.com/topic/303155-anonymous-array/#findComment-1542566
Share on other sites

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];
}
Link to comment
https://forums.phpfreaks.com/topic/303155-anonymous-array/#findComment-1542568
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.