Solved

loading a array using a form loop

Posted on 2014-03-14
5
202 Views
Last Modified: 2014-03-26
I am trying to figure out how to load a array using a for loop.  When you print out the array using print_r it would output  this.  Can somebody show me how to load a associative array that would output this format.

$data = array(
    array('name'=>'Bar', 'lastname'=>'Baz'),
    array('name'=>'Foo', 'lastname'=>'Baz'),
);

Open in new window

0
Comment
Question by:stargateatlantis
  • 3
  • 2
5 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39930270
Uhh, where do you envision that the data would come from?  I've never seen anyone load an array with a for() loop, at least not since there are while() and foreach().

If you're new to PHP and want to find your way to some good learning resources, this article may be helpful.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0
 

Author Comment

by:stargateatlantis
ID: 39930308
I just need a example of a for loop that will load the array with those values
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39930335
But nobody would write a for() loop to do that.  The simple assignment statement you showed with the question creates an array.  It is a multidimensional array.  The "sub-arrays" are associative arrays (with named keys).  The outer array is a numerically indexed array with numeric keys.  Here is how to look at the data with print_r():
http://www.iconoun.com/demo/temp_stargateatlantis.php
<?php // demo/temp_stargateatlantis.php
error_reporting(E_ALL);

// FROM THE POST AT EE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28388972.html

$data = array(
    array('name'=>'Bar', 'lastname'=>'Baz'),
    array('name'=>'Foo', 'lastname'=>'Baz'),
);

// SHOW WHAT THE TWO-DIMENSIONAL ARRAY STRUCTURE LOOKS LIKE
echo '<pre>';
print_r($data);

Open in new window

0
 

Author Comment

by:stargateatlantis
ID: 39930383
so let's reverse this.  How do we load the associative array.  from a php for loop the. output the data


$data = array(
    array('name'=>'Bar', 'lastname'=>'Baz'),
    array('name'=>'Foo', 'lastname'=>'Baz'),
);

Open in new window

0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
ID: 39930581
I just don't get why you would want to be using a for() loop.  Is this a schoolwork assignment?  It doesn't fit in the same way that a fish does not use a bicycle.  Maybe I am missing what you mean by "load the array?"  Arrays are created by PHP assignment operators.  The for() statement is a loop control structure.  It does not make assignments.

Does something like this make sense to you?

<?php // demo/temp_stargateatlantis.php
error_reporting(E_ALL);

// FROM THE POST AT EE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28388972.html

$data = array(
    array('name'=>'Bar', 'lastname'=>'Baz'),
    array('name'=>'Foo', 'lastname'=>'Baz'),
);

// SHOW WHAT THE TWO-DIMENSIONAL ARRAY STRUCTURE LOOKS LIKE
echo '<pre>';
print_r($data);

foreach ($data as $ptr => $sub_array)
{
    echo PHP_EOL . "ARRAY POSITION $ptr";
    foreach ($sub_array as $key => $value)
    {
        echo PHP_EOL . $key . ' => ' . $value;
    }
    echo PHP_EOL;
}

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

744 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