By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,994 Members | 2,073 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,994 IT Pros & Developers. It's quick & easy.

truncating an array

P: n/a
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

Jul 13 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Jul 13 '06 #2

P: n/a
Rik
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
A lot of code, that really doesn't make things clear to me.
Could you give an example of the array you have, and the array(s) you want
to have? That would make things a lot clearer I think.

I mainly don't understand this bit:
2) its parts should be added to the parts array
3) it should be removed from the parts array

Grtz,
--
Rik Wasmus
Jul 13 '06 #3

P: n/a

ImOk wrote:
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)
ok, this did it for me.

BR
Sonnich

Jul 17 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.