如何使用PHP循环使用foreach循环数组?

Im am inside a while loop and need to grab some data in array produced by the loop. My array looks like this:

print_r($fields); //produces this

Array
(
    [0] => 5, - not set -
    [1] => 9, - not set -
    [2] => 10, - not set -
    [3] => 11, - not set -
    [4] => 14, - not set -
    [5] => 19, 12
    [6] => 20, mm_cb_on
    [7] => 21, - not set -
    [8] => 27, Noe
    [9] => 28, Pena
    [10] => 62, mm_cb_off
)
Array
(
    [0] => 5, Two Individuals
    [1] => 9, - not set -
    [2] => 10, - not set -
    [3] => 11, - not set -
    [4] => 14, - not set -
    [5] => 20, mm_cb_off
    [6] => 21, 1
    [7] => 27, SecPartF
    [8] => 28, SecPartL
    [9] => 30, testname3
    [10] => 31, last3
    [11] => 33, 
    [12] => 34, 
    [13] => 36, 
    [14] => 37, 
    [15] => 39, 
    [16] => 40, 
    [17] => 42, 
    [18] => 43, 
    [19] => 45, 
    [20] => 46, 
    [21] => 48, 
    [22] => 49, 
    [23] => 51, 
    [24] => 52, 
)
Array
(
    [0] => 5, - not set -
    [1] => 9, December
    [2] => 10, 4
    [3] => 11, 2014
    [4] => 12, 1915 Canterbury Street
    [5] => 13, Austin
    [6] => 14, Texas
    [7] => 15, 78702
    [8] => 19, 14
    [9] => 20, mm_cb_on
    [10] => 21, - not set -
    [11] => 62, mm_cb_off
)

Each key has a key and value itself like so:

foreach ($fields as $keys) {

    $key = explode(',', $keys);

}

I am trying to set values for each item in the array, but need to check if something exists first. For example:

if ($key[0] == 19) {

    $prov_id = $key[1];

} else {

    $prov_id = '';

}

The problem I am running into is that not every array from $key[0] has "19" ... How do I go about setting an empty value if it does not exist? Am I mising something?

Perhaps you need to check if there's a value at that array index by using isset() and if not, set your default value.

Still be nice to clean up the array, make the first tokenized value simply the key for the array and the value just the value, to not need to think about that explode.

Anyway, try this:

<?php
$uglyArray = ['1,', '2,notset','3,imaset'];

foreach ($uglyArray as $k => $v) {
      $parts = explode(',', $v);      
      var_dump($parts);
      if (empty($parts[1]) || $parts[1] == 'notset') {
        //not set
        $uglyArray[$k] = "{$parts[0]}, A new value";
      } else {
          //has value
          continue;
      }            
}

var_dump($uglyArray);
?>

Output:

array(2) {
  [0]=>
  string(1) "1"
  [1]=>
  string(0) ""
}

array(2) {
  [0]=>
  string(1) "2"
  [1]=>
  string(6) "notset"
}

array(2) {
  [0]=>
  string(1) "3"
  [1]=>
  string(6) "imaset"
}


array(3) {
  [0]=>
  string(14) "1, A new value"
  [1]=>
  string(14) "2, A new value"
  [2]=>
  string(8) "3,imaset"
}

To clean things up consider:

    $uglyArray = ['2,', '4,notset','6,imaset'];
    $cleanArray = [];
    foreach ($uglyArray as $k => $v) {
          $parts = explode(',', $v);                  
          if (empty($parts[1]) || $parts[1] == 'notset') {
            //not set
            $cleanArray[$parts[0]] = "A new value";
          } else {
              //has value
              $cleanArray[$parts[0]] = $parts[1];
              continue;
          }            
    }


 array(3) {
  [2]=>
  string(11) "A new value"
  [4]=>
  string(11) "A new value"
  [6]=>
  string(6) "imaset"
}