我想我需要某种多维数组。我只是不知道如何以易于理解的格式获取它。基本上我有一堆发票去特定的公园。我想要的最终结果是在单个公园上显示多个发票的公园,所以我可以轻松地循环它。我有多个sql问题来获取变量,但我得到了它:
has_many :teams, -> {order 'team_enrollments.termination_date DESC NULLS LAST'}, through: :team_enrollments
在一切结束时,这是我的输出:
$sql = "SELECT * FROM INVOICE WHERE ID=$ids[$i]";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
//invoice variables go here
//row is invoices
$invoiceapplication = $row['APPLICATION'];
$invoiceid = $row['ID'];
$invoicedate = $row['DATE'];
$late = $row['LATE'];
$amountowed = '';
$amountowed = $row['AMOUNTOWED'];
$originalamountowed = $row['AMOUNTOWED'];
//grabbing application park id to get all the parks.
$sqlapplication = "SELECT * FROM APPLICATION WHERE ID=$invoiceapplication";
$resultapplication = $conn->query($sqlapplication);
if($resultapplication->num_rows > 0){
$rowapplication = $resultapplication->fetch_assoc();
//rowapplication is applications
//application variables here
$applicationparkid = $rowapplication['MOBILEPARK'];
//end app variables.
}
//finished grabbing application park id to get all the parks.
echo $invoiceid.' is park '.$applicationparkid.'<br>';
if(in_array($applicationparkid,$allparkarray)){
//if already contained in the allparkarray
} else {
array_push($allparkarray,$applicationparkid);
}
}
$i++;
}
理想情况下......我希望像这样的多维数组作为输出:
901 is park 38
1362 is park 38
1595 is park 38
1825 is park 38
2093 is park 38
4766 is park 38
17018 is park 61
17011 is park 66
17015 is park 66
17014 is park 66
17013 is park 66
17012 is park 66
请记住......发票完全有可能无法正常运行。在它的输出中,如果突然出现不同的数字......我基本上不能依靠简单的0,1,2,3系统来配对它。
我确信有一种简单的方法可以做到这一点我不能为我的生活弄明白。
答案 0 :(得分:1)
将其添加到数组中时,而不是......
if(in_array($applicationparkid,$allparkarray)){
//if already contained in the allparkarray
} else {
array_push($allparkarray,$applicationparkid);
}
你可以写......
$allparkarray[$applicationparkid][]=$invoiceid;
$allparkarray[$applicationparkid]
只是设置要添加值的点。因此$ applicationparkid是用于关联值的键。
[]位只是将值添加到数组的最后一个元素。或者是第一次新的阵列。