Jump to content

proper array usage


markbett

Recommended Posts

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

[code] <?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]; ?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/20953-proper-array-usage/
Share on other sites

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
[code]
<?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
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/20953-proper-array-usage/#findComment-92902
Share on other sites

Markbett,

I noticed that this post was linked to your last post "[url=http://www.phpfreaks.com/forums/index.php/topic,108250.0.html]Determine variable names for dynamic variables[/url]"

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

[code]<?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>";
}
?>[/code]

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
Link to comment
https://forums.phpfreaks.com/topic/20953-proper-array-usage/#findComment-92933
Share on other sites

im trying to base off of thise example:

[code]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
  )
) [/code]
from [url=http://www.w3schools.com/php/php_arrays.asp]http://www.w3schools.com/php/php_arrays.asp[/url] but i seem to be missing something...
Link to comment
https://forums.phpfreaks.com/topic/20953-proper-array-usage/#findComment-93151
Share on other sites

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

[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];[/code]
Link to comment
https://forums.phpfreaks.com/topic/20953-proper-array-usage/#findComment-93223
Share on other sites

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 [color=green]while[/color] loop.

So instead of [code=php:0]$guest_array = array(.....[/code]

You need [code=php:0]$guest_array[$i] = array(.....[/code]


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
Link to comment
https://forums.phpfreaks.com/topic/20953-proper-array-usage/#findComment-93478
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.