[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
PhpArray

(PHP) Array

<?php 
// file example 1: read a text file into an array, with 
// each line in a new element 
$filename="input.txt"; 
$lines = array(); 
$file = fopen($filename, "r"); 
while(!feof($file)) { 
    //read file line by line into a new array element 
    $lines[] = fgets($file, 4096); 
   
} 
fclose ($file); 
print_r($lines); 
?> 


This snippet shows how to read a text file line by line and create an array of lines to represent the read file.

This example has been taken from http://www.snippetcollection.com
<?php
//file example 2: this class reads a CSV file."comma delimited file" with four elements.	
class stockdetails
{
//defines an array called allStock;		
  private $allStocks;
		
  function __construct(){
  }

//Method that processes the file, one line at a time.
	
function process_file($file_name){		
  $d_filecontent = file($file_name);
  $i=0;
 /*reads each line, then explodes the line into a list.
 the elements in the list are then assigned into an 2 dimensional  associative array
    • /
foreach($d_filecontent as $aline) { list($a,$b,$c,$d)= explode(",",$aline); $this->allStocks[$i]['stock_number'] = $a; $this->allStocks[$i]['stock_name'] = $b; $this->allStocks[$i]['stock_code'] = $c; $this->allStocks[$i]['stock_price'] = $d; $i++; } } // method to get the entire array structure. function get_allStocks(){ return $this->allStocks; } } //end class here. ?>
This code is brought to you courtsey of http://agbagbara.freeserverhost.com/

Arrays in other languages



last edited (November 11, 2006) by bilderbikkel, Number of views: 4548, Current Rev: 5 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.