Jump to content

[SOLVED] Array question


yellowepi

Recommended Posts

I'm new to php and have been reading a lot, but I have been going through arrays and have a multidemensional array. When I want to print everything to the screen this is the code I used.

 

To print the array values I used $a from $arrayvals, does php always take the first letter of the variable and use it as a variable?

 

For the key I used $k and for the values I used $v, are these set in stone variables, or can you use any variable name you want for them?

 

$arrayvals = array(
array(
	"name" => "Taylor",
	"age" => "26",
	"occupation" => "Supernurse",
	),
	array(
	"name" => "Rachel",
	"age" => "25",
	"occupation" => "Supernurse",
	)
);

echo  $arrayvals[$a>0]['name'];
foreach ($arrayvals as $a) {
	while (list($k, $v) = each ($a)) {
	echo "$k ... $v <br/>";
	}
}

 

Any help would be very much appreciated.

Link to comment
Share on other sites

I'm not sure what you are doing here.

 

The value in brackets should be the value of one of the array indexes. Instead you are doing a comparrison?

echo  $arrayvals[$a>0]['name'];

 

 

I'm new to php and have been reading a lot, but I have been going through arrays and have a multidemensional array. When I want to print everything to the screen this is the code I used.

 

To print the array values I used $a from $arrayvals, does php always take the first letter of the variable and use it as a variable?

 

No, it gets assigned to whatever variable you choose to assign it to. When using foreach you can either assign just the values to a variable or the values and the keys.

 

foreach ($arrayList as $value)

foreach ($arrayList as $key => $value)

 

Try using this code and it may be more clear:

<?php
$arrayvals = array(
array(
	"name" => "Taylor",
	"age" => "26",
	"occupation" => "Supernurse",
	),
	array(
	"name" => "Rachel",
	"age" => "25",
	"occupation" => "Supernurse",
	)
);


foreach ($arrayvals as $record) {
    foreach ($record as field => $value) {
        echo $field . ': ' . $value . '<br>';
    }
}
?>

Link to comment
Share on other sites

Okay, yours is much clearer, but wouldn't work when I pasted it in. You left off a $ for the variable $field in the foreach statement.

 

To reiterate to make sure I understand this.

 

foreach($arrayvals as $record)

  This is placing the values of arrayvals into the variable $record, right?

foreach ($record as $field => $value) {
        echo $field . ': ' . $value . '<br>';

  This foreach statement is taking each of the records in the array and assigning them to field and value?

        name, age, and occupation are assigned to the first variable- $field

        and the data for each is assigned to the $value

        and printing them to the page.

 

and for the function I used to start with

foreach ($arrayvals as $a) {
	while (list($k, $v) = each ($a)) {
	echo "$k ... $v <br/>";

The arrayvals data is placed $a.

    list takes the first thing it sees in the array which would be the $key and places it in the first variable $k and then the second variable $v in the list function is the second thing it sees and places it in $v. and repeats do to the while statement for each $a.

And prints.

 

The [a>0] was me messing around to see what would happen. It was working too. I just forgot to erase it before I posted the code.

Link to comment
Share on other sites

foreach($arrayvals as $record)

   This is placing the values of arrayvals into the variable $record, right?

It is placing the value for each element of the array into $record. However, each value is also an array of name, age, occupation - so $record is now an array too.

 

foreach ($record as $field => $value) {
        echo $field . ': ' . $value . '<br>';

   This foreach statement is taking each of the records in the array and assigning them to field and value?

         name, age, and occupation are assigned to the first variable- $field

         and the data for each is assigned to the $value

         and printing them to the page.

Correct. The Keys will be assigned to the first variable and the values to the 2nd.

 

and for the function I used to start with

foreach ($arrayvals as $a) {
	while (list($k, $v) = each ($a)) {
	echo "$k ... $v <br/>";

The arrayvals data is placed $a.

     list takes the first thing it sees in the array which would be the $key and places it in the first variable $k and then the second variable $v in the list function is the second thing it sees and places it in $v. and repeats do to the while statement for each $a.

And prints.

Well, $a would be given the value for each value in the array (so each value of $a would be an array with name, age, occupation.) Then you use list within a loop to assign values to $k & $v from each($a). The each() will return the current key/value pair from the array and move the pointer tot he next record. So, it essentially performs the same as the 2nd foreach I posted, but in a more confusing manner, IMHO.

 

The [a>0] was me messing around to see what would happen. It was working too. I just forgot to erase it before I posted the code.

Well, it would be no different than $arrayvals[0]['name']; since $a was undefined so that comparrison would always resolve to false (or 0).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.