Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return first key in foreach

Tags:

arrays

post

php

key

I have problem in returning the first key of array in foreach. I can return the $key but i cant figure out on returning the first $key only.

<td>
    <div align="center">
        <span class="formlist">
            <select id="plant" name="plant[]" class="form-control" multiple="multiple">
            <?php 
                $query_plant = "SELECT * FROM plant WHERE plant_enable=1 ORDER BY plant_name";
                $rs_plant = DB_Query($query_plant); 
                while ($row_plant = DB_FetchRow($rs_plant)) {
                    $plant.='<option name='.$row_plant["plant_shortname"].' value='.$row_plant["plant_id"].'>' .$row_plant["plant_name"].' ['.$row_plant["plant_id"].']</option>';
                }   
                mysql_free_result($rs_plant);
                echo $plant;
            ?>
            </select>
       </span>
    </div>
</td>

if(isset($_POST['plant'])) {        
    $checkbox1 = $_POST['plant']; 
    $chk="";
    $stf_sql = "SELECT * FROM test_plant WHERE staff_id = '".$STAFF_ID."'";
    $stf_res = DB_Query($stf_sql);      
    if(DB_RowsReturned($stf_res) > 0) {
       $del_sql = "DELETE FROM test_plant WHERE staff_id = '".$STAFF_ID."'";
       $del_res = DB_Query($del_sql);
    }       
    foreach($checkbox1 as $chk1)  
    {  
        $in_ch="insert into test_plant(staff_id, plant_name, submit_dt) values ('$STAFF_ID','$chk1', Now())";  
        $in_res = DB_Query($in_ch);                
        $abc = mysql_query("SELECT * FROM test_plant");    
        while($abc_row = mysql_fetch_assoc($abc)) {
            foreach($abc_row as $key => $value) {
                echo $key; //return first key here
            }          
        }      
    }       
} else {
    echo "OK"; 
}

I have tried returning $key[0] but it returns the first letter only. I want to return 'p_id'

My table structure :

like image 479
NFSJ Avatar asked May 15 '17 02:05

NFSJ


People also ask

How do you get the first value in foreach?

Method 1: It is the naive method inside foreach loop to find iteration. Use a counter variable and check when the counter value is zero then it is the first iteration and when the counter value is length-1 then it is the last iteration.

How do you get the first key of an array?

How do I find the first key of an array? Alternativly, you can also use the reset() function to get the first element. The reset() function set the internal pointer of an array to its first element and returns the value of the first array element, or FALSE if the array is empty.

How do you find array keys?

The array_keys() function is used to get all the keys or a subset of the keys of an array. Note: If the optional search_key_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the array are returned.

How do you check if a key exists in an array PHP?

PHP array_key_exists() Function The array_key_exists() function checks an array for a specified key, and returns true if the key exists and false if the key does not exist.


1 Answers

Like this,

$keys = array_keys($checkbox1);
print_r($keys); // for all keys
echo $keys[0]; // It will echo first key of the array.
like image 185
Naga Avatar answered Nov 09 '22 02:11

Naga