将PHP代码转换为Vb.net

时间:2018-05-24 13:20:46

标签: php vb.net

我正面临vb.net的问题,关于如何将数据放入多维数组并循环遍历它并将结果打印到字符串

现在我在php中解决它,但我不能在vb.net中做到这一点我不熟悉vb.net也是多维数组

这是我的PHP脚本

$first_dimensionarray = array();//empty array
            $second_dimensionarray = array();//empty array
            for($i = 0; $i < count ( $data); $i ++){
            //loop through the data
            $second_dimensionarray = array(strtotime($data['date']),  $data['name']  , $data['phone'] , $data['email'] ,$data['date']  );//put every record as array in second_dimensionarray
            array_push( $first_dimensionarray, $second_dimensionarray ); //push second_dimensionarray inside the first_dimensionarray 
            }
            sort( $first_dimensionarray ); //sort $first_dimensionarray by the first element   >> (strtotime($data['date'])
            $arraycheck = array(); //another array to put the names into it
            for($i = 0; $i < count ( $first_dimensionarray ); $i ++)// loop through the new dimensional array --first_dimensionarray
            { 
                if(!in_array($first_dimensionarray[$i][1],$arraycheck ))//check if name exsit in arraycheck 
                {     
                        $arraycheck[] =$first_dimensionarray[$i][1];// if not then add it to arraycheck 
                        unset($first_dimensionarray[$i][0]);//remove the first index (the purpose for this element (date) is to sort all Data by  date)
                        $string .= implode( ",", $first_dimensionarray[$i] );
                        $string .= "\n";

                }
            }

这是我在vb.net中尝试过的

   For i = 0 To ata.Count 
              second_dimensionarray(i) = (strtotime($data('date')),$data('name'),$data('phone'),$data('email'),$data('date')}
              Array.Copy(second_dimensionarray, first_dimensionarray, 13)
              Next i

请有人帮我在vb.net中重写这个php代码吗?

1 个答案:

答案 0 :(得分:0)

我不确定您提供的数据是什么,因此我只使用DataTable作为示例。首先,创建一个类(或结构)。 VB编译器将为每个属性创建getter和setter以及backer字段。当然,如果你需要getter或setter中的代码,你可以拼写出来。还将创建默认构造函数。 如果使用通用列表而不是数组,则无需定义大小。遍历数据,首先创建类的新实例,然后为属性赋值。然后将实例添加到列表中。获得列表后,您可以轻松地对属性进行排序和访问。

Public Class Form1
Private Sub ConvertPHP()
        Dim strData As DataTable
    Dim lstPerson As New List(Of Person)
    For i As Integer = 0 To strData.Rows.Count - 1
        Dim p As New Person()
        p.Time = CDate(strData.Rows(i)("date")).ToShortTimeString
        p.Name = strData.Rows(i)("name").ToString
        p.Phone = strData.Rows(i)("phone").ToString
        p.Email = strData.Rows(i)("email").ToString
        p.PDate = CDate(strData.Rows(i)("date"))
        p.Children = {"Mathew", "Mark", "Luke", "John"}
        lstPerson.Add(p)
    Next
    Debug.Print(lstPerson.Item(0).Children(0))
    Dim lstSortedByName As List(Of Person) = lstPerson.OrderBy(Function(p) p.Name).ToList
    For i As Integer = 0 To lstPerson.Count - 1
        If String.IsNullOrWhiteSpace(lstPerson.Item(0).Name) Then
            'Do something
        End If
    Next
End Sub
End Class
Class Person
    Public Property Time As String
    Public Property Name As String
    Public Property Phone As String
    Public Property Email As String
    Public Property PDate As DateTime
    Public Property Children As String()
End Class