echo specific information from a POSTED array

I have a form with input fields as follows:
<input name="fp[Title 1][]" type="text" id="test"  value=""/>
<input name="fp[Title 2][]" type="text" id="test"  value=""/>

I would like to output the form input like so:
<?php echo $fp[0][0]." ".$fp[0][1]; ?>
<?php echo $fp[1][0]." ".$fp[1][1]; ?>

I had though that the code might look something like this however this does not work.
<?php echo $_POST['fp'][0][0]." ".$fp[0][1]; ?>
<?php echo ['fp'][1][0]." ".$fp[1][1]; ?>
Can anyone show me best practice?
Cheers,
N
LVL 5
Neil_BradleyWeb UX/UI DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marco GasiFreelancerCommented:
A question first: in your code I see

<input name="fp[Title 1][]"

Title 1 hasn't quotes, so it seems to be a constant, but what is its value? To be sure on how your fields are posted by your form, print the $_POST array content

if (isset($_POST['submit'])){

echo "<pre>"; // more readable output
var_dump($_POST); //print out the whole content of the $_POST array

Here you'll see the exact names of your fields. If you need help about, post here the output of the var_dump so I'll can help you better

Cheers
0
Ray PaseurCommented:
Agree with marqusG: var-dump() is  your friend.  But use it unconditionally.  In other words, do not test anything before calling it.  Just call it this way...
<?php // action script
error_reporting(E_ALL);
session_start(); // if needed
echo "<pre>";
var_dump($_POST);
echo "</pre>";
// REST OF PAGE GOES HERE

Open in new window

0
Ray PaseurCommented:
Also, you may want to learn about the nature of the "id" field in HTML.  I do not think you want to have two id's that are the same.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Ray PaseurCommented:
Install this and run it, or just test on my server, here.  Note there is no use of the id= attribute!
http://www.laprbass.com/RAY_temp_neil_bradley.php
<?php // RAY_temp_neil_bradley.php
error_reporting(E_ALL);

// SHOW THE POSTED FORM
echo "<pre>";
var_dump($_POST);

// SHOW HOW TO USE THE CONTENTS OF THE POSTED FORM
foreach ($_POST["fp"] as $key => $sub_array)
{
    foreach ($sub_array as $sub_sub_array)
    {
        echo PHP_EOL . $key . ' POINTS TO A SUB-SUB-ARRAY CONTAINING ';
        var_dump($sub_sub_array);
    }
}

// CREATE THE FORM USING THE INPUTS FROM THE POST AT EE
$form = <<<FORM
<form method="post">
<input name="fp[Title 1][]" type="text" id="test"  value=""/>
<input name="fp[Title 2][]" type="text" id="test"  value=""/>
<input type="submit" value="GO" />
</form>
FORM;

echo $form;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Neil_BradleyWeb UX/UI DeveloperAuthor Commented:
Thank you for you comments. I have re written the code to better illustrate my question
<?php error_reporting(E_ALL);
		 if (isset($_POST['fp'])){
$arrValues = array();
foreach( $_POST['fp'] as $list_id => $values ){
  foreach( $values as $k => $v ){
    $arrValues[] = $list_id.', ' .$v;
	echo '<li>'. $list_id .': '.$v.'</li><br>';
  }
}
		 }

?>
<form action='array.php' method='post' enctype="multipart/form-data">
<label for="fullName">title 1</label><input name="fp[Title 1 is][]" type="text" id="1"  value=""/><br />
<label for="email">title 2</label><input type="text" name="fp[Title 2 is][]" id="2"  value=""/><br />
<label for="email">title 3</label><input type="text" name="fp[Title 3 is][]" id="3"  value=""/><br />
<input type="submit" value="Register" id="submit" />
</form>

Open in new window

.

If you run this code (live view - http://www.beaconhilldesign.co.nz/sandbox/array.php) the entire contents of the array is successfully outputted using foreach.
My question is: If I wanted to output a specific part of the array as opposed to all of it , then how would this be achieved?
For example, if I wanted to output only the second array item (<?php echo $fp[0][0]." : ".$fp[0][1]; ?>) as opposed to listing the entire contents of the array what is the best way to achieve this?
Cheers,
N
0
Marco GasiFreelancerCommented:
Well, you can do this;

echo $_POST['fp']['Title 1 is'][0];
echo $_POST['fp']['Title 2 is'][0];
echo $_POST['fp']['Title 3 is'][0];

Cheers
0
Marco GasiFreelancerCommented:
Let me say that I used

echo "<pre>";
var_dump($_POST);

If you do it, as me and Ray said, you see how info are formatted within the $_POST array:

array(1) {
  ["fp"]=>
  array(3) {
    ["Title 1 is"]=>
    array(1) {
      [0]=>
      string(4) "sasa"
    }
    ["Title 2 is"]=>
    array(1) {
      [0]=>
      string(4) "dada"
    }
    ["Title 3 is"]=>
    array(1) {
      [0]=>
      string(4) "caca"
    }
  }
}

This output leads you to the right way to output specific elements.
I said this for two reason: first, to allow you to understand better what we meant suggesting to use var_dump; second, to help you to correctly split points (if answer will be a good solution for you)

Cheers
0
Neil_BradleyWeb UX/UI DeveloperAuthor Commented:
Both yours and Rays comments are helping me to not only answer this question but also to comprehend the working of an array..
echo $_POST['fp']['Title 1 is'][0]; is close to answering  my question.

The above code will work to echo the value of the 0 however it relies on adding 'Title 1 is' in the square brackets before it.  I also want to echo the value of 'Title 1 is' without actually writing 'title 1 is' (as is show above) how would this be done? In the foreach code I assign the value of the field name to $list and output it that way..

My reasons for needing  to pass along the "title 1 is" text is that this will be a variable so I cant always bank of that text being the same.
Updated code here:

Open in new window

<?php error_reporting(E_ALL);
		 if (isset($_POST['fp'])){
$arrValues = array();
foreach( $_POST['fp'] as $list_id => $values ){
  foreach( $values as $k => $v ){
    $arrValues[] = $list_id.', ' .$v;
	echo '<li>'. $list_id .': '.$v.'</li><br>';
  }
} echo "The name and value of the form input field is:";
// this should echo Title 1 is : the value of the fleid

echo "<pre>";
var_dump($_POST);
		 }

?>
<form action='array.php' method='post' enctype="multipart/form-data">
<label for="fullName">title 1</label><input name="fp[Title 1 is][]" type="text" id="1"  value=""/><br />
<label for="email">title 2</label><input type="text" name="fp[Title 2 is][]" id="2"  value=""/><br />
<label for="email">title 3</label><input type="text" name="fp[Title 3 is][]" id="3"  value=""/><br />
<input type="submit" value="Register" id="submit" />
</form>

Open in new window

.
Cheers,
N
0
Marco GasiFreelancerCommented:
The problem is that you're using a variable value as an array index. To gain direct access an element of an array whose index is not numeric but a variable string you must to know what is the value. Otherwise you have to iterate through the array - as you do - to retrieve all elements.
Summarizing, AFAIK you can't access directly an element of an array without referring to its index: here my knowledge stops but Ray's one goes far and maybe he can teach something about this.

@Ray: I'm reading your articles about registration and login: they're really wonderful! I'm trying to make a login system oop and maybe I'll post some question here at EE: hope you'll wnat help me, eventually.

Cheers
0
Neil_BradleyWeb UX/UI DeveloperAuthor Commented:
Big thanks you to both Ray and marqusG for participating in my question..
marqusG, your last post has helped to clarify what was before missing from my comprehension of multi dimensional arrays. Ray, I will be sure to use the info from your second post to help properly understand the anatomy of an array.
Thanks again.
Neil
0
Ray PaseurCommented:
Thanks for the points.  Glad we could help -- it's a great question! ~Ray
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.