star rock
star rock

Reputation: 3

Issue with Combine array php

I am trying to combine two arrays in one array, i want to get value and need to keep it has a key for second array.

example

Array (
     [CONFIRMATION_NUM] => DBSUUA
      )

**

here my code 
first array

**

Array ( 
    [0] => CONFIRMATION_NUM
    [1] => BOOKING_AGENT
    [2] => CONFIRMATION_NUM 
    [3] => BOOKING_AGENT 
    [4] => CONFIRMATION_NUM 
    [5] => BOOKING_AGENT 
    [6] => CONFIRMATION_NUM 
    [7] => BOOKING_AGENT 
    [8] => CONFIRMATION_NUM 
    [9] => BOOKING_AGENT 
    [10] => CONFIRMATION_NUM 
    [11] => BOOKING_AGENT
    [12] => CONFIRMATION_NUM
 )

second array is

Array (
 [0] => DBSUUA 
 [1] => faras-nmdc 
 [2] => UKAZZQ 
 [3] => yco-lmcy 
 [4] => QVTUTS 
 [5] => sohail-npcc 
 [6] => HGQQEF 
 [7] => masood-muss 
 [8] => HOHCFQ 
 [9] => yco-lmcy 
 [10] => JSDUIT 
 [11] => otacallcentre 
 [12] => LHLHWL
)

I tried with this code in this code $tablecolumnsarray is first array and $tabledatacsvcolumnarray is the second array name

$keys = array_keys($tablecolumnsarray);
                    $final=array();

                    foreach ($tabledatacsvcolumnarray as $v) {
                      $final[]=array_combine($keys,$v);
                    }

                    print_r($final);

Upvotes: 0

Views: 65

Answers (4)

AbraCadaver
AbraCadaver

Reputation: 79024

Unless there's something that you're not showing, no need to loop:

$final = array_combine($tablecolumnsarray, $tabledatacsvcolumnarray);

If $tabledatacsvcolumnarray is actually multidimensional then:

foreach($tabledatacsvcolumnarray as $values) {
    $final[] = array_combine($tablecolumnsarray, $values);
}

Upvotes: 1

Karol Samborski
Karol Samborski

Reputation: 2965

As others have said, it is enough to use: $final = array_combine($tablecolumnsarray, $tabledatacsvcolumnarray); to achieve what you want.

But the reason that you're only getting two keys this way is the first array. Take a look at its values:

Array ( 
    [0] => CONFIRMATION_NUM 
    [1] => BOOKING_AGENT 
    [2] => CONFIRMATION_NUM 
    [3] => BOOKING_AGENT 
    [4] => CONFIRMATION_NUM 
    [5] => BOOKING_AGENT 
    [6] => CONFIRMATION_NUM 
    [7] => BOOKING_AGENT 
    [8] => CONFIRMATION_NUM 
    [9] => BOOKING_AGENT 
   [10] => CONFIRMATION_NUM 
   [11] => BOOKING_AGENT 
   [12] => CONFIRMATION_NUM
)

There cannot be two keys with the same name in the array. But there are only two different values in that array so the final array will contain only two (probably last) elements.

Upvotes: 0

Joe Horn
Joe Horn

Reputation: 443

Try

$final = array();
foreach ($tablecolumnsarray as $k => $column) {
  $final[$column] = $tabledatacsvcolumnarray[$k] ;
}
print_r($final) ;

Upvotes: 0

Chris J
Chris J

Reputation: 7779

$final = array();
foreach ($tablecolumnarray as $n=>$column) {
  $final[$column][] = $tabledatacsvcolumnarray[$n] ;
}
print_r($final) ;

Upvotes: 0

Related Questions