Skip to main content

Try the following,

array_push(yourarray, rtrim(array_pop(yourarray), ' **and**'and'));

It will pop the last element, trim away the and" and" at the end and then re-add itthe modified element to the array.

ExampleFor example,

$yourarray = array("date = '2012-02-22' and time ='08:49' and",  
             "date = '2012-02-22' and time ='08:49' and ");

var_dump($yourarray);
array_push($yourarray, rtrim(array_pop($yourarray), ' **and**'and'));
var_dump($yourarray);

Outputsoutputs the following

array(2) {
  [0]=>
  string(41) "date = '2012-02-22' and time ='08:49' and"
  [1]=>
  string(42) "date = '2012-02-22' and time ='08:49' and "
}

array(2) {
  [0]=>
  string(41) "date = '2012-02-22' and time ='08:49' and"
  [1]=>
  string(37) "date = '2012-02-22' and time ='08:49'"
}

So, the last and" and" in the last element ishas been removed.

Try

array_push(yourarray, rtrim(array_pop(yourarray), ' **and**'));

It will pop the last element, trim away the and at the end and then re-add it to the array.

Example

$yourarray = array("date = '2012-02-22' and time ='08:49' and",  
             "date = '2012-02-22' and time ='08:49' and ");

var_dump($yourarray);
array_push($yourarray, rtrim(array_pop($yourarray), ' **and**'));
var_dump($yourarray);

Outputs

array(2) {
  [0]=>
  string(41) "date = '2012-02-22' and time ='08:49' and"
  [1]=>
  string(42) "date = '2012-02-22' and time ='08:49' and "
}

array(2) {
  [0]=>
  string(41) "date = '2012-02-22' and time ='08:49' and"
  [1]=>
  string(37) "date = '2012-02-22' and time ='08:49'"
}

So the last and in the last element is removed.

Try the following,

array_push(yourarray, rtrim(array_pop(yourarray), ' and'));

It will pop the last element, trim away the " and" at the end and then re-add the modified element to the array.

For example,

$yourarray = array("date = '2012-02-22' and time ='08:49' and",  
             "date = '2012-02-22' and time ='08:49' and ");

var_dump($yourarray);
array_push($yourarray, rtrim(array_pop($yourarray), ' and'));
var_dump($yourarray);

outputs the following

array(2) {
  [0]=>
  string(41) "date = '2012-02-22' and time ='08:49' and"
  [1]=>
  string(42) "date = '2012-02-22' and time ='08:49' and "
}

array(2) {
  [0]=>
  string(41) "date = '2012-02-22' and time ='08:49' and"
  [1]=>
  string(37) "date = '2012-02-22' and time ='08:49'"
}

So, the last " and" in the last element has been removed.

simplified answer
Source Link
inquam
  • 12.9k
  • 17
  • 66
  • 106

Simply

array_pop(yourarray)

This pops and returns the last value of the array, shortening the array by one element. If array is empty NULL is returned.

EDIT

I just noticed you didn't want to get rid of the last element but a word in the last element.

But tryTry

array_push(yourarray, rtrim(array_pop(yourarray), ' **and**'));

Haven't tried the code but it shouldIt will pop the last element, trim away the and at the end and then re-add it to the array.

Update

Tested now and seem to produce the output you want.

Example

$yourarray = array("date = '2012-02-22' and time ='08:49' and",  
             "date = '2012-02-22' and time ='08:49' and ");

var_dump($yourarray);
array_push($yourarray, rtrim(array_pop($yourarray), ' **and**'));
var_dump($yourarray);

Outputs

array(2) {
  [0]=>
  string(41) "date = '2012-02-22' and time ='08:49' and"
  [1]=>
  string(42) "date = '2012-02-22' and time ='08:49' and "
}

array(2) {
  [0]=>
  string(41) "date = '2012-02-22' and time ='08:49' and"
  [1]=>
  string(37) "date = '2012-02-22' and time ='08:49'"
}

So the last and in the last element is removed.

Simply

array_pop(yourarray)

This pops and returns the last value of the array, shortening the array by one element. If array is empty NULL is returned.

EDIT

I just noticed you didn't want to get rid of the last element but a word in the last element.

But try

array_push(yourarray, rtrim(array_pop(yourarray), ' **and**'));

Haven't tried the code but it should pop the last element, trim away the and at the end and then re-add it to the array.

Update

Tested now and seem to produce the output you want.

Example

$yourarray = array("date = '2012-02-22' and time ='08:49' and",  
             "date = '2012-02-22' and time ='08:49' and ");

var_dump($yourarray);
array_push($yourarray, rtrim(array_pop($yourarray), ' **and**'));
var_dump($yourarray);

Outputs

array(2) {
  [0]=>
  string(41) "date = '2012-02-22' and time ='08:49' and"
  [1]=>
  string(42) "date = '2012-02-22' and time ='08:49' and "
}

array(2) {
  [0]=>
  string(41) "date = '2012-02-22' and time ='08:49' and"
  [1]=>
  string(37) "date = '2012-02-22' and time ='08:49'"
}

So the last and in the last element is removed.

Try

array_push(yourarray, rtrim(array_pop(yourarray), ' **and**'));

It will pop the last element, trim away the and at the end and then re-add it to the array.

Example

$yourarray = array("date = '2012-02-22' and time ='08:49' and",  
             "date = '2012-02-22' and time ='08:49' and ");

var_dump($yourarray);
array_push($yourarray, rtrim(array_pop($yourarray), ' **and**'));
var_dump($yourarray);

Outputs

array(2) {
  [0]=>
  string(41) "date = '2012-02-22' and time ='08:49' and"
  [1]=>
  string(42) "date = '2012-02-22' and time ='08:49' and "
}

array(2) {
  [0]=>
  string(41) "date = '2012-02-22' and time ='08:49' and"
  [1]=>
  string(37) "date = '2012-02-22' and time ='08:49'"
}

So the last and in the last element is removed.

added 762 characters in body
Source Link
inquam
  • 12.9k
  • 17
  • 66
  • 106

Simply

array_pop(yourarray)

This pops and returns the last value of the array, shortening the array by one element. If array is empty NULL is returned.

EDIT

I just noticed you didn't want to get rid of the last element but a word in the last element.

But try

array_push(yourarray, rtrim(array_pop(yourarray), ' **and**'));

Haven't tried the code but it should pop the last element, trim away the and at the end and then re-add it to the array.

Update

Tested now and seem to produce the output you want.

Example

$yourarray = array("date = '2012-02-22' and time ='08:49' and",  
             "date = '2012-02-22' and time ='08:49' and ");

var_dump($yourarray);
array_push($yourarray, rtrim(array_pop($yourarray), ' **and**'));
var_dump($yourarray);

Outputs

array(2) {
  [0]=>
  string(41) "date = '2012-02-22' and time ='08:49' and"
  [1]=>
  string(42) "date = '2012-02-22' and time ='08:49' and "
}

array(2) {
  [0]=>
  string(41) "date = '2012-02-22' and time ='08:49' and"
  [1]=>
  string(37) "date = '2012-02-22' and time ='08:49'"
}

So the last and in the last element is removed.

Simply

array_pop(yourarray)

This pops and returns the last value of the array, shortening the array by one element. If array is empty NULL is returned.

EDIT

I just noticed you didn't want to get rid of the last element but a word in the last element.

But try

array_push(yourarray, rtrim(array_pop(yourarray), ' **and**'));

Haven't tried the code but it should pop the last element, trim away the and at the end and then re-add it to the array.

Simply

array_pop(yourarray)

This pops and returns the last value of the array, shortening the array by one element. If array is empty NULL is returned.

EDIT

I just noticed you didn't want to get rid of the last element but a word in the last element.

But try

array_push(yourarray, rtrim(array_pop(yourarray), ' **and**'));

Haven't tried the code but it should pop the last element, trim away the and at the end and then re-add it to the array.

Update

Tested now and seem to produce the output you want.

Example

$yourarray = array("date = '2012-02-22' and time ='08:49' and",  
             "date = '2012-02-22' and time ='08:49' and ");

var_dump($yourarray);
array_push($yourarray, rtrim(array_pop($yourarray), ' **and**'));
var_dump($yourarray);

Outputs

array(2) {
  [0]=>
  string(41) "date = '2012-02-22' and time ='08:49' and"
  [1]=>
  string(42) "date = '2012-02-22' and time ='08:49' and "
}

array(2) {
  [0]=>
  string(41) "date = '2012-02-22' and time ='08:49' and"
  [1]=>
  string(37) "date = '2012-02-22' and time ='08:49'"
}

So the last and in the last element is removed.

Misread question... updated answer
Source Link
inquam
  • 12.9k
  • 17
  • 66
  • 106
Loading
Source Link
inquam
  • 12.9k
  • 17
  • 66
  • 106
Loading