CodeIgniter Forums
New to php Array question, probably VERY simple... - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: New to php Array question, probably VERY simple... (/showthread.php?tid=20016)



New to php Array question, probably VERY simple... - El Forum - 06-25-2009

[eluser]PoetaWD[/eluser]
Sorry for this very infant question.. I am just learning PHP.

I am using the form helper to create a <select> input...

The data is passed to the view file as a object...

But i am having trouble building the array to generate the <select> input !

Here is what I got:

Code:
foreach ($clientes as $cliente)
                                {
                                    $opcoesC[] = array($cliente->id => $cliente->id.' - '.$cliente->stNome);
                                }
echo form_dropdown('cliente', $opcoesC);

But that will generate a array like this:

$opcoesC[0] = 'aaaaaaaaa' => 'aaaaaaaaaaa'
$opcoesC[1] = 'bbbbbbbbb' => 'bbbbbbbbbbb'
...

or like this:

Array ( [0] => Array ( [7] => 7 - jkkk ) [1] => Array ( [8] => 8 - jk ) [2] => Array ( [9] => 9 - kkh ) [3] => Array ( [10] => 10 - klçklçklç ) )

I needed to be generated like this:

$opcoesC = array( 'aaaaaaa' => 'aaaaaaa','bbbbbbb' => 'bbbbbbbb','cccccccc'=>'ccccccc' ....);

I have also tryed this:

Code:
$opcoes = array(
                        '' => 'Selecione o funcionário',
               foreach ($funcionarios as $funcionario)
                                {
           $funcionario->id => $funcionario->id.' - '.$funcionario->stNome,
                     }
                            );

Can anyone give me a hint ?

Thanks in advance...


New to php Array question, probably VERY simple... - El Forum - 06-25-2009

[eluser]kgill[/eluser]
The reason you're getting:
Code:
$opcoesC[0] = ‘aaaaaaaaa’ => ‘aaaaaaaaaaa’
is this part here:
Code:
$opcoesC[] = array($cli...
when you use the empty brackets like that it automatically increments an integer based index. If you want to use an associative array then you have to specify the index like so:
Code:
$opcoesC[$cliente->id] = $cliente->id.' - '.$cliente->stNome;



New to php Array question, probably VERY simple... - El Forum - 06-25-2009

[eluser]Phil Sturgeon[/eluser]
Here's what kgill said implemented to your example.

Code:
foreach ($clientes as $cliente)
                                {
                                    $opcoesC[$cliente->id] = $cliente->id.' - '.$cliente->stNome;
                                }
echo form_dropdown('cliente', $opcoesC);



New to php Array question, probably VERY simple... - El Forum - 06-25-2009

[eluser]PoetaWD[/eluser]
[quote author="Phil Sturgeon" date="1245961469"]Here's what kgill said implemented to your example.

Code:
foreach ($clientes as $cliente)
                                {
                                    $opcoesC[$cliente->id] = $cliente->id.' - '.$cliente->stNome;
                                }
echo form_dropdown('cliente', $opcoesC);
[/quot

Got that !

Thank you very much for the quick help ! It is working now !

Big Grin


New to php Array question, probably VERY simple... - El Forum - 07-29-2009

[eluser]tashigiri[/eluser]
[quote author="Phil Sturgeon" date="1245961469"]Here's what kgill said implemented to your example.

Code:
foreach ($clientes as $cliente)
                                {
                                    $opcoesC[$cliente->id] = $cliente->id.' - '.$cliente->stNome;
                                }
echo form_dropdown('cliente', $opcoesC);
[/quote]
Thanks Phil and kgill..
my problem solved..^^