Starcute Jones
Starcute Jones

Reputation: 23

assosiate two arrays with php

I have learned how to combine 2 arrays, so I made the first $array1 that will be the names of loop fields and the 2nd $array2 will be the values of field it selft, as following

$array1 = array ( Field One, Field Two, Field Three );

then the 2nd Array derived from mysql table e.g.

$array2 = $conn->db_FetchResult("SELECT id FROM %sfields ...);

the result view of $array2 such as

$array2 = array (field_1 => '1', field_2 => '2', field_3 => '3' );

how to combine both those arrays?

Expected output:

$array3 = array (Field One => '1', Field Two => '2', Field Three => '3' );

I have Tried use array_combine, array_merge

print_r(array_combine($array1,$array2) as $array => $value);

but can not acomplish it, I have not found the exact same example, there might be willing to help, thanks

Upvotes: 2

Views: 38

Answers (1)

Sahil Gulati
Sahil Gulati

Reputation: 15141

PHP code demo

<?php
$array1 = array ( "Field One", "Field Two", "Field Three" );
$array2 = array ("field_1" => '1', "field_2" => '2', "field_3" => '3' );

$c=array_combine($array1,$array2);
print_r($c);

Output:

Array
(
    [Field One] => 1
    [Field Two] => 2
    [Field Three] => 3
)

Upvotes: 1

Related Questions