Jump to content


Photo

proper array usage


  • Please log in to reply
6 replies to this topic

#1 markbett

markbett
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 16 September 2006 - 07:18 AM

im trying to make sure i am building my array correctly and so far its not working right... im not great with arrays so i was hoping someone might be able to point out my mistake

<?php	$i='1';
		$guests=3;
		$guest_name1="test";
		$guest_email1="tesingt@blah";
		$guestnum3="tessafasfd sdft";
		//$guests=mysql_real_escape_string($_POST['guests']);
		while($i<=$guests){
			$guest_array = array($i=>array(${guest_name.$i}, ${guest_email.$i}));
			$i++;
			}
	echo $guest_array[1][0]; ?>


#2 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 16 September 2006 - 07:59 AM

I had to double check this, I am right

When calling an array, you only want 1 number
You have
echo $guest_array[1][0];

As an example
<?php
$myarray = array(1 => "Jamie",
				2 => "Amber",
				3 => "Wayne",
				4 => "Oliver",
				5 => "Mark");
				

echo $myarray[1][0];
//This will show J to the browser
//It gets from the array where the key is 1 [1] then it takes the 0 value from that [0]
// Remember in PHP 0 is the first number

echo $myarray[1];
//This will show Jamie to the browser
//Its findinig from $myarray the value of where the key is 1 
?>

Tell me the problem, I will try tell you the solution

#3 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 16 September 2006 - 10:23 AM

Markbett,

I noticed that this post was linked to your last post "Determine variable names for dynamic variables"

I think that the code below is probably quite a good solution to what you're after.  Give it a whirl.

<?php

// Get data from URL ... You could use $fields = $_GET (They do the same thing)
foreach ($_GET as $key => $value){
	$fields[$key] = $value;
}

// Import all of the array keys into the namespace
extract($fields, EXTR_OVERWRITE);

// Get the total number of guests
$totalguests = count($fields)/2;

// Populate array
$i = 1;
while ($i <= $totalguests){
	$name = ${guestname.$i}; // Join the variable for name
	$email = ${guestemail.$i}; // Join the variable for email
	
	// The next line puts the values into a multi-dimensional array keyed on $i
	$guests[$i] = array('name' => $name , 'email' => $email);
	$i++;
}

// Output everything to show it's all worked ok.
foreach ($guests as $key => $value){
	echo "<b>Guest: $key<br>Name: {$guests[$key]['name']}<br> Email: {$guests[$key]['email']}<br><br></b>";
}
?>

I liked the look of your post so I had a crack at it.  You can fit all your error checking in around it.

Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#4 markbett

markbett
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 16 September 2006 - 06:04 PM

well the reason i have it twice is cause i think i am properly setting up a multidimensional array.... (aray withing an array)

#5 markbett

markbett
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 16 September 2006 - 06:56 PM

im trying to base off of thise example:

Example

In this example we create a multidimensional array, with automatically assigned ID keys:

$families = array
(
  "Griffin"=>array
  (
  "Peter",
  "Lois",
  "Megan",
  ),
  "Quagmire"=>array
  (
  "Glenn"
  ),
  "Brown"=>array
  (
  "Cleveland",
  "Loretta",
  "Junior"
  )
);
The array above would look like this if written to the output:

Array
(
[Griffin] => Array
  (
  [0] => Peter
  [1] => Lois
  [2] => Megan
  )
[Quagmire] => Array
  (
  [0] => Glenn
  )
[Brown] => Array
  (
  [0] => Cleveland
  [1] => Loretta
  [2] => Junior
  )
)
from http://www.w3schools.../php_arrays.asp but i seem to be missing something...

#6 markbett

markbett
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 16 September 2006 - 09:50 PM

this still isn't woking for me... right now i am trying the code

$i='1';
		$guests=3;
		$guest_name1="test";
		$guest_email1="tesingt@blah";
		$guestnum3="tessafasfd sdft";
		//$guests=mysql_real_escape_string($_POST['guests']);
		while($i<=$guests){
			$guest_array = array("$i"=>array("${guest_name.$i}", "${guest_email.$i}"));
			$i++;
			}
	echo $guest_array[1][0];


#7 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 17 September 2006 - 01:54 PM

I think the thing you're missing is that in the example, they're defining their entire array, you're not.  You're adding to yours on each iteration of your while loop.

So instead of
$guest_array = array(.....

You need
$guest_array[$i] = array(.....


As in my example above.

If in each iteration of the loop you use the code you have, you're not adding to it, you're redefining it.

I hope that helps.

Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users