Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

loading a array using a form loop

Posted on 2014-03-14
5
Medium Priority
?
225 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
[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
  • 3
  • 2
5 Comments
 
LVL 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.
Suggested Courses

636 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