0
$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?

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

you can check it by count function too ..

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

}

Comments

0
$last_index = sizeof($result) - 1;

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

Comments

0

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.

Comments

0

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;

2 Comments

@hakre seems i misunderstand question
Well the idea is still good to map instead of to copy 10 elements one by one.

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.