Jake
Jake

Reputation: 205

PHP, checking values from My-Sql data

$result = $this->db->query($query)->result_array();


        if($result[9]['b_code'] != FALSE)
            {

        $result_array = array(
                            '0' => $result[0]['b_code'],
                            '1' => $result[1]['b_code'],
                            '2' => $result[2]['b_code'],
                            '3' => $result[3]['b_code'],
                            '4' => $result[4]['b_code'],
                            '5' => $result[5]['b_code'],
                            '6' => $result[6]['b_code'],
                            '7' => $result[7]['b_code'],
                            '8' => $result[8]['b_code'],
                            '9' => $result[9]['b_code']
                        );


                return $result_array;
            }
        else
            return FALSE;

It extracts 10 data in rows and make it a new array as $result_array;

Sometimes, it won't have 10 results and may extract 5 or 8.

So, I wanna check wether there is a value for last one, which is $result[9]['b_code] or not.

Should I use isset?

Upvotes: 0

Views: 99

Answers (5)

hakre
hakre

Reputation: 197692

Should I use isset?

Yes, isset­Docs is the right language construct if you want to test if a value is set (e.g. set and not NULL­Docs).

In your particular case you can also check the length of $result, that is done with count­Docs:

$numberOfElements = count($result);

Some example code with isset:

if (!isset($result[9]))
{
    return FALSE;
}

return array_map(function($v) {return $v['b_code'];}, $result);

Note that in this example we're preventing to repeat ourselves 10 times only to copy each b_code element. Instead, this is done with array mapping, see array_map­Docs.

A probably more readable variant, this time with count:

if (10 != count($result))
{
    return FALSE;
}

$resultArray = array();
foreach ($result as $v)
{
    $resultArray[] = $v['b_code'];
}
return $resultArray;

This variant is making use of foreach­Docs to prevent assigning 10 elements one by one.

Upvotes: 2

safarov
safarov

Reputation: 7804

You dont need to assign whole array again to new one.

 $result_array = $this->db->query($query)->result_array();

   if(array_key_exist($result_array, 9) && isset($result_array[9]['b_code'])) {
      return $result_array;
   }

 return false;

Upvotes: 0

Daan
Daan

Reputation: 3348

Can't you simply do this?

$result = $this->db->query($query)->result_array();

$result_array = array();

foreach($result as $key=>$value) { 
    $result_array[$key] = $value['b_code'];
}

This should work for any number of results.

Upvotes: 0

aifarfa
aifarfa

Reputation: 3939

$last_index = sizeof($result) - 1;

if(!$result[$last_index]['b_code']){
 //...
}

Upvotes: 0

Rukmi Patel
Rukmi Patel

Reputation: 2561

you can check it by count function too ..

if(count($result) == 10 && $result[9]['b_code'] != '') {
   // your code...

}

Upvotes: 0

Related Questions