Solved

loading a array using a form loop

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
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.

867 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

16 Experts available now in Live!

Get 1:1 Help Now