Arrays in PHP

To define an Array

$prod = array();
$prod = array(“a”,”b”,”c”);

$prod = array(
“prod1” => array(“id”=>”prod001″,”date” => “111208”, “name”=>”aaa”);
“prod2” => array(“id”=>”prod002″,”date” => “111108”, “name”=>”bbb”);
“prod3 => array(“id”=>”prod003″,”date” => “111008”, “name”=>”ccc”);
);

$data=array();
$data[‘id’] = 1;
$data[‘name’] = ‘John’;

To read values out

foreach($SITE as $key1 => $val1){

foreach($sval1 as $key2 => $val2){

if($key2==”id”) echo “ID: $val2[0]\n”;

}

}

foreach($data as $value){

echo $value.”\n”;

}

Push elements into Array

PHP File access

Read by lines

$hd = fopen(“$file”, “r”);

if($hd){

while(!feof($hd)){

$line = fgets($hd);

}

}

fclose($hd);

Read the whole file in

$hd = fopen(“$file”, “r”);if($hd){

$contents = fread($hd, filesize($file));

}

fclose($hd);

Read image in binary

if(!($img = fopen(“$img_path”,”rb”))){

echo “couldn’t open $img_path”;

die;

}

while (!feof($img)) {

$contents .= fread($img, 1024);

}

fclose($img);

###Header for image
header(“Cache-Control: public”);

if(eregi(“\.[jpg|jpeg]”,$file)){

header (“Content-type: image/jpg”);

header(“Content-Disposition: inline; filename=$file”);

#header(‘Content-Length: ‘ . filesize($img));

print $contents;

}