Solved

last element in array

Posted on 2013-05-22
5
346 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
Comment Utility
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
Comment Utility
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
Comment Utility
Can you please give me some sample output?
0
 
LVL 6

Accepted Solution

by:
mickey159 earned 500 total points
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now