Php 我有一个数组,它是如何显示的
可以这样显示一个数组吗Php 我有一个数组,它是如何显示的,php,arrays,Php,Arrays,可以这样显示一个数组吗 $a = array(array( 'dates' => '12-11-13', 'customer' => 'ann', 'place' => 'EKM'),array('dates' => '12-11-13', 'customer' => 'annex', 'place' => 'KLM'),array('dates' => '13-11-13',
$a = array(array( 'dates' => '12-11-13',
'customer' => 'ann',
'place' => 'EKM'),array('dates' => '12-11-13',
'customer' => 'annex',
'place' => 'KLM'),array('dates' => '13-11-13',
'customer' => 'anna',
'place' => 'PTA')
);
因此,输出将是:
dates:12-11-13
customer:ann
place:EKM
customer:annex
place:KLM
dates:13-11-13
customer:anna
place:PTA
所以,为了给你一个更明智的答案,你首先需要做的是正确地存储数组,这样你就可以正确地对它进行迭代,看起来你的键是日期,所以你在编译数组时应该做的是将所有类似的(日期)键保存在一起 例如:
// Array Creation
$orders = array();
$orders['12-11-13'][] = array('customer' => 'ann', 'place' => 'EKM');
$orders['12-11-13'][] = array('customer' => 'annex', 'place' => 'KLM');
$orders['10-11-13'][] = array('customer' => 'alex', 'place' => 'MCD');
$orders['10-11-13'][] = array('customer' => 'ronald', 'place' => 'BK');
这只是一个使用相似键创建数组的示例
然后,您将在数组上迭代,以显示您希望它的样子
foreach($orders as $date => $order)
{
echo "Date: {$date} \n";
foreach($order as $data)
{
echo "Customer: " . $data['customer'] ."\n";
echo "Place: " . $data['place'] ."\n";
}
echo "\n\n";
}
把这一切放在一起应该会给你想要的
<?php
// Array Creation
$orders = array();
$orders['12-11-13'][] = array('customer' => 'ann', 'place' => 'EKM');
$orders['12-11-13'][] = array('customer' => 'annex', 'place' => 'KLM');
$orders['10-11-13'][] = array('customer' => 'alex', 'place' => 'MCD');
$orders['10-11-13'][] = array('customer' => 'ronald', 'place' => 'BK');
foreach($orders as $date => $order)
{
echo "Date: {$date} \n";
foreach($order as $data)
{
echo "Customer: " . $data['customer'] ."\n";
echo "Place: " . $data['place'] ."\n";
}
echo "\n\n";
}
?>
单个数组
多阵列
预期输出的格式不一致。并使用foreach
loop。感谢您的支持..但我希望此数组采用上述格式$a=array(数组('dates'=>'12-11-13','customer'=>'ann','place'=>'EKM')、数组('dates'=>'12-11-13','customer'=>'appendment','place=>'KLM')、数组('dates'=>'13-11-13','customer'=>'anna','place'=>'PTA');@user3049172若要这样做,您必须迭代该数组,以使所有相似的日期合并到一个数组中。若要使其显示您想要的方式,请执行以下操作。
$ php test.php
Date: 12-11-13
Customer: ann
Place: EKM
Customer: annex
Place: KLM
Date: 10-11-13
Customer: alex
Place: MCD
Customer: ronald
Place: BK
Single Array
<?php
$test=array("data1","data2","data3");
echo "I like " . $test[0] . ", " . $test[1] . " and " . $test[2] . ".";
?>
Multiple Array
<?php
$test=array("teatdata"=>"testdata","testdata1"=>array("testinnderarraydata"=>"data"));
echo $test['testdata1']['testinnderarraydata'];
?>