0

I have the following array which is being generated from a msqli query.

Array
(
    [0] => 21
    [1] => AAAAA
    [2] => BBBBB
    [3] => 1234567
    [4] => aaa
    [5] => bbb
    [6] => ccc
    [7] => ddd
    [8] => 2015-01-19 03:31:33
    [9] => 1
    [10] => 5
    [11] => 5, - not set -;9, - not set -;10, - not set -;11, - not set -;14, - not set -;19, 12;20, mm_cb_on;21, - not set -;27, Noe;28, Pena;62, mm_cb_off
)

Array
(
    [0] => 22
    [1] => AAAAA
    [2] => BBBBB
    [3] => 1234567
    [4] => aaa
    [5] => bbb
    [6] => ccc
    [7] => ddd
    [8] => 2015-01-19 03:31:33
    [9] => 1
    [10] => 6
    [11] => 5, - not set -;9, - not set -;10, - not set -;11, - not set -;14, - not set -;20, mm_cb_on;21, - not set -;
)

Array
(
    [0] => 23
    [1] => AAAAA
    [2] => BBBBB
    [3] => 1234567
    [4] => aaa
    [5] => bbb
    [6] => ccc
    [7] => ddd
    [8] => 2015-01-19 03:31:33
    [9] => 1
    [10] => 7
    [11] => 5, - not set -;11, - not set -;14, - not set -;19, 23;20, mm_cb_on;21, - not set -;27, Noe;28, Pena;62, mm_cb_off
)

I am looping through this data like so:

while ($row = mysqli_fetch_array($data)) {
    echo "<td nowrap>" .$row{'first_name'}. "</td>
          <td nowrap>" .$row{'last_name'}. "</td>
          .......";
}

When I get to the last key in the array, I am looping through that data, and exploding like this:

while ($row = mysqli_fetch_array($data)) {
    echo "<td nowrap>" .$row{'first_name'}. "</td>
          <td nowrap>" .$row{'last_name'}. "</td>
          .......";
    $custom_fields = $row[11];
    $fields = explode(";", $custom_fields);
    foreach ($fields as $keys) {
        $key = explode(',', $keys);

        var_dump($key);
    }
}

That var dump produces the following array structure (for the first key in the previous array):

Array
(
    [0] => 5
    [1] =>  - not set -
)
Array
(
    [0] => 9
    [1] =>  - not set -
)
Array
(
    [0] => 10
    [1] =>  - not set -
)
Array
(
    [0] => 11
    [1] =>  - not set -
)
Array
(
    [0] => 14
    [1] =>  - not set -
)
Array
(
    [0] => 19
    [1] =>  12
)
Array
(
    [0] => 20
    [1] =>  mm_cb_on
)
Array
(
    [0] => 21
    [1] =>  - not set -
)
Array
(
    [0] => 27
    [1] =>  Noe
)
Array
(
    [0] => 28
    [1] =>  Pena
)
Array
(
    [0] => 62
    [1] =>  mm_cb_off
)

I am trying to create conditionals like so:

if (isset($key[0]) && $key[0] == 19) { // this produce none for every row
    $prov_id = $key[1];
} else {
    $prov_id = 'None';
}

if ($key[0] == 27) {
    $fm2_fname = $key[1];
} else {
    $fm2_fname  = '';
}

if ($key[0] == 28) {
    $fm2_lname = $key[1];
} else {
    $fm2_lname  = '';
}

The problem I am having is in the loop some of the items do not have $key[0] == "19" ... How would I handle this inside the foreach loop? Any help is greatly appreciated.

12
  • What with the items do not have $key[0] == "19" ? Commented Aug 10, 2015 at 20:42
  • 1
    if $key[0] doesn't exist, then your explode failed. so... isset($key[0])? Commented Aug 10, 2015 at 20:42
  • Thanks @Marc B. I understand that, but $key[0] will always exist somewhere in the array it just wont alway equal 19 ... So isset will alway be true. Commented Aug 10, 2015 at 20:44
  • then what exactly is the problem? Commented Aug 10, 2015 at 20:44
  • I need to grab the value of $key[1] where $key[0] == 19 and if there is not a $key[0] that equals 19, I need to provide an empty value. Commented Aug 10, 2015 at 20:47

1 Answer 1

1
if (isset($key[0]) && $key[0] == 19) { // this produce none for every row
    $prov_id = $key[1];
} else {
    $prov_id = 'None';
}

if ($key[0] == 27) {
    $fm2_fname = $key[1];
} else {
    $fm2_fname  = '';
}

if ($key[0] == 28) {
    $fm2_lname = $key[1];
} else {
    $fm2_lname  = '';
}

Because you have an else on each statement, that value will be set as it is looping, so in your situation it is setting the variable to 'None' after setting it to $key[1].

Instead get rid of the } else { And change variables to the following so each row has its own values:

$row['prov_id']
$row['fm2_fname']
$row['fm2_lname']

So the final code now will be like this:

if ($key[0] == 19)
    $row['prov_id'] = $key[1];

if ($key[0] == 27) 
    $row['fm2_fname'] = $key[1];

if ($key[0] == 28) 
    $row['fm2_lname'] = $key[1];

Outside the foreach loop do this for each variable

if (!isset($row['prov_id'])) $row['prov_id'] = '';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.