Solved

loading a array using a form loop

Posted on 2014-03-14
5
206 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 109

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 109

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 109

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

773 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