Jump to content

Help me on Array


terryds

Recommended Posts

The first one assigns an array to $var. The second one adds an array to the end of the $var array. So if the $var array looks like this:$var = array('apple', 'orange', 'grapes'); Then you do this:$var[] = array(); After doing that, the $var array should look like this: Array ( [0] => apples [1] => oranges [2] => grapes [3] => Array ( ) )

<?php$var = array('apples', 'oranges', 'grapes');$var[] = array(); print_r($var); //prints the contents of the $var array?>

Link to comment
Share on other sites

It means the same as: $var[] = array(); But with: $var[] = array ('a' => 'b', 'c' => 'd');you're filling in the array that gets added to the end of the $var array with values. It will look like this if you print_r $var:Array ( [0] => apples [1] => oranges [2] => grapes [3] => Array ( [a] => b [c] => d ) ) index 3 of the $var array has an array and that array has it's first index labeled 'a' and it's value is b. The second index is labeled 'c' and it's value is 'd'. Arrays with named keys/indexes are called Associative Arrays.

Link to comment
Share on other sites

Look at my code below : PHP

<?php// Display search forminclude_once $connect; try{$result = $pdo->query('SELECT id, name FROM author');}catch (PDOException $e){$error = 'Error fetching authors from database.';require $errpg;exit();} foreach ($result as $row) {$authors[] = array('id' => $row['id'],'name' => $row['name']);} try{$result = $pdo->query('SELECT id, name FROM category');} catch (PDOException $e){$error = 'Error fetching categories';include $errpg;exit();} foreach ($result as $row) {$categories[] = array('id' => $row['id'],'name' => $row['name']);} include 'searchform.html.php';exit();

The template

<?php include_once $helpers; ?><!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Manage Jokes</title></head><body><h1>Manage Jokes</h1><p><a href="?add">Add new joke</a></p><form action="" method="get"><p>View jokes satisfying by following criteria :</p><div><label for="author">By Author:</label><select name="author" id="author"><option value="">Any Option</option><?php foreach($authors as $author): ?><option value="<?php htmlout($author['id']); ?>"><?php htmlout($author['name']); ?></option><?php endforeach; ?></select></div><div><label for"category">By Category:</label><select name="category" id="category"><option value="">Any Option</option><?php foreach($categories as $category): ?><option value="<?php htmlout($category['id']); ?>"><?php htmlout($category['name']); ?></option><?php endforeach; ?></select></div><div><label for="text">Containing text :</label><input type="text" name="text" id="text"></div><div><input type="hidden" name="action" value="search"><input type="submit" value="Search"></div></form><p><a href="..">Return to JMS home</a></p></body></html>

Can you tell me why use $authors[] = array() and $categories[] = array(), not $author = array ???

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...