|
Posted by ImOk on 07/13/06 13:44
I didnt read all the code below but did you consider using
array_splice() to truncate the array?
$xx=array(1,2,3,5,6);
$yy=array_splice($xx,3);
$yy -> (5,6)
$xx-> (1,2,3)
Sonnich wrote:
> Hi all!
>
> I have 2 arrays of parts and assemblies. I check the part array whether
> there is an assembly part in there, if so, 1) it should be moved to
> the assembly array 2) its parts should be added to the parts array 3)
> it should be removed from the parts array.
>
> My problem is to delete from the assy array, ot truncate it. I am used
> to Delphi, where the is a SetLenght function.
>
> Code;
>
> $j=count($part1)-1; // counting down is faster than counting up
> while( ($j>=0) && !($part1[$j] == $CurrentItem]) )
> $j--;
> if($j>-1)
> {
> // move to assy
> $assy1[]=$part1[$j];
> // clear up array
> while($j < count($part1)-2)
> {
> $part1[$j]=$part1[$j+1];
> $j++;
> }
> // free last item
> unset($part1[$j]);
>
> This moves it all, but the unset does not remove it (the last one)
> from my array.
> So I tried this without luck (I dont like to do it this way, but that
> was a try):
>
> (when adding parts):
>
> It was as simple as this: $part1[]=odbc_result($result2,1);
>
> Now I tried: (if empty, then reuse)
> if( $part1[count($part1)-1] == "" )
> {
> $part1[count($part1)-1] = odbc_result($result2,1);
> }
> else
> {
> $part1[]=odbc_result($result2,1);
> }
>
> This instead adds a whole lot more and I end up with a number of items
> which I cannot explain and a number of empty items, which are not
> overwritten.
>
> Yep, I am still new to arrays in PHP
>
> BR
> Sonnich
[Back to original message]
|