Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

last element in array

Posted on 2013-05-22
5
Medium Priority
?
388 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

715 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