php擴充:如何訪問php數組

來源:互聯網
上載者:User
     在php擴充中,時常需要接受php類型的數組作為參數,php數組的參數都是zval類型的,並不適合在擴充中方便的使用,一般都要提前轉換成c或cpp中的資料類型。首先看一個轉換的例子:

void convert_to_vector(const zval * vals, vector<string> &valList) ...{
    // create the list to write
    HashPosition         pos;
    zval               **z_val = NULL;
    string               value;

    zend_hash_internal_pointer_reset_ex( Z_ARRVAL_P( vals ), &pos );

    while ( zend_hash_get_current_data_ex( Z_ARRVAL_P( vals ), (void **)&z_val, &pos ) == SUCCESS ) ...{
        convert_to_string_ex( z_val );
        value = Z_STRVAL_PP(z_val);

        valList.push_back(value);

        zend_hash_move_forward_ex( Z_ARRVAL_P( vals ), &pos );
    }
}

     上述列子是把php的數群組轉換成vector<string>的類型。HashPosition 是一個指標,通過zend_hash_internal_pointer_reset_ex( Z_ARRVAL_P( vals ), &pos );方法使得pos指向zval數組的第一個元素,然後通過while迴圈中的zend_hash_get_current_data_ex( Z_ARRVAL_P( vals ), (void **)&z_val, &pos ) 方法取得pos所指位置的元素值,儲存在z_val指標中。
      convert_to_string_ex( z_val ),轉換z_val中包含的實際資料為字串,value = Z_STRVAL_PP(z_val) 則獲得此字串的值,然後push_back到valList中。zend_hash_move_forward_ex( Z_ARRVAL_P( vals ), &pos );則移動pos到下一個元素位置。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.