I have an array or arrays. Around 500 primaries with 6 lines each.
Here are the first 2 examples:
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
)
I want to inject a selection of this code into a while function to print the metadata on some images.
I need to query this list to find the variable $image
, which will have the values held in [Stock book code] eg. a-04. It has been suggested the best approach would be a Linq library. e.g. https://code.google.com/p/phinq
The array is currently called through a snippet, e.g. <?php snippet('csvtoarrayvibhu')?>
, so that could be pre-loaded globally or put in the if-statement. Opinions on which would be better are welcome.
<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' ?>" />
query here
<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>
I am currently working with csv to array complier using the code is as follows. From a project on gist hub https://gist.github.com/jaywilliams/385876 and the first answer from Joe below. It works wonderfully.
Don't know if it makes best sense to do this in one function or two so I quoted the lot below.
<?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>';
?>
It would be better if you could redesign your array so that it looks more like this;
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
)
)
Then you could just call the data like $additionalData[$image]['Work Title'], assuming your additional data array is stored in $additionalData and your $image contains the stock number of the item. What you're essentially doing here is creating an array of models, where each row of the array contains all the information about one instance of the data.
you can make your array look like this by adding a function so
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;
}
and calling it from your existing snippet.