Sign up ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have an array that I have JSON decoded from a JSON object. Below is my array I am using for select options. The problem is each select options has a option group with a number (0-11). How can I convert this array to one that does not have these numbers in front of it?

Here is my array:

    array(21) { [0]=> array(1) { [198413]=> string(23) "option1" } [1]=> array(1) { [247598]=> string(24) "option2" } [2]=> array(1) { [195666]=> string(17) "option3" } [3]=> array(1) { [195632]=> string(22) "option..." } [4]=> array(1) { [243763]=> string(24) "option..." } [5]=> array(1) { [196600]=> string(13) "option..." } [6]=> array(1) { [196597]=> string(11) "option..." } [7]=> array(1) { [196592]=> string(16) "option..." } [8]=> array(1) { [196549]=> string(17) "option..." } [9]=> array(1) { [251324]=> string(10) "option..." } [10]=> array(1) { [195564]=> string(50) "option..." } } 

Here is my code:

      $result = drupal_http_request('https://'.$key.'@'.$company.'.example.com/example.json');
    $feed_array = json_decode($result->data, TRUE);
    $projects = array();
    foreach ($feed_array['todo-lists'] as $project) {
    $projects[] = array(
    $project['id'] => $project['name']
    );
    }  
share|improve this question

1 Answer 1

The easiest way to do it would be to iterate through the array

$options = array();
foreach($project as $option_group)
{
   $options[key($option_group)] = next($option_group);
}

Or, if you can't count on there being only one element in each option group, do:

$options = array();
foreach($project as $option_group)
{
   $options = array_merge($options, $option_group);
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.