0

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);
2
  • 1
    Please provide a more detailed example of how your expected output would be. Commented Jul 19, 2018 at 19:12
  • I got it i tried with other code it works has expected thanks to all Commented Jul 20, 2018 at 14:01

4 Answers 4

1

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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

HI i tried this but it showing like this [code] Array ( [CONFIRMATION_NUM] => CGDSAC [BOOKING_AGENT] => otacallcentre )[/code] only one is showing
Then your sample arrays are incorrect. I updated with a guess.
No array is correct i am getting with that details
0

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.

1 Comment

isn't possible to get all?
0
$final = array();
foreach ($tablecolumnarray as $n=>$column) {
  $final[$column][] = $tabledatacsvcolumnarray[$n] ;
}
print_r($final) ;

2 Comments

HI Chris o tried this but it showing like this <code> Array ( [CONFIRMATION_NUM] => CGDSAC [BOOKING_AGENT] => otacallcentre )</code> only one is showing
Edited to add [], creating a sub-array. You need to be clearer in what you're expecting so see.
0

Try

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.