我正在尝试从PHP多维数组中提取特定数据。
数组是这样的:
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
)
)
我想要做的是在数组中循环并从每个条目中拉出0,1,2,4,6并显示该数据。
这是我尝试使用的:
$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>";
}
我无法显示任何内容。
我也试过了:
for($i = 0; $i < count($csv); $i++) {
echo "<tr>";
foreach($csv[$keys[$i]] as $key => $value) {
echo "<td>".$value(0)."</td>";
}
echo "</tr>";
}
我无法弄清楚我做错了什么。任何帮助都会很棒。
答案 0 :(得分:1)
尝试
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>";
}
答案 1 :(得分:1)
尝试此代码:
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>";
}
答案 2 :(得分:0)
使用两个foreach
:
foreach ($row ar $tr) {
echo "<tr>";
foreach($tr as $td) {
echo "<td>".$td."</td>";
}
echo "</tr>";
}
答案 3 :(得分:0)
假设=有
$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
)
)
您可以使用几个foreach
查看内容 foreach($myArray as $key1 => $value1){
foreach( $value1 as $key2 => $value2 ){
echo $key1 . ' ' $key2 .' value ' .$value2
}
}
或特定访问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>';
}
}
答案 4 :(得分:0)
采用不同的方法,将数组合并为第1个作为键,第2个作为值。
//$data is contains your multidimensional array!
$combinedData = array_combine($data[0],$data[1]);
现在访问....喜欢!
echo $combinedData["Item Code Number"] ; // output 1
echo $combinedData["Item Name"]; //output Amazing Magic Set
//And so on....