• You are not logged in. | Login

Post a reply

  • Index
  •  » Help me!
  •  » Massives -> array_splice -> are duplicated

March 7, 2007 1:55 pm

steven9x
Member
Ranks

Massives -> array_splice -> are duplicated

Hi! I have such a trouble. I make a class and in it
var $id;
var $name;

In the initialization these variables become a blank massive:
$this->$id = array();
$this->$name = array();

Further working with class I call function of this class containing following code:

array_splice($this->$id, count($this->$id), 0, $newId); 
array_splice($this->$name, count($this->$name), 0, $newCategoryName);

where $newId and $newCategoryName are the values which should be added to the massives.

Everything would be all right but accomplishing of the last two strings results into increasing a massive twice, i.e. after the accomplishment we have:

count($this->$id) = 2
count($this->$name) = 2

though it should be:

count($this->$id) = 1
count($this->$name) = 1

Why is it so?

Before array_splice another code is accomplished, perhaps, this also should matter:

$newId = 0; 
if (count($this->$id) > 0) { 
  $newId = max($this->$id); 
} 
$newId += 1;

PHP 4.5 @ windows XP

P.S. Sorry if my explanations aren??™t clear??¦


 

 

March 7, 2007 1:59 pm

biopd42
Member
Ranks

Re: Massives -> array_splice -> are duplicated

As I understand you have some programming experience??¦ In what language?


 

 

March 7, 2007 2:09 pm

steven9x
Member
Ranks

Re: Massives -> array_splice -> are duplicated

Sure. In ASP, ASP .NET, VB, VB .NET, Delphi, C#, )


 

 

March 7, 2007 2:15 pm

biopd42
Member
Ranks

Re: Massives -> array_splice -> are duplicated

Then I don??™t understand your phrase

Before array_splice another code is accomplished, perhaps, this also should matter

Why cannot you check on your own if it matters or not?
Why cannot you write a text example without classes, methods and ???another codes??™?
Why don??™t you reduce the number of unknown values in the task by minimizing them?

As to your question??¦
Frankly speaking, I didn??™t understand it completely??¦

$newId and $newCategoryName are the values which should be added to the massives

If you need to add
1) why such an original function as array_splice is used for it?
2) why are you surprised with the fact that massive size increased?


 

 

March 7, 2007 2:21 pm

steven9x
Member
Ranks

Re: Massives -> array_splice -> are duplicated

Why cannot you check on your own if it matters or not?

I??™ve checked it:

$id = array(); 
$name = array(); 
$newId = 0; 
         
if (count($id) > 0) { 
  $newId = max($id); 
} 
$newId += 1; 

array_splice($id, count($id), 0, $newId); 
array_splice($name, count($name), 0, "some text "); 
         
echo count($name);

It functions properly, count($name) = 1.

1) why such an original function as array_splice is used for it?

I??™m new to PHP and haven??™t studied all the functions yet. If there are some other functions, show them to me and I will use them.

2) why are you surprised with the fact that massive size increased?

I??™m surprised with the fact size of each massive in the object increases by each call of array_splice i.e. the same code placed within class returns count($name) = 2 instead of count($name) = 1:

array_splice($id, count($id), 0, $newId); 
array_splice($name, count($name), 0, "some text");

As you can see I insert new $newId value into $id massive and in the next string $name massive is also a new value. Within class after accomplishment of the first string:

array_splice($id, count($id), 0, $newId);

$name massive also increases somehow?!

Sure, I wrote like this as well:

array_splice($id, count($id), 0, $newId); 
$this->$name[count($this->$name)] = "some text ";

And it works. But is it correct? Will I have any problems with it in the future? And why all the massives increase?

Last edited by steven9x (March 7, 2007 2:22 pm)


 

 
  • Index
  •  » Help me!
  •  » Massives -> array_splice -> are duplicated
  • Actions
  • Top