Page 1 of 2
help with array
Posted: Sat Oct 04, 2008 4:33 am
by 3hhh
while( $row=mysql_fetch_row($sql) )
{
$seatAvailable[] = $row[0];
}
for($counter=0; $counter<$qty; $counter +=1) {
// I want to create another array and copy the array of $seatAvailable to it. For example, if $qty = 2, I will only copy $seatAvailable[0] and $seatAvailable[1] to my new array
}
Thank you in advance!
Re: help with array
Posted: Sat Oct 04, 2008 5:00 am
by onion2k
Why not use array_slice()? Eg
Code: Select all
$new_array = array_slice($seatAvailable, 0, 2);
Re: help with array
Posted: Sat Oct 04, 2008 5:06 am
by 3hhh
onion2k wrote:Why not use array_slice()? Eg
Code: Select all
$new_array = array_slice($seatAvailable, 0, 2);
Hi, I don't know why but it doesnt work

Re: help with array
Posted: Sat Oct 04, 2008 5:20 am
by onion2k
Define "doesn't work".
Re: help with array
Posted: Sat Oct 04, 2008 5:22 am
by 3hhh
onion2k wrote:Define "doesn't work".
Hi,
When I did an echo $new_array, it shows Array0
Re: help with array
Posted: Sat Oct 04, 2008 7:54 am
by onion2k
What were you expecting it to echo? It's an array.. that's what arrays do. Try print_r($new_array) instead.
Re: help with array
Posted: Sat Oct 04, 2008 7:59 am
by 3hhh
onion2k wrote:What were you expecting it to echo? It's an array.. that's what arrays do. Try print_r($new_array) instead.
Hi,
I actually want the array to print the values in it. Is there any way? Sorry for being such a newbie

Re: help with array
Posted: Sat Oct 04, 2008 8:30 am
by onion2k
You can use implode(), or foreach(), or a for loop with count(), or array_walk() ... there's loads of ways. It depends what you want to display. Try reading the array page on php.net.
Re: help with array
Posted: Mon Oct 06, 2008 2:11 pm
by matthewboh
I'm having the same problem. I have an array with one entry. When I do a print_r on the Array, I get
Array ( [1] => Array ( [ID] => 1 [TITLE] => Default School ) ) Default School
if I do a array_slice($RET, 1, 1) - nothing changes. What am I doing wrong?
Re: help with array
Posted: Mon Oct 06, 2008 3:17 pm
by onion2k
You don't have an array. You have an array with another array inside it. The inner array is in position 1 which is a little odd.. you've done something to get rid of element 0, or you've put the inner array in with a specific numeric key...
Code: Select all
$array = $RET[1];
echo $array['ID']; //echoes 1
echo $array['TITLE']; //echoes Default School
Re: help with array
Posted: Mon Oct 06, 2008 4:07 pm
by matthewboh
Thanks! I ended up using unset - did what I wanted it to do...
Re: help with array
Posted: Tue Oct 07, 2008 10:07 am
by 3hhh
3hhh wrote:onion2k wrote:What were you expecting it to echo? It's an array.. that's what arrays do. Try print_r($new_array) instead.
Hi,
I actually want the array to print the values in it. Is there any way? Sorry for being such a newbie

Just to help those who are as noob as me. This is how you print an array:
for($i=0; $i<count($yourarray); $i++) {
$value = $yourarray[$i];
echo "\n$value\n";
}?>
Re: help with array
Posted: Tue Oct 07, 2008 10:41 am
by onion2k
3hhh wrote:Just to help those who are as noob as me. This is how you print an array:
for($i=0; $i<count($yourarray); $i++) {
$value = $yourarray[$i];
echo "\n$value\n";
}?>
Doing that is actually slightly bad practise because it means count() will be called for every iteration of the loop. If your array is very big it'll slow the script down. It's faster to do this:
Code: Select all
$items = count($yourarray);
for($i=0; $i<$items; $i++) {
$value = $yourarray[$i];
echo "\n$value\n";
}
As you're not actually doing anything with the values besides echo'ing them with a couple of carriage returns between them it'd be better still to use implode..
Code: Select all
echo "\n".implode("\n\n",$yourarray)."\n";
Re: help with array
Posted: Tue Oct 07, 2008 11:07 am
by 3hhh
Thanks onion2k!
Re: help with array
Posted: Tue Oct 07, 2008 11:42 am
by RobertGonzalez
For the record, there is a neat little button above the text area where you make posts that has the word "Code" written on it. When you post code in the forums you can actually wrap it in the code tag so that it looks like code when it is posted (see how Onion's code looks?). Please make it a practice to use the code tags. You can also use
Code: Select all
or [syntax="php"] to post code.
As for viewing the contents of an array, you can always use print_r() to see what is in it. To actually put it to the screen you can use any of a collection of loop constructs. If you are talking about a numerically indexed array then a for loop, foreach loop or a while loop would work. For associative arrays you can use foreach() or you can use a combination of list() and each(). Just remember when you use list() and each() the internal array pointer will be left at the end of the array so if you want to reuse the array you will need to call reset() on it before trying to use it again.
[php]
$array = array(
'name' => 'fred',
'age' => 22,
'city' => 'Denver',
'channel' => 103.7,
);
// While, using list and each
while (list($key, $value) = each($array)) {
echo "The key is $key and its value is $value.\n";
}
// Each leaves the pointer where it left off, so reset the array
reset($array);
// No need to reset after this one since foreach makes a copy of the array
foreach ($array as $key => $value) {
echo "The key is $key and its value is $value.\n";
}
while ($value = current($array)) {
$key = key($array);
echo "The key is $key and its value is $value.\n";
next($array);
}
// Now you will need to reset the array again in order to use it.
?>