473,789 Members | 3,013 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

truncating an array

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($r esult2,1);

Now I tried: (if empty, then reuse)
if( $part1[count($part1)-1] == "" )
{
$part1[count($part1)-1] = odbc_result($re sult2,1);
}
else
{
$part1[]=odbc_result($r esult2,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
3 12529
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_splic e($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($r esult2,1);

Now I tried: (if empty, then reuse)
if( $part1[count($part1)-1] == "" )
{
$part1[count($part1)-1] = odbc_result($re sult2,1);
}
else
{
$part1[]=odbc_result($r esult2,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
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($r esult2,1);

Now I tried: (if empty, then reuse)
if( $part1[count($part1)-1] == "" )
{
$part1[count($part1)-1] = odbc_result($re sult2,1);
}
else
{
$part1[]=odbc_result($r esult2,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

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_splic e($xx,3);

$yy -(5,6)
$xx-(1,2,3)
ok, this did it for me.

BR
Sonnich

Jul 17 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1783
by: Jeremy | last post by:
I have an include file which has some text such as: The quick brown fox jumped over the lazy dog I want to have a certain amount of that text to display on one page wherein the user can click a link for more of the text on a new page such as: The quick brown fox >>MORE
4
21985
by: qazmlp | last post by:
Can anybody comment(& suggest improvements) on the following implementation that is for truncating the character buffer contents to the desired length? Truncating char array void truncateCharArray(int maxLength , char * buffer ) { if ((0 == buffer) || !(0 < maxLength)) return; if (maxLength < strlen(buffer))
5
5432
by: VISHNU VARDHAN REDDY UNDYALA | last post by:
Hello, Can someone over here help me in truncating a float variable. I mean if PI=3.14159 ...How can I get to read the first two or first three decimal values with out rounding them. Any suggestions are appreciated. I am BEGINNER TO C PROGRAAMING. Thanks
1
2522
by: Jitesh Sinha | last post by:
Hi, I am running Windows 2003/ IIS 6.0. I was stuck with rather a abnormal behaviour of System.Web.mail class. It was truncating the message body after 3,071 character. The code i was testing was: Import Namespace="System.Web.Mail" Dim aa as new StringBuilder(600010) Dim i as Integer
4
3406
by: DotNetJunkies User | last post by:
I am calling a VB6 dll from a vb.net windows application that returns an array of strings. My issue is it seems to truncate after a NULL character. For Example VB 6 is returning a string with the HEX value of 4E 31 00 00 01 00 20 20 20 20 20 00 00 00 20 20 20 31 32 30. But when it gets back to Vb.net all I have is 4E 31 or N1. Now I can return the same string as a return value of a function and I get it all. Please Help, Patrick Horn...
3
2263
by: rangermccoy | last post by:
Hello there, What are the best php/c libraries for handling media including images, video, and music? I would like to manipulate media dfiles, including watermarking, thumbnailing, truncating, etc. I know there's the GD llibrary for images.
1
1642
by: Daniel Wilson | last post by:
We have a stored procedure that is giving us an XML representation (using FOR XML Explicit) of a sales order. Sometimes that evaluats to quite a long string. Unfortunately, this XML string is being truncated to 2034 characters before we can do anything with it. Here's our code: Private Sub LoadXML()Dim dtaXML As New SqlClient.SqlDataAdapter("MyStoredProcedureThatReturnsXML", "MyConnectionString")Dim dsXML As New...
3
5091
by: rlrcstr | last post by:
Is it possible to truncate a MemroyStream to free up the space up to the current position? I wantes to be able to just keep dumping bytes into a stream and then read the stream as needed. After the read, free up the portion of the stream I read, esentially making the first unread byte the first byte in the stream. Thanks. Jerry
2
2045
by: masterofzen | last post by:
Hi, everybody -- first post for me, though I land here via Google all the time. This is the first time, though, that I just haven't been able to find anything even remotely resembling a solution to my problem. I have a large block of HTML. I won't get into unnecessary specifics, but let's just say that it's not possible for me to edit this HTML directly or on the server side. Any changes I make to it has to be on the client side, through...
0
9665
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10408
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3697
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.