0

I am trying to create an array using foreach which is used inside an another array.

$team =  array();
foreach ($items as $item) {
  $team[] = array($item->post_title => $item->post_title, );
}
print_r($team);


$meta_boxes[] = array(
    'title'  => __( 'Team', 'meta-box' ),
    'fields' => array(
        array(
            'name'     => __( 'Select', 'meta-box' ),
            'options'  =>  $team,
        ),
    )
);

The options array should be in the format of

'options' => array(
    'value1' => ('Label1'),
    'value2' => ('Label2'),
),

The print_r output is

Array ( 
  [0] => Array ( [Jessi] => Jessi ) 
  [1] => Array ( [Adam] => Adam )
  [2] => Array ( [Babu] => Babu ) 
)

I am getting output as Array,Array,Array in selectbox for which I am trying to use this for. How do I fix this?

Thanks

3 Answers 3

1

Add Key in array [$item->post_title] and assign the values

$team =  array();
foreach ($items as $item) {
    $team[$item->post_title] = $item->post_title;
}
print_r($team);
Sign up to request clarification or add additional context in comments.

Comments

1

Try with -

foreach ($items as $item) {
  $team[$item->post_title] = $item->post_title;
}

Comments

1

You can also try like this

$team =  array();
foreach ($items as $item) {
  $team = array_merge($team,array($item->post_title => $item->post_title));
}

Comments

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.