Solved

loading a array using a form loop

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

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 110

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 110

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
The viewer will learn how to dynamically set the form action using jQuery.

688 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