php foreach question
Posted
by user295189
on Stack Overflow
See other posts from Stack Overflow
or by user295189
Published on 2010-05-06T15:54:17Z
Indexed on
2010/05/06
15:58 UTC
Read the original article
Hit count: 578
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++;
}
}
this generates this output
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
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.
Thanks
© Stack Overflow or respective owner