如何从多维数组中获取id作为键和电子邮件作为其值?

时间:2015-05-04 08:18:02

标签: php arrays multidimensional-array

我有如下面的多维数组

$records = array(
    array(
        'id' => 11,
        'first_name' => 'John',
        'last_name' => 'Doe',
        'email' => 'john@gmail.com'
    ),
    array(
        'id' => 12,
        'first_name' => 'Sally',
        'last_name' => 'Smith',
        'email' => 'sally@gmail.com'
    ),
    array(
        'id' => 13,
        'first_name' => 'Jane',
        'last_name' => 'Jones',
        'email' => 'jane@gmail.com'
    )
);

现在,我想将输出作为数组以id的形式作为键,并将电子邮件作为其值。

Array
(
    [11] => john@gmail.com
    [12] => sally@gmail.com
    [13] => jane@gmail.com
)

我想要它的短代码,不需要任何冗长的代码。

我用foreach

尝试过
$collect_data = array();
foreach($records as $key=>$data){
  $collect_data[$data['id']] = $data['email']
}

任何人都知道上面的短代码以达到我的要求。

3 个答案:

答案 0 :(得分:3)

我认为,您可以尝试使用php in-build功能来解决您的解决方案

$emails = array_column($records, 'email', 'id');
print_r($last_names);

您也可以参考以下链接。

Php In-build Function (array_column)

答案 1 :(得分:1)

这应该适合你:

idemail只有$collect_data = array_combine(array_column($records, "id"), array_column($records, "email")); 列,您可以使用array_combine()抓住该列。

Public Class Form1
    Private Function vld(ByVal ParamArray ctl() As Object) As Boolean
        For i As Integer = 0 To UBound(ctl)
            If ctl(i).text = "" Then
                ErrorProvider1.SetError(ctl(i), ctl(i).tag)
                Return False
                Exit For
            End If
        Next
        Return True
    End Function
    Dim cn As New OleDbConnection
    Dim cm As New OleDbCommand
    Dim da As OleDbDataAdapter
    Dim dt As New DataTable

    Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        cn.Close()
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TxtExamtime.Format = DateTimePickerFormat.Custom
        TxtExamtime.CustomFormat = "hh:MM tt"
        cn.ConnectionString = "provider=microsoft.jet.oledb.4.0; Data Source=C:\psave\New folder\save.xls;Extended Properties=Excel 8.0;"
        cn.Open()
        FillDataGridView("select ID, Family Name, Given Name, Gender, DOB, Exam Date, Exam Time, Street Name, House Nr, PLZ, City from [edit$]")

    End Sub

    Private Sub FillDataGridView(ByVal Query As String)
        da = New OleDbDataAdapter(Query, cn)
        dt.Clear()
        da.Fill(dt)
        With DataGridView1
            .DataSource = dt
            .Columns(0).HeaderText = "ID"
            .Columns(1).HeaderText = "Family Name"
            .Columns(2).HeaderText = "Given Name"
            .Columns(3).HeaderText = "Gender"
            .Columns(4).HeaderText = "DOB"
            .Columns(5).HeaderText = "Exam Date"
            .Columns(6).HeaderText = "Exam Time"
            .Columns(7).HeaderText = "Street Name"
            .Columns(8).HeaderText = "House Nr"
            .Columns(9).HeaderText = "PLZ"
            .Columns(10).HeaderText = "City"
            .Columns(10).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
        End With
    End Sub

    Private Sub BtnSearch_Click(sender As Object, e As EventArgs) Handles BtnSearch.Click
        Try
            FillDataGridView("select * from [edit$] where ID='" & TxtId.Text & "'")
            TxtFamilyname.Text = dt.Rows(0).Item(1)
            TxtGivenname.Text = dt.Rows(0).Item(2)
            TxtGender.Text = dt.Rows(0).Item(3)
            TxtDob.Text = dt.Rows(0).Item(4)
            TxtExamdate.Text = dt.Rows(0).Item(5)
            TxtExamtime.Text = dt.Rows(0).Item(6)
            TxtStreet.Text = dt.Rows(0).Item(7)
            TxtHouse.Text = dt.Rows(0).Item(8)
            TxtPlz.Text = dt.Rows(0).Item(9)
            TxtCity.Text = dt.Rows(0).Item(10)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Text)
        End Try
    End Sub

    Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click
        If vld(TxtId, TxtFamilyname, TxtGivenname, TxtGender, TxtDob, TxtExamdate, TxtExamtime, TxtStreet, TxtHouse, TxtPlz, TxtCity) = False Then
            Exit Sub
        Else

        End If

        Try
            With cm
                .Connection = cn
                .CommandText = "insert into [edit$]values('" & TxtId.Text & "','" & TxtFamilyname.Text & "','" & TxtGivenname.Text & "','" & TxtGender.Text & "','" & TxtDob.Text & "','" & TxtExamdate.Text & "','" & TxtExamtime.Text & "','" & TxtStreet.Text & "','" & TxtHouse.Text & "','" & TxtPlz.Text & "','" & TxtCity.Text & "' )"
                .ExecuteNonQuery()
            End With
            FillDataGridView("select * from [edit$]")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Text)
            Return
        End Try
        MsgBox("succefully Saved!", MsgBoxStyle.Information, Text)
    End Sub

    Private Sub BtnUpdate_Click(sender As Object, e As EventArgs) Handles Btnupdate.Click
        Try
            With cm
                .Connection = cn
                .CommandText = "Update from [edit$] set [Family Name] = '" & TxtFamilyname.Text & "' where ID ='" & TxtId.Text & "' and Given Name = '" & TxtGivenname.Text & "' and Gender = '" & TxtGender.Text & "'and DOB = '" & TxtDob.Text & "'and Exam Date'" & TxtExamdate.Text & "'and Exam Time = '" & TxtExamtime.Text & "'and Street Name = '" & TxtStreet.Text & "'and House Nr = '" & TxtHouse.Text & "'and PLZ = '" & TxtPlz.Text & "'and CITY = '" & TxtCity.Text & "'"
                .ExecuteNonQuery()
            End With
            FillDataGridView("select * from [edit$]")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, Text)
            Return
        End Try
        MsgBox("Succesfully updated!", MsgBoxStyle.Information, Text)
    End Sub

    Private Sub BtnClose_Click(sender As Object, e As EventArgs) Handles BtnClose.Click
        Close()
    End Sub

    Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles BtnClear.Click
        TxtId.Clear()
        TxtFamilyname.Clear()
        TxtGivenname.Clear()
        TxtStreet.Clear()
        TxtHouse.Clear()
        TxtPlz.Clear()
        TxtCity.Clear()
        'To see all the data in DataGridView
        FillDataGridView("select * from[edit$]")

    End Sub

    Private Sub BtnDelete_Click(sender As Object, e As EventArgs) Handles BtnDelete.Click
        Try
            With cm
                .Connection = cn
                .CommandText = "Delete from [edit$] where [Family Name] = '" & TxtFamilyname.Text & "' and ID ='" & TxtId.Text & "' and [Given Name] = '" & TxtGivenname.Text & "'and Gender = '" & TxtGender.Text & "'and DOB = '" & TxtDob.Text & "'and [Exam Date]'" & TxtExamdate.Text & "'and [Exam Time] = '" & TxtExamtime.Text & "'and [Street Name] = '" & TxtStreet.Text & "'and [House Nr] = '" & TxtHouse.Text & "'and PLZ = '" & TxtPlz.Text & "'and CITY = '" & TxtCity.Text & "'"
                .ExecuteNonQuery()
            End With
            MsgBox("Succesfully Deleted!", MsgBoxStyle.Information, Text)
            FillDataGridView("select * from [edit$]")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, Text)

        End Try
    End Sub
End Class

答案 2 :(得分:0)

为了兼容性,在PHP 5.5中引入了array_column,请考虑array_reduce

$output = array_reduce($records, function($memo, $item){
  $memo[$item['id'] = $item['email'];
  return $memo;
}, []);

我发现它比foreach更具表现力,但这是个人品味的问题。