我一直在讨论这个阵列:
Array (
[invoicenr] => Array (
[0] => 1234
[1] => 1234
[2] => 1234
[3] => 4321
[4] => 3214
)
[invoicedate] => Array (
[0] => 17.07.2017.
[1] => 17.07.2017.
[2] => 17.07.2017.
[3] => 11.07.2017.
[4] => 11.07.2017. )
[amount] => Array (
[0] => 10
[1] => 1
[2] => 23
[3] => 10
[4] => 1 )
[cause] => Array (
[0] => 1
[1] => 1
[2] => 1
[3] => 1
[4] => 1 )
)
我试图根据第一级关键字" invoicenr"价值,但到目前为止没有运气。 我期待结果:
Array (
[invoicenr] => Array (
[0] => 1234
[1] => 1234
[2] => 1234)
[invoicedate] => Array (
[0] => 17.07.2017.
[1] => 17.07.2017.
[2] => 17.07.2017.)
[amount] => Array (
[0] => 10
[1] => 1
[2] => 23)
[cause] => Array (
[0] => 1
[1] => 1
[2] => 1)
)
Array (
[invoicenr] => Array (
[0] => 4321 )
[invoicedate] => Array (
[0] => 11.07.2017. )
[amount] => Array (
[0] => 10)
[cause] => Array (
[0] => 1 )
)
Array (
[invoicenr] => Array (
[0] => 3214 )
[invoicedate] => Array (
[0] => 11.07.2017. )
[amount] => Array (
[0] => 1)
[cause] => Array (
[0] => 1 )
)
我想知道这是否可行以及如何,或者我需要先重写数组?
先谢谢大家,到目前为止,我是新编码和努力学习的人。 :)
答案 0 :(得分:1)
请发布您尝试解决问题的代码。
我相信你需要一个计数器(i)来通过invoicenr和一个记住最后一个值(Array [i])的临时变量,并将它与invoicenr数组中的下一个值进行比较。如果值保持不变,如果它不同,则打印出所有其他数组[i]。
答案 1 :(得分:0)
我已经完成了它,但我认为我的方式太原始了。这是我做的:
//Count number of rows in initial array
$rows = count(array_filter($_POST['invoicenr']));
//Crete unique array that contains only $_POST['invoicenr'] which i can count later
$uniquearray = array();
for ($row = 0; $row < $rows; $row++) {
$uniquearray[] = $_POST['invoicenr'][$row] ;
}
//return unique keys only
$invoicenrunique=array_keys(array_flip($uniquearray));
//count number of rows in unique array
$invoicenruniquecount=count(array_keys(array_flip($uniquearray)));
//do magic, this splits main array into n smaller arrays based on $_POST['invoicenr']
for($uniquerow = 0; $uniquerow < $invoicenruniquecount; $uniquerow++) {
$testarray = array();
for ($row = 0; $row < $rows; $row++) {
if($_POST['invoicenr'][$row]==$invoicenrunique[$uniquerow]){
$testarray[$row]['invoicenr']=$_POST['invoicenr'][$row];
$testarray[$row]['invoicedate']=$_POST['invoicedate'][$row];
$testarray[$row]['amount']=$_POST['amount'][$row];
$testarray[$row]['cause']=$_POST['cause'][$row];
}
}
print_r(array_values($testarray));
echo "<br>";
}
您对如何改进我的解决方案有任何建议吗?
祝你好运!