Solved

last element in array

Posted on 2013-05-22
5
378 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 500 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

630 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