我有一个或多个数组。大约500个初选,每个6行。
下面是前两个例子:
Array
(
[0] => Array
(
[Stock book code] => a-04
[Date] => 1962
[Work Title] => River Barge
[height] => 0
[width] => 0
[materials] => Oil on Board
[Ownership] => Sold
)
[1] => Array
(
[Stock book code] => a-08
[Date] => 1962
[Work Title] => Thames Bridge Fantasy
[height] => 48
[width] => 36
[materials] => Oil on Board
[Ownership] => Available
)
我想将这段代码的一个选择注入到while函数中,以打印一些图像上的元数据。
我需要查询这个列表来找到变量$image
,它将具有在[Stock book code]中保存的值。a-04。有人建议最好的方法是使用Linq库。例如https://code.google.com/p/phinq
数组当前是通过代码段调用的,例如<?php snippet('csvtoarrayvibhu')?>
,因此可以全局预加载或放在if语句中。欢迎大家提出更好的意见。
<ul class="gallery">
<?php $imagelist= $page->imagelist();
if ($imagelist != ''){
$imagelistitem= explode(", ", $imagelist);
foreach ($imagelistitem as $image): ?>
<li><img src="<?php echo url('assets/artistswork/450/').$image.'.jpg' ?>" />
查询这里
<span class="workTitle">Title</span><span class="workDate">Date</span><span class="workMaterial">Material</span><span class="workDimensions">Dimensions</span><span class="workPrice">Price</span></li>
<?php endforeach ;} ?>
</ul>
我目前正在使用csv到数组编译器的代码如下所示。从gist hub https://gist.github.com/jaywilliams/385876上的一个项目和下面Joe的第一个答案。效果非常好。
不知道在一个或两个函数中这样做是否最有意义,所以我在下面引用了很多。
<?PHP
function csv_to_array($filename='', $delimiter=',') {
if (!file_exists($filename))
return 'not exist';
if (!is_readable($filename))
return 'not readable';
$header = NULL;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE) {
while (($row = fgetcsv($handle, 200, $delimiter)) !== FALSE) {
if (!$header)
$header = $row;
else
$data[] = array_combine($header, $row);
}
fclose($handle);
}
return $data;
}
function createModels($data)
{
$newRow = array();
if (is_array($data)){
foreach($data as $rowIndex => $row){
if(is_array($row)){
$newRowIndex = $row['Stock book code'];
}
$newData[$newRowIndex] = array_merge($newRow, $row);
}
}
return $newData;
}
echo '<pre>';
$imageinfoarray = csv_to_array('peterliddle/assets/artistswork/imagemeta.csv');
print_r(createModels($imageinfoarray));
echo '</pre>';
?>
如果你能重新设计你的数组,让它看起来更像这样,那就更好了;
Array
(
[a-04] => Array
(
[Stock book code] => a-04
[Date] => 1962
[Work Title] => River Barge
[height] => 0
[width] => 0
[materials] => Oil on Board
[Ownership] => Sold
)
[a-08] => Array
(
[Stock book code] => a-08
[Date] => 1962
[Work Title] => Thames Bridge Fantasy
[height] => 48
[width] => 36
[materials] => Oil on Board
[Ownership] => Available
)
)
然后你可以像$additionalData[$image]['Work Title']那样调用数据,假设你的附加数据数组存储在$additionalData中,你的$image包含商品的库存号。您在这里所做的实际上是创建一个模型数组,其中数组的每一行都包含关于一个数据实例的所有信息。
你可以让你的数组看起来像这样通过添加一个函数
function createModels($data)
{
$newRow = array();
if (is_array($data)){
foreach($data as $rowIndex => $row){
if(is_array($row)){
$newRowIndex = $row['Stock book code'];
}
$newData[$newRowIndex] = array_merge($newRow, $row);
}
}
return $newData;
}