Reputation: 245
I'm trying to pull specific data from a PHP multidimensional array.
Array is like so:
Array
(
[0] => Array
(
[0] => Item Code Number
[1] => Item Name
[2] => Item Description
[3] => MSRP
[4] => Cost
[5] => Weight Pounds
[6] => Weight Kgs
[7] => Category 1
[8] => Category 2
[9] => Category 3
[10] => Category 4
[11] => Youtube Link
[12] => Large Image URL
)
[1] => Array
(
[0] => 1
[1] => Amazing Magic Set
[2] => Easy to learn magic tricks
[3] => 24.95
[4] => 10.5
[5] => 0.95
[6] => 0.43
[7] => New Items
[8] => Tricks
[9] => *New Tricks*
[10] => All Tricks
[11] => blank
[12] => http://www.magicmakersinc.com/images/product/large/0051.jpg
)
)
What I want to do it loop through the array and pull keys, 0, 1, 2, 4, 6 from each entry and display that data.
This is what I've tried to use:
$keys = (array_keys($csv));
for ($row = 0; $row < sizeof($csv); $row++) {
echo "<tr>";
foreach($keys[$row] as $key => $value) {
echo "<td>".$value."</td>";
}
echo "</tr>";
}
I can't get this to display anything.
I've also tried:
for($i = 0; $i < count($csv); $i++) {
echo "<tr>";
foreach($csv[$keys[$i]] as $key => $value) {
echo "<td>".$value(0)."</td>";
}
echo "</tr>";
}
I can't figure out what I'm doing wrong. Any help would be great.
Upvotes: 1
Views: 58
Reputation: 1571
Something different approach, Combining array together 1st as key, 2nd as value.
//$data is contains your multidimensional array!
$combinedData = array_combine($data[0],$data[1]);
Now access.... Like!
echo $combinedData["Item Code Number"] ; // output 1
echo $combinedData["Item Name"]; //output Amazing Magic Set
//And so on....
Upvotes: 0
Reputation: 133410
assuming = have
$myArray = Array(
[0] => Array
(
[0] => Item Code Number
[1] => Item Name
[2] => Item Description
[3] => MSRP
[4] => Cost
[5] => Weight Pounds
[6] => Weight Kgs
[7] => Category 1
[8] => Category 2
[9] => Category 3
[10] => Category 4
[11] => Youtube Link
[12] => Large Image URL
)
[1] => Array
(
[0] => 1
[1] => Amazing Magic Set
[2] => Easy to learn magic tricks
[3] => 24.95
[4] => 10.5
[5] => 0.95
[6] => 0.43
[7] => New Items
[8] => Tricks
[9] => *New Tricks*
[10] => All Tricks
[11] => blank
[12] => http://www.magicmakersinc.com/images/product/large/0051.jpg
)
)
You can see the content using a couple of foreach
foreach($myArray as $key1 => $value1){
foreach( $value1 as $key2 => $value2 ){
echo $key1 . ' ' $key2 .' value ' .$value2
}
}
or for specific access to 0, 1, 2, 4, 6
foreach($myArray as $key1 => $value1){
echo $key1 . ' ' . $value1[0] . '</br>';
echo $key1 . ' ' . $value1[1] . '</br>';
echo $key1 . ' ' . $value1[2] . '</br>';
echo $key1 . ' ' . $value1[4] . '</br>';
echo $key1 . ' ' . $value1[6] . '</br>';
}
}
Upvotes: 0
Reputation: 275
Try This code:
foreach ($row ar $val) {
echo "<tr>";
foreach($val as $k => $v) {
if($k == '0' || $k == '1' || $k == '2' || $k == '4' || $k == '6'){
echo "<td>".$td."</td>";
}
}
echo "</tr>";
}
Upvotes: 1
Reputation: 2275
try
for ($row = 0; $row < sizeof($csv); $row++) {
echo "<tr>";
foreach($csv[$row] as $key => $value) {
if($k == '0' || $k == '1' || $k == '2' || $k == '4' || $k == '6'){
echo "<td>".$value."</td>";
}
}
echo "</tr>";
}
Upvotes: 1
Reputation: 1391
Use two foreach
:
foreach ($row ar $tr) {
echo "<tr>";
foreach($tr as $td) {
echo "<td>".$td."</td>";
}
echo "</tr>";
}
Upvotes: 0