通过循环访问数组内容

时间:2017-07-07 20:33:11

标签: php mysql arrays

我有一个mysql查询结果,我已经变成了一个数组:

$row = customfunction_fetch_array($results);

这是$row数组的外观:

Array(
  [0]=> Array ([Fruit]=> apple [Count]=>4 [Season]=>Summer
  [1]=> Array ([Fruit]=> grape [Count]=>1 [Season]=>Fall
  [2]=> Array ([Fruit]=> apple [Count]=>3 [Season]=>Winter
  [3]=> Array ([Fruit]=> orange [Count]=>5 [Season]=>Spring
  [4]=> Array ([Fruit]=> apple [Count]=>45 [Season]=>All
)

我要做的是循环显示特定字段的唯一值并将它们保存到新数组中。

$newArray =[];
foreach( $row["Fruit"] as $myFruits){
  $newArray[] = $myFruits;
}

我收到警告:

  

“为foreach()提供的参数无效”

1 个答案:

答案 0 :(得分:7)

根据您发布的数组,没有Highcharts.chart('container', { chart: { type: 'scatter' }, plotOptions: { series: { lineWidth:1, states: { hover: { lineWidthPlus: 2, marker: { enabled: true, radius:3, states: { hover: { fillColor:'#FF0000', lineColor:'#00FF00', lineWidth:3, radius:12 } } } } }, marker: { enabled: true, radius:3, states: { hover: { fillColor:'#FF0000', lineColor:'#00FF00', lineWidth:3, radius:12 } } } } }, series: [{ data: [[29.9, 71.5], [106.4, 129.2], [144.0, 176.0], [135.6, 148.5], [216.4, 194.1], [95.6, 54.4]] }] }); ,有$row['Fruit']$row[0]['Fruit']等...

$row[1]['Fruit']

但是,要获得所有foreach($row as $values){ $newArray[] = $values['Fruit']; } ,您只需执行此操作:

Fruits