0

Found it hard to make a question title that can make sense. I'll explain better with an example:

$defaults = [
   'user_posts' => [
      'default_per_line' => 4,
      'default_per_page' => [user 'default_per_line'] * 2,
   ],
   'admin_posts' => [
      'default_per_line' => 6,
      'default_per_page' => [admin 'default_per_line'] * 2,
   ],
];

What I mean by 'user default_per_line' is using using the value already assigned as ['user_posts']['default_per_page'] and multypling it with 2 (or anything else).

Can it be done? If so how?

1
  • you mean 'default_per_page' => default_per_line(so 4)* 2 ? because in you example you did not assign any value to ['user_posts']['default_per_page'] Commented Dec 1, 2015 at 17:25

1 Answer 1

1

Pretty sure I'm right in saying no, it can't be done in a single assignment.

Only way you could do it would be to alter the array after the fact:

$defaults = [
   'user_posts' => [
      'default_per_line' => 4,
      'default_per_page' => 0,
   ],
   'admin_posts' => [
      'default_per_line' => 6,
      'default_per_page' => 0,
   ],
];
$defaults['user_posts']['default_per_page'] = $defaults['user_posts']['default_per_line'] * 4;
$defaults['admin_posts']['default_per_page'] = $defaults['admin_posts']['default_per_line'] * 4;

Which largely defeats the point of what you are trying to do.

Sign up to request clarification or add additional context in comments.

1 Comment

I wanted single assignment because this is a config file in Laravel 5 and the .php file only returns an array. I guess I will have to do some calculations and then return the resulted array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.