1

How do I take an array like this:

Array 
( 
    [0] => Array 
    ( 
        [id] => 1 
        [BillGroup] => Group A 
    ) 
    [1] => Array 
    ( 
        [id] => 2 
        [BillGroup] => Group B 
    ) 
)

and dynamically turn it into an array like this:

$params = array('1' => 'Group A', '2' => 'Group B')

Where '1' and '2' is from [id] and 'Group 1' and 'Group 2' is from [BillGroup].

I need to be able to pass $params into Codeigniter's form_dropdown() helper function as the 2nd parameter to supply the dropdown options

1
  • Sometimes the solution to a question is even in a user profile, hint, hint (@abracadaver) Commented Mar 24, 2016 at 0:00

2 Answers 2

2

You can use array_column:

$params = array_column ( $array , 'BillGroup', 'id' );

Result:

Array
(
    [1] => Group A 
    [2] => Group B 
)

array_combine is available on PHP >= 5.5.0. On previous version you can use array_map:

$params = array();
array_map
(
    function ($row) use (&$params) 
    {
        $params[ $row['id'] ] = $row['BillGroup'];
    }
    ,$array
);

Note that we need to pass $params by reference due to variable scope inside anonymous function.


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

4 Comments

Why array_combine()? Totally not needed.
@Rizier123 to have 'id' values as keys
I think looking at the manual page for array_column() would be worth it, hint
@Rizier123 Ok ok ok... I've forgotten and edited. Thank you
0
<?php
print "<pre>";
echo "Your Array:<br>";
    $array=array(

                array('id'=>1 , 'billgroup'=>'Group A'),
                array('id'=>2 , 'billgroup'=>'Group B'),
                array('id'=>3 , 'billgroup'=>'Group C')

        );

print_r($array);


function recursive($array, $level = 1){

    global $temp_data;

    foreach($array as $key => $value){
        //If $value is an array.
        if(is_array($value)){
            //We need to loop through it.
            recursive($value, $level + 1);
        } else{

            //It is not an array, so print it out.
            $temp_data[] = $value;                                      
            // echo $key . ": " . $value, '<br>';
        }
    }

    return $temp_data;
}

recursive($array);
// Converted Array
echo "Converted Array:<br>";

print_r($temp_data);

$i=0;$j=1;$len = count($temp_data)/2;

// Converting Normal Array into Associative
echo "Normal Array to Associative Array:<br>";
for($l=1;$l<=$len;$l++){

    $output[$temp_data[$i]]= $temp_data[$j];
    $i=$i+2; $j=$j+2;
}
print_r($output);

?>

OUTPUT:

Your Array:
Array
(
    [0] => Array
        (
            [id] => 1
            [billgroup] => Group A
        )

    [1] => Array
        (
            [id] => 2
            [billgroup] => Group B
        )

    [2] => Array
        (
            [id] => 3
            [billgroup] => Group C
        )

)
Converted Array:
Array
(
    [0] => 1
    [1] => Group A
    [2] => 2
    [3] => Group B
    [4] => 3
    [5] => Group C
)
Normal Array to Associative Array:
Array
(
    [1] => Group A
    [2] => Group B
    [3] => Group C
)

1 Comment

This is for all kind of multidimensional 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.