I have the following code
$oldID=-1;
$column=0;
foreach($pv->rawData as $data) {
if ($oldID!= $data->relativeTypeID) {
$oldID= $data->relativeTypeID;
$column++;
$row=1;
}
echo "Row: ".$row.": Column: ".$column.": ID".$data->relativeTypeID."<br>";
// if exists a description
if($data->description) {
//insert here in the array
$pv->results[$data->relativeTypeID][$row][0]= $data->relation;
$pv->results[$data->relativeTypeID][$row][1]= '';
$pv->results[$data->relativeTypeID][$row][2] ='';
$pv->results[$data->relativeTypeID][$row][3] = '';
$row++;
}
}
That generates this output:
Row: 1: Colum开发者_JS百科n: 1: ID1
Row: 2: Column: 1: ID1 Row: 1: Column: 2: ID2 Row: 2: Column: 2: ID2 Row: 3: Column: 2: ID2 Row: 4: Column: 2: ID2 Row: 5: Column: 2: ID2 Row: 6: Column: 2: ID2 Row: 7: Column: 2: ID2 Row: 8: Column: 2: ID2 Row: 9: Column: 2: ID2 Row: 10: Column: 2: ID2 Row: 11: Column: 2: ID2 Row: 1: Column: 3: ID3 Row: 1: Column: 4: ID4 Row: 1: Column: 5: ID8 Row: 2: Column: 5: ID8 Row: 3: Column: 5: ID8 Row: 1: Column: 6: ID10 Row: 2: Column: 6: ID10 Row: 3: Column: 6: ID10 Row: 4: Column: 6: ID10 ... ...
What I want it to do is to stop at the top 4 columns so I want an output like this
Row: 1: Column: 1: ID1
Row: 2: Column: 1: ID1 Row: 1: Column: 2: ID2 Row: 2: Column: 2: ID2 Row: 3: Column: 2: ID2 Row: 4: Column: 2: ID2 Row: 5: Column: 2: ID2 Row: 6: Column: 2: ID2 Row: 7: Column: 2: ID2 Row: 8: Column: 2: ID2 Row: 9: Column: 2: ID2 Row: 10: Column: 2: ID2 Row: 11: Column: 2: ID2 Row: 1: Column: 3: ID3 Row: 1: Column: 4: ID4
As you can see it stopped at column 4.
You could do it like this.
foreach($pv->rawData as $data){
if ($oldID!= $data->relativeTypeID){
$oldID= $data->relativeTypeID;
$column++;
$row=1;
}
if ($column >=4) break; // Exit the foreach loop if column is equal to or bigger than 4
echo "Row: ".$row.": Column: ".$column.": ID".$data->relativeTypeID."
I'm just being paranoid with my condition, which might as well be $column == 4. Your call. The key is to break out of the loop using the break statement. Check the documentation for break here if you wish.
精彩评论