Clan Adverts

Php Talk · Working with Arrays in PHP and PhpNuke for Beginners

Posted on Thursday, August 13, 2009 @ 13:18 by admin

I think one of the key features of PHP is surely the array.  If you code php then you use them more than you know.

An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.

Today I would just like to cover some of the basics to help people better understand how they are used.  You can find all this information on the php.net website, including full documentation on all array functions.  I think the most common use of an array that phpnuke users see is from working with the database.

<?php
require_once 'mainfile.php';
global $db, $prefix;
$sql = 'SELECT username FROM '.$prefix.'_users WHERE user_id <= "5"';
$query = $db->sql_query($sql);
while($row = $db->sql_fetchrow()){//$row is returned as an array
	echo $row['username'].'<br />';
}
?> 
//Returns
Anonymous
Ped
JunkMonkey
floppy

The Basics

An array can be created by the array() language construct. It takes as parameters any number of comma-separated key => value pairs.

A simple array would look something like this

<?php
$fruits = array('1' => 'apple', '2' => 'orange', '3' => 'pear');

echo $fruits['1'];//apple
echo $fruits['2'];//orange
echo $fruits['3'];//pear
?>

The array construct is just simply array();. The numbers (1, 2, 3) are known as the keys and apple, orange, pear are their values.  A key may be a string or integer.  A value can be any data of any type.

Multidimensional Arrays

To create multidimensional arrays we are simply creating an array inside another.

<?php
$arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42));

echo $arr["somearray"][6];    // 5
echo $arr["somearray"][13];   // 9
echo $arr["somearray"]["a"];  // 42
?>

As you can see, we now have two array constructs.  You can have as many as you would like to my knowledge.

Displaying All the Elements of a Compiled Array

There are few ways to simply display everything within an array.  The most common is print_r

<?php
require_once 'mainfile.php';
global $db, $prefix;
$sql = 'SELECT username FROM '.$prefix.'_users WHERE user_id <= "5"';
$query = $db->sql_query($sql);
$row = $db->sql_fetchrowset();
print_r($row);//Print All Elements of this array
?>
//Returns
Array ( [0] => Array ( [0] => Anonymous [username] => Anonymous ) [1] => Array ( [0] => Ped [username] => Ped ) [2] => Array ( [0] => JunkMonkey [username] => JunkMonkey ) [3] => Array ( [0] => floppy [username] => floppy ) )

//Php Usuage
echo $row['1']['username'];//Ped
echo $row['3']['username'];//Floppy

Looping Example for Php-Nuke

In this example we have requested the user_id and username from the users table as long as the user_id is less than or equal to 20 AND it is not 1.  Our loop will continue until those requirements are met.  If you wanted every column from the users table you would just use * instead of user_id, username.  When you use the asterisk (*) for all details it is a good time to use print_r to see what is returned from the database.

<?php
require_once 'mainfile.php';
global $db, $prefix;
$example = array();//Initialize new array
$sql = 'SELECT user_id, username FROM '.$prefix.'_users WHERE user_id <= "20" AND user_id != "1"';//Any user id that is less or equal to 20 AND not equal to 1 (Anonymous)
$query = $db->sql_query($sql);
/*Easy way to check for mysql errors*/
if(mysql_error()){
	echo mysql_error();//Show me error
	die();//Stop Script
}
while($row = $db->sql_fetchrow()){//Initiates a loop that doesn't stop until $sql requirements have been met
	echo 'UserID: '.intval($row['user_id']).' - Username: '.mysql_real_escape_string($row['username']).'<br />';
	$example[] = $row;//Adding the $row array to the $example array to be displayed after the loop (not necessary for you)
}
echo '<br />';//Line Break
print_r($example);//Show me all components of the $example array
?>
UserID: 2 - Username: Ped
UserID: 4 - Username: JunkMonkey
UserID: 5 - Username: floppy
UserID: 9 - Username: nbpro
UserID: 10 - Username: Kenny
UserID: 11 - Username: JAGGEDJUNKIE
UserID: 12 - Username: inter-corrupt
UserID: 13 - Username: corruptor
UserID: 14 - Username: SPLIN
UserID: 15 - Username: Foggy
UserID: 16 - Username: RogueDOC
UserID: 17 - Username: pablo_Ba
UserID: 18 - Username: gnav
UserID: 19 - Username: DocHaVoC
UserID: 20 - Username: tester

Array ( [0] => Array ( [0] => 2 [user_id] => 2 [1] => Ped [username] => Ped ) [1] => Array ( [0] => 4 [user_id] => 4 [1] => JunkMonkey [username] => JunkMonkey ) [2] => Array ( [0] => 5 [user_id] => 5 [1] => floppy [username] => floppy ) [3] => Array ( [0] => 9 [user_id] => 9 [1] => nbpro [username] => nbpro ) [4] => Array ( [0] => 10 [user_id] => 10 [1] => Kenny [username] => Kenny ) [5] => Array ( [0] => 11 [user_id] => 11 [1] => JAGGEDJUNKIE [username] => JAGGEDJUNKIE ) [6] => Array ( [0] => 12 [user_id] => 12 [1] => inter-corrupt [username] => inter-corrupt ) [7] => Array ( [0] => 13 [user_id] => 13 [1] => corruptor [username] => corruptor ) [8] => Array ( [0] => 14 [user_id] => 14 [1] => SPLIN [username] => SPLIN ) [9] => Array ( [0] => 15 [user_id] => 15 [1] => Foggy [username] => Foggy ) [10] => Array ( [0] => 16 [user_id] => 16 [1] => RogueDOC [username] => RogueDOC ) [11] => Array ( [0] => 17 [user_id] => 17 [1] => pablo_Ba [username] => pablo_Ba ) [12] => Array ( [0] => 18 [user_id] => 18 [1] => gnav [username] => gnav ) [13] => Array ( [0] => 19 [user_id] => 19 [1] => DocHaVoC [username] => DocHaVoC ) [14] => Array ( [0] => 20 [user_id] => 20 [1] => tester [username] => tester ) ) 

I know these are very basic examples, but I am trying to at least give some users simple idea of how arrays are used.


Votes up: 6 / Votes down: 0
Random Products

No Comments Allowed for Anonymous, please register
 

Advertisement