help with array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

3hhh
Forum Newbie
Posts: 22
Joined: Wed Sep 10, 2008 12:02 pm

help with array

Post 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!
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: help with array

Post by onion2k »

Why not use array_slice()? Eg

Code: Select all

$new_array = array_slice($seatAvailable, 0, 2);
3hhh
Forum Newbie
Posts: 22
Joined: Wed Sep 10, 2008 12:02 pm

Re: help with array

Post 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 :(
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: help with array

Post by onion2k »

Define "doesn't work".
3hhh
Forum Newbie
Posts: 22
Joined: Wed Sep 10, 2008 12:02 pm

Re: help with array

Post by 3hhh »

onion2k wrote:Define "doesn't work".
Hi,

When I did an echo $new_array, it shows Array0
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: help with array

Post by onion2k »

What were you expecting it to echo? It's an array.. that's what arrays do. Try print_r($new_array) instead.
3hhh
Forum Newbie
Posts: 22
Joined: Wed Sep 10, 2008 12:02 pm

Re: help with array

Post 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 :(
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: help with array

Post 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.
matthewboh
Forum Newbie
Posts: 2
Joined: Mon Oct 06, 2008 2:08 pm

Re: help with array

Post 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?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: help with array

Post 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
matthewboh
Forum Newbie
Posts: 2
Joined: Mon Oct 06, 2008 2:08 pm

Re: help with array

Post by matthewboh »

Thanks! I ended up using unset - did what I wanted it to do...
3hhh
Forum Newbie
Posts: 22
Joined: Wed Sep 10, 2008 12:02 pm

Re: help with array

Post 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";
}?>
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: help with array

Post 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";
3hhh
Forum Newbie
Posts: 22
Joined: Wed Sep 10, 2008 12:02 pm

Re: help with array

Post by 3hhh »

Thanks onion2k!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: help with array

Post 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.
?>
Post Reply