Welcome Guest, Not a member yet? Register   Sign In
SOLVED: PHP Array Help Needed.
#1

[eluser]Deep Arora[/eluser]
I have following code in my model:

Quote: if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$options[] = array(
$row->$field_key => $row->$field_value
);
}
return $options;
}

It returns array like this:

Code:
Array ( [0] => Array ( [FLAG_CHOICE] => Y ) [1] => Array ( [FLAG_CHOICE] => N ) )

In my controller, I am receiving the value returned as array above. Now, I want to form an array out of the above array as:

Code:
$options = array(
        'FLAG_CHOICE'  => 'Y',
        'FLAG_CHOICE'    => '2'
     );

which is a single dimensional array..

How do I do that? I suck at handling arrays sometimes, so need a little helping hand Sad
#2

[eluser]danmontgomery[/eluser]
Array keys must be unique, you can't have the array you're describing.
#3

[eluser]Deep Arora[/eluser]
Hmmm..then how can we use form_dropdown function?

Code:
form_dropdown('fieldname', $field_value, 'selectedvaluearray')

The $field_value expects single dimension array. And I am getting $field_value array from a table..and when we get records from table, they are returned back in 2-dimension array, yes?
#4

[eluser]mddd[/eluser]
There are multiple things going on here.

First, noctrum is correct: you can't have an array containing duplicate keys.

Second, yes the input for the form_dropdown is as simple array, like
Code:
$form_values = array(
'nl' => 'Netherlands',
'be' => 'Belgium',
'de' => 'Germany'
);

The problem here is that you are creating the two dimensional array yourself! You are adding options, but each option is an array!
Code:
$options[] = array( .... );

You should use something like
Code:
foreach ($query->result() as $row)
{
$options[ $row['your_key_value'] ] = $row['your_display_value'];
}

Then the result is a simple array like shown above.
#5

[eluser]Deep Arora[/eluser]
Worked like a charm....thanks Smile




Theme © iAndrew 2016 - Forum software by © MyBB