Link to home
Start Free TrialLog in
Avatar of tonelm54
tonelm54

asked on

last element in array

Im trying to build by own binder for binding to mysql (long story.....)

So I want to pass the data via an array:-
    $arr = array('sqlCode'=>"INSERT INTO CountryLanguage (`code`,`language`,`official`,`percent`) VALUES (?, ?, ?, ?)",
        'parameter'=>array('field'=>'code', 'type'=>'s', 'value'=>'DEU', 'post'=>'txtCode' ),
        'parameter'=>array('field'=>'language', 'type'=>'s', 'value'=>'Bavarian', 'post'=>'txtLanguage' ),
        'parameter'=>array('field'=>'official', 'type'=>'s', 'value'=>'F', 'post'=>'txtOfficial' ),
        'parameter'=>array('field'=>'percent', 'type'=>'d', 'value'=>'11.2', 'post'=>'txtPercent' ));

Open in new window


But when I look at the array only the last element of parameter is stored:-


Array
(
    [sqlCode] => INSERT INTO CountryLanguage (`code`,`language`,`official`,`percent`) VALUES (?, ?, ?, ?)
    [parameter] => Array
        (
            [field] => percent
            [type] => d
            [value] => 11.2
            [post] => txtPercent
        )

)

What I want to be able to do is list all the parameters, like:-
    foreach ($arr['parameter'] as $value) {
        echo $value['field'];
    }

Open in new window


I understand that the each parameter is overwriting its previous, but would like to know how I can get around this, so I can this working.

Thank you
Avatar of mickey159
mickey159
Flag of United States of America image

You have used the same key for the last few elements of the array. In this case, the values will overlap each other, therefore only the last value set is stored.
Therefore you should use different names, such as parameter1, parameter2, etc.

If you have any further questions, feel free to ask me.
Avatar of tonelm54
tonelm54

ASKER

Is there anyway to call them the same?

What I want to do is list the child array elements inside the elements with the
    foreach ($arr['parameter'] as $value) {
        echo $value['field'];
    }

Open in new window

Can you please give me some sample output?
ASKER CERTIFIED SOLUTION
Avatar of mickey159
mickey159
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To make it simpler read this:

$arr=array('sqlCode'=>"INSERT INTO CountryLanguage (`code`,`language`,`official`,`percent`) VALUES (?, ?, ?, ?)");
$p1=array('field'=>'code', 'type'=>'s', 'value'=>'DEU', 'post'=>'txtCode' );
$p2=array('field'=>'language', 'type'=>'s', 'value'=>'Bavarian', 'post'=>'txtLanguage' );
$p3=array('field'=>'official', 'type'=>'s', 'value'=>'F', 'post'=>'txtOfficial' );
$p4=array('field'=>'percent', 'type'=>'d', 'value'=>'11.2', 'post'=>'txtPercent' );
$arr['parameters']=array($p1,$p2,$p3,$p4);

Open in new window


Then you can use your foreach() on the array. Hooray!