home ~ previous

PHP

Arrays

It’s good to know how to interpret arrays.

Here is an example that loads the array and spits it out via a foreach loop to the browser.

Although this is a simple example, there is enough scope here to build a larger application, should your data be presented in one.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
#### this is the array. I would expect it to be loaded in somehow, probably, but not necessarily by cURL
$data = array(
0 => array(
'title' => "Hadoop: The Definitive Guide [Paperback]",
'author' => "Tom White ",
'price' => "19.25",
'daysInTop100' => 0,
'href' => "http://www.amazon.co.uk/Hadoop-Definitive-Guide-Tom-White/dp/1449311520/ref=sr_1_1?ie=UTF8&qid=1351857254&sr=8-1",
'imageSrc' => "http://ecx.images-amazon.com/images/I/51Mvsl3gMIL._BO2,204,203,200_PIsitb-sticker-arrow-click,TopRight,35,-76_AA300_SH20_OU02_.jpg",
),
1 => array(
'title' => "Hadoop in Action [Paperback]",
'author' => "Chuck Lam" ,
'price' => "27.19",
'daysInTop100' => 0,
'href' => "http://www.amazon.co.uk/Hadoop-Action-Chuck-Lam/dp/1935182196/ref=sr_1_2?ie=UTF8&qid=1351857254&sr=8-2",
'imageSrc' => "http://ecx.images-amazon.com/images/I/51W-lrKKv%2BL._BO2,204,203,200_PIsitb-sticker-arrow-click,TopRight,35,-76_AA300_SH20_OU02_.jpg",
),
2 => array(
'title' => "MapReduce Design Patterns",
'author' => "Donald Miner",
'price' => "25.02",
'daysInTop100' => 0,
'href' => "http://www.amazon.co.uk/MapReduce-Design-Patterns-Effective-Algorithms/dp/1449327176/ref=sr_1_6?ie=UTF8&qid=1351857254&sr=8-6",
'imageSrc' => "http://ecx.images-amazon.com/images/I/61ufyLoMR7L._SL500_AA300_.jpg",
),
3 => array(
'title' => "Programming Pig [Paperback]",
'author' => "Alan Gates",
'price' => "20.14",
'daysInTop100' => 1,
'href' => "http://www.amazon.co.uk/Programming-Pig-Alan-Gates/dp/1449302645/ref=sr_1_10?ie=UTF8&qid=1351857254&sr=8-10",
'imageSrc' => "http://ecx.images-amazon.com/images/I/51beaVs5OjL._BO2,204,203,200_PIsitb-sticker-arrow-click,TopRight,35,-76_AA300_SH20_OU02_.jpg",
),
4 => array(
'title' => "Pro Hadoop (Expert's Voice in Open Source) [Paperback]",
'author' => "Jason Venner",
'price' => "29.92",
'daysInTop100' => 3,
'href' => "http://www.amazon.co.uk/Hadoop-Experts-Voice-Open-Source/dp/1430219424/ref=sr_1_11?ie=UTF8&qid=1351857254&sr=8-11",
'imageSrc' => "http://ecx.images-amazon.com/images/I/51uypFXbOpL._BO2,204,203,200_PIsitb-sticker-arrow-click,TopRight,35,-76_AA300_SH20_OU02_.jpg",
),
5 => array(
'title' => "Hadoop in Practice [Paperback]",
'author' => "Alex Holmes",
'price' => "31.99",
'daysInTop100' => 3,
'href' => "http://www.amazon.co.uk/Hadoop-Practice-Alex-Holmes/dp/1617290238/ref=sr_1_12?ie=UTF8&qid=1351857254&sr=8-12",
'imageSrc' => "http://ecx.images-amazon.com/images/I/4163rlEHzlL._BO2,204,203,200_PIsitb-sticker-arrow-click,TopRight,35,-76_AA300_SH20_OU02_.jpg",
),
6 => array(
'title' => "Hadoop Operations [Paperback]",
'author' => "Eric Sammer",
'price' => "26.34",
'daysInTop100' => 2,
'href' => "http://www.amazon.co.uk/Hadoop-Operations-Eric-Sammer/dp/1449327052/ref=sr_1_14?ie=UTF8&qid=1351857254&sr=8-14",
'imageSrc' => "http://ecx.images-amazon.com/images/I/51pDy48gstL._BO2,204,203,200_PIsitb-sticker-arrow-click,TopRight,35,-76_AA300_SH20_OU02_.jpg",
),
7 => array(
'title' => "Cassandra: The Definitive Guide [Paperback]",
'author' => "Eben Hewitt",
'price' => "20.14",
'daysInTop100' => 7,
'href' => "http://www.amazon.co.uk/Cassandra-Definitive-Guide-Eben-Hewitt/dp/1449390412/ref=sr_1_22?ie=UTF8&qid=1351864760&sr=8-22",
'imageSrc' => "http://ecx.images-amazon.com/images/I/51X9yyUJ%2BnL._BO2,204,203,200_PIsitb-sticker-arrow-click,TopRight,35,-76_AA300_SH20_OU02_.jpg",
),
);
#### Here the array is processed
$sortarray = array();
foreach ($data as $k=>$d) {
$price = $d['price'];
$sortarray[$k] = $price;
}
arsort($sortarray, SORT_NUMERIC );

$newdata = array();
foreach($sortarray as $k=>$d) {
$newdata[$k] = $data[$k];
}
##### below is the HTML with some VER simple styling.
?>
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><meta name="description" content="meta description"/>
<meta name="keywords" content="keywords"/>
<meta name="author" content="author" >
<link rel="stylesheet" media="all" href="complete.css" type="text/css">
<title>title info</title>
<style>
body {
	background-color: #ebebeb;
}
.container {
	width: 1200px;
	margin: 10px auto;
	background-color: #fff;
	padding:10px 0 0 10px;
	font-family: arial;
}
.field {
	width: 166px;
	float: left;
}
.clear {
	clear: both;
}
a {
	text-decoration: underline;
	color: #000;
	font-weight: bold;
}
hr {
    border-collapse: collapse;
    border-style: solid;
    margin: 20px 8px 20px 0;
}
</style>
</head>
<body>
<div class="container">
<?php
##### here is the foreach loop, that loops through the array and puts the values into the div containers

foreach ($newdata as $value){?>
	<div class="field"><?php echo($value['title']); ?></div>
	<div class="field"><?php echo($value['author']); ?></div>
	<div class="field"><?php echo($value['price']); ?></div>
	<div class="field"><?php echo($value['daysInTop100']); ?></div>
	<div class="field"><a href="<?php echo($value['href']); ?>">click here</a></div>	
	<div class="field"><img src="<?php echo($value['imageSrc']) ?>"/></div>
	<div class="clear"></div>
	<hr />
<?php } ?>
</div>
</body>
</html>

home ~ previous

Rohan Leach .... thoughtful