Jump to content
gunter

[solved] adding an element to wire array during loop/iteration

Recommended Posts

I have a sorted list of calendar events, stored in a wire array. I want  iterate/loop through the wire array and at a specific condition I want split the actual element into two elements... so I use inserAfter() the actual element - this works wonderfull, but with foreach the loop does not know that the array is now an element bigger....

so foreach does not work, insertAfter() does work, but the new element does not show up during the loop

// $s is a wire array containing 10 Elements
$insert = new Event(5555);

foreach($s as $key => $event){
	if($key==5){
		 $s->insertAfter($insert, $event); //insert an new item when $key==5
	}
	echo $event;
}

this works... the inserted item is shown after inserting it into the array

$insert = new Event(5555);
$count=count($s);
$counter = 0;

while($counter <$count) {
	if($counter==5){
		 $s->insertAfter($insert, $s[$counter]);
		 $count++;
	}
    echo "<br>".$s[$counter]->start;
    $counter++;
} 

but I want a shorter/ more elegant solution

Share this post


Link to post
Share on other sites
$insert = new Event(5555);
if($s->count() > 5) {
	$s->insertAfter($insert, $s->eq(5);
}

 

Share this post


Link to post
Share on other sites
$new = new Event(5555);
$s->add($new); // just prepend
$s->sort("myDateTimeFieldOrWhateverSortOption");

foreach ($s as $sortedWireDataObject)  {
  // ...
}

 

Share this post


Link to post
Share on other sites

Sorry guys, this is not what I need... inserting the array element should be IN the loop
I found some snippets here and will try it in the evening: 
https://stackoverflow.com/questions/2348077/change-initial-array-inside-the-foreach-loop

precede $value with &like so

$array = array('red', 'blue');
foreach($array as $key => &$value) {// <-- here
    $array[] = 'white';
    echo $value . '<br />';
}

A while loop would be a better solution.

while (list ($key, $value) = each ($array) ) {
    $array[] = 'white';
    echo $value . '<br />';
}

(edit: unfortunately this code did not work...)

 

Share this post


Link to post
Share on other sites

I will use this until I find a better solution

$value = $s->first();
while ($value) {
  	if($value->start==50){ //just an example to select an element 
 		$s->insertAfter($insert, $value);
 	}
    echo $value.'<br>';	
    $value = $s->getNext($value);
}

[edit] I have now the solution, I rewrote the iterator class for my wire array an now it works

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...