0

Basically I would like to create a function that will in turn create a multidimensional array based on another function. Here's the format of the array I need to create (end result):

'options' => array (
        'one' => array (
            'label' => 'Option One',
            'value' => 'one'
        ),
        'two' => array (
            'label' => 'Option Two',
            'value' => 'two'
        ),
        'three' => array (
            'label' => 'Option Three',
            'value' => 'three'
        )
    )

This will need to be created by a function using another array that was created to store wordpress categories:

$categories = get_categories('hide_empty=0&orderby=name');
$wp_cats = array();
foreach ($categories as $category_list ) {
       $wp_cats[$category_list->cat_ID] = $category_list->cat_name;
}       

And here's the code I wrote to try accomplish what I've described but couldn't get it to work:

    function wcat2() {
    $i = 0;
    $categories = get_categories();
    foreach( $categories as $category ) {
        $i++;
        array (
        $i => array (
            $category->slug => $category->slug,
            $category->term_id  => $category->term_id
        ),
        );
    }
}

Any ideas of what I'm missing? Help would be much appreciated.

Update: Here's an illustration of what I need: descriptin 1

description 2

1
  • for starters you are not saving the array to any variable. Commented Dec 6, 2012 at 0:20

1 Answer 1

1

Maybe something like:

function wcat2() {
    $out = array();
    $categories = get_categories();
    foreach( $categories as $category ) {
        $out[$category->term_id] = array(
            'label' => $category->slug,
            'value' => $category->term_id
        );
    }
    return array('options'=>$out);
}
print_r(wcat2());

Create a blank variable $out, loop through the categories and on each loop append a new array in the desired format. I'm not sure which is which in regards to the slug/term_id, so you might need to switch those.

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

4 Comments

Hi Jonathan. Thanks for your help. It did work but the array didn't have all elements I need in it. I've added an image explaining which array I need and where which element should go.
I believe I did have an extra array in there. Check it now. That should match your requested output. However, like I said, under $category, I have no idea what fields there are, I was just guessing based on the code you had written. if slug/term_id are not in the right spot (or not the right property), you will need to fix them as I have no idea what slug/term_id refer to.
Also, you say the first key needs to be numbers 1,2,3 but in all the images it shows "one", "two", "three" as the key (text, not numbers).
That was it!!! I did edited out the return array('options'=>$out); and used return $out; instead which was what I needed (the 'options' is part of the other code that this function will go with). Thank you very much for your help! Much appreciated indeed!

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.