-1

I have some code that pushes values into a PHP array via array_push(). However, when I do print_r() on the array, it skips a key, and the ending result looks like this:

Array ( 
  [0] => appease 
  [1] => cunning 
  [2] => derisive 
  [3] => effeminate 
  [4] => grievance 
  [5] => inadvertently miscreants 
  [7] => ominous 
  [8] => resilient 
  [9] => resolute 
  [10] => restrain 
  [11] => superfluous 
  [12] => trudged 
  [13] => undiminished 
)

As you can see, it skipped the 6th value, and moved onto the next one. For some reason though, if I call the foreach() method on the array, it stops after "grievance", or index 4. Does anyone know why it does this?

Edit: if I do echo $words[6], it prints the value of the 6th index correctly.

Edit 2: here's my code:

$return = file_get_contents($target_file);
$arr = explode('<U><B>', $return);
$a = 1;

$words = [];

foreach($arr as $pos) {
    $important = substr($arr[$a], 0, 20);
    $arr2 = explode("</B></U>",$important);
    array_push($words,strtolower(trim($arr2[0])));
    $a++;
} 

Contents of the file are:

<U><B>appease</B></U><U><B>cunning</B></U><U><B>derisive</B></U><U><B>effeminate</B></U><U><B>grievance</B></U><U><B>inadvertently</B></U><U><B>miscreants</B></U><U><B>ominous</B></U><U><B>resilient</B></U><U><B>resolute</B></U><U><B>restrain</B></U><U><B>superfluous</B></U><U><B>trudged</B></U><U><B>undiminished</B></U>

*removed some irrelevant file content for easier readability

Thomas
  • 11
  • 4

4 Answers4

1

i wrote something simplier:

<?php
$return="<U><B>appease</B></U><U><B>cunning</B></U><U><B>derisive</B></U><U><B>effeminate</B></U><U><B>grievance</B></U><U><B>inadvertently</B></U><U><B>miscreants</B></U><U><B>ominous</B></U><U><B>resilient</B></U><U><B>resolute</B></U><U><B>restrain</B></U><U><B>superfluous</B></U><U><B>trudged</B></U><U><B>undiminished</B></U>";

$arr = explode('<U><B>', $return);


$words = [];

foreach($arr as $pos) {
    if(!empty($pos)){
    $words[]=strip_tags($pos);
    }
} 


echo '<pre>';
print_r($words);

demo: http://codepad.viper-7.com/XeUWui

0
$arr = "<U><B>appease</B></U><U><B>cunning</B></U><U><B>derisive</B></U><U><B>effeminate</B></U><U><B>grievance</B></U><U><B>inadvertently</B></U><U><B>miscreants</B></U><U><B>ominous</B></U><U><B>resilient</B></U><U><B>resolute</B></U><U><B>restrain</B></U><U><B>superfluous</B></U><U><B>trudged</B></U><U><B>undiminished</B></U>";

$arr = explode('<U><B>', $arr);
$a = 1;

foreach($arr as &$pos) 
{        
   if(!empty($pos))
   {
      $pos = str_replace("</B></U>","",$pos);   
   }
} 

print_r(array_filter($arr));
Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193
0

Might be overkill but there is always SimpleXml as well:

$arr = "<U><B>appease</B></U><U><B>cunning</B></U><U><B>derisive</B></U><U><B>effeminate</B></U><U><B>grievance</B></U><U><B>inadvertently</B></U><U><B>miscreants</B></U><U><B>ominous</B></U><U><B>resilient</B></U><U><B>resolute</B></U><U><B>restrain</B></U><U><B>superfluous</B></U><U><B>trudged</B></U><U><B>undiminished</B></U>";
$xml = new SimpleXmlElement('<root>' . $arr . '</root>');
$words = $xml->xpath('//B');

foreach ($words as $i =>$word) {
  printf("%d.\t%s\n", $i+1, $word);
}
prodigitalson
  • 60,050
  • 10
  • 100
  • 114
0

This is pretty simple with regex:

<?php

$words = "<U><B>appease</B></U><U><B>cunning</B></U><U><B>derisive</B></U><U><B>effeminate</B></U><U><B>grievance</B></U><U><B>inadvertently</B></U><U><B>miscreants</B></U><U><B>ominous</B></U><U><B>resilient</B></U><U><B>resolute</B></U><U><B>restrain</B></U><U><B>superfluous</B></U><U><B>trudged</B></U><U><B>undiminished</B></U>";

preg_match_all("#<U><B>(.*?)</B></U>#",$words,$matches);

print_r($matches[1]);
?>

Fiddle here