0

I create an array which works fine:

$user_list_args = array(
 'role__in' => array('editor', 'author', 'administrator' ),
);
$user_list = get_users($user_list_args);
$user_list_array = array();
foreach ( $user_list as $user ) {
 $user_list_array[] = $user->user_nicename;
}

I then want to display that in an another array which is set up as follows:

array(
 'id' => 'task-users',
 'label' => 'User(s)',
 'type' => 'select',
 'options' => array(
  'tom',
  'jerry',
 ),
),

I want to change the options to be dynamic from my array as so:

private $fields = array(
 array(
  'id' => 'task-users',
  'label' => 'User(s)',
  'type' => 'select',
  'options' => array($user_list_array),
 ),
);

This generates an error:

Fatal error: Constant expression contains invalid operations

Can someone point me in the right direction? I am using PHP7 if that helps but should be usable on older versions too.

Thanks

4
  • i think you mean you want to join the arrays? not echo? Commented Dec 11, 2016 at 2:08
  • I guess? so array_join is what I need to look at? Commented Dec 11, 2016 at 2:09
  • Why are you creating the $user_list_array like that? If you want that array to contain the user's nice names, you only need to do $user_list_array[] = $user->user_nicename; Commented Dec 11, 2016 at 2:13
  • $newArray[] = print_r($oldArray,true); does this do what you need? Commented Dec 11, 2016 at 22:50

2 Answers 2

1

You can't declare class properties using variables. If you need to assign values like that to the property use a constructor method and do it there. Maybe something like this:

private $fields;

public function __construct() {
    // your code to define $user_list_array somewhere here
    // or passed to constructor when you instantiate the class

    $this->fields = array(
        array(
            'id' => 'task-users',
            'label' => 'User(s)',
            'type' => 'select',
            'options' => array($user_list_array),
        )
    );
}

http://php.net/manual/en/language.oop5.properties.php:

This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

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

Comments

0

your joining not echoing and this is completely valid:

<?php

$user_list_array=array(
  'tom',
  'jerry',
 );

$f= array(
 'id' => 'task-users',
 'label' => 'User(s)',
 'type' => 'select',
 'options' => array($user_list_array),
);

print_r($f);

output:

Array
(
    [id] => task-users
    [label] => User(s)
    [type] => select
    [options] => Array
        (
            [0] => Array
                (
                    [0] => tom
                    [1] => jerry
                )

        )

)

OR:

<?php

$user_list_array=array(
  'tom',
  'jerry',
 );

$f= array(
 'id' => 'task-users',
 'label' => 'User(s)',
 'type' => 'select',
 'options' => $user_list_array,
);

print_r($f);

output:

Array
(
    [id] => task-users
    [label] => User(s)
    [type] => select
    [options] => Array
        (
            [0] => tom
            [1] => jerry
        )

)

2 Comments

I must be missing something but can't get it too work, tried every option possible, still getting the php error.....
@GarethGillman you need to provide the exact code, or at least enough for us to replicate the error

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.