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

x
?
Solved

POST multi-array problem: How can I create a $_POST multi-array variable in a for loop?

Posted on 2008-06-13
2
Medium Priority
?
463 Views
Last Modified: 2010-05-18
I'm having some difficulties to create the following in a for loop. For example, the following:

if ( $c == 1 ) {
    $classEndDate =  $_POST['classDates_1']['classEndDate_1']['d'];
} elseif ( $c == 2 ) {
     $classEndDate = $_POST['classDates_2']['classEndDate_2']['d'];
} ...

and convert the above in a for loop as:

// the following does not work
for ( $x=1;$x<5;$x++ ) {
     // concatenate $classDates and append number
     $classDates = "classDates_";
     $classDatesConcatenated = $classDates.$x;
    // concatenate $classEndDate and append number
     $classEndDate = "classEndDate_";
     $classEndDateConcatenated = $classEndDate.$x;

  if ( $c == 1 ) {
    $classEndDate =  $_POST[$classDatesConcatenated][$classEndDate]['d'];
  } elseif ( $c == 2 ) {
     $classEndDate = $_POST['classDates_2']['classEndDate_2']['d'];
  } ...
}

The above for loop does not function and returns an array for $classEndDate rather than an single value.

I can concatenate the following succesfully if $_POST is not a multi array and in a for loop as:

// the following works
for ( $c; $c<5; $c++ ) {
     ...// concatenate $studentName and append number
     $varStudent = 'student_name_';
     $studentNameConc = $varStudent.$c;
    $studentName = $_POST[$studentNameConc];
}

Can someone tell me how I can fetch these multi-array $_POST variables in a for loop.
0
Comment
Question by:Victor Kimura
[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
2 Comments
 
LVL 9

Expert Comment

by:Rurne
ID: 21781145
1. What's your input form look like?

2. you're not concatenating anything in the second loop.  Each pass replaces $studentName and throws the value away (unless you're purposefully throwing it away fror the sake of example).

3.  In your for loop, you iterate over $x.  However, in your if/else block, you're referencing $c.  Where do you obtain the value of $c?
0
 

Accepted Solution

by:
Victor Kimura earned 0 total points
ID: 21800976
Hi Rurne,

Sorry, the code had typos. I wanted to change this:
  // concatenate $classEndDate and append number
     $classEndDate = "classEndDate_";
     $classEndDateConcatenated = $classEndDate.$x;

where variable $x is an incremental value of 1 in a for loop

I found out to concatenate properly even multi arrays is done like so:
$classEndDateConc = ${'classEndDate'.$x};

Thank you for your help though.
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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

722 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