Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

last element in array

Posted on 2013-05-22
5
Medium Priority
?
396 Views
Last Modified: 2013-05-22
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
0
Comment
Question by:tonelm54
  • 4
5 Comments
 
LVL 6

Expert Comment

by:mickey159
ID: 39187940
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.
0
 

Author Comment

by:tonelm54
ID: 39188009
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

0
 
LVL 6

Expert Comment

by:mickey159
ID: 39188111
Can you please give me some sample output?
0
 
LVL 6

Accepted Solution

by:
mickey159 earned 2000 total points
ID: 39188152
Perhaps try 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);

//code to list the array

foreach ($arr['parameters'] as $value) {
        echo $value['field']."\n";
        echo $value['type']."\n";
        echo $value['value']."\n";
        echo $value['post']."\n";
}

//My sample output

code
s
DEU
txtCode
language
s
Bavarian
txtLanguage
official
s
F
txtOfficial
percent
d
11.2
txtPercent


I hope this is helpful.
BTW it is hard to maintain the code if you make an array that way. Try separate into a few lines.
0
 
LVL 6

Expert Comment

by:mickey159
ID: 39188157
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!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question