user1542894
user1542894

Reputation: 105

Combine two arrays, set one as keys and the other as values

Solved:

Thanks for the answers, but I found another way to solve my particular problem. I didn't mention what I needed this combined array for. I wanted to get the value of $exbet where key $ex matched a variable from a mysql query. Instead I did the following using FIELD() in the sql:

$mid=$pred['mid'] ///this is from a previous query

$mid = str_replace('|', ',', $mid);

SELECT * FROM matches m  
LEFT JOIN combo c ON c.mid 
LIKE CONCAT('%|',m.id,'|%') 
WHERE c.mid=:mid 
ORDER BY FIELD(m.id".$mid.")

So that the fetch_array was sorted the way it was in $mid (and not by id as default).

Then I just looped through $exbet getting the right mid / bet pair


Original post:

$exbet=explode('|',$tt['bet']);
$ex=explode('|',$tt['mid']);

I have these two arrays and I want to combine them, using $ex as keys and $exbet as values. I tried

$array = array_combine($ex,$exbet);

But it outputs nothing.

Here is $ex:

Array ( [0] => [1] => 8879 [2] => 8878 [3] => 8880 [4] => ) Array ( [0] => [1] => 8868 [2] => 8876 [3] => 8873 [4] => 8869 [5] => ) 

And $exbet:

Array ( [0] => [1] => P [2] => N [3] => X ) Array ( [0] => [1] => 1 [2] => 1X [3] => P [4] => 1 ) 

Now I see there are 2 arrays for each. I have to group the first one from $ex with the first from $exbet and so on.

Upvotes: 1

Views: 220

Answers (2)

GreensterRox
GreensterRox

Reputation: 7140

Assuming that $ex and $exbet are array of arrays and you want the combined array to look something like this:

array(7) {
  [8879]=>
  string(1) "P"
  [8878]=>
  string(1) "N"
  [8880]=>
  string(1) "X"
  [8868]=>
  int(1)
  [8876]=>
  string(2) "1X"
  [8873]=>
  string(1) "P"
  [8869]=>
  int(1)
}

Then this should do the job. I added some error checking to skip the nulls you have. If there is a null in the keyname or in the associated value in the other array then it is skipped.

<?php
$exbet=explode('|',$tt['bet']);
$ex=explode('|',$tt['mid']);

$combined_array = array();

foreach($ex as $top_level_key => $ex_array){
  $exbet_array = $exbet[$top_level_key];
  foreach($ex_array as $key => $keyName){
    if(!empty($keyName) && isset($exbet_array[$key]) && !empty($exbet_array[$key])){
      $combined_array[$keyName] = $exbet_array[$key];
    }
  }
}

var_dump($combined_array);

?>

Upvotes: 1

Dano
Dano

Reputation: 169

Your sub-arrays for $ex and $exbet do not have matching numbers of elements. $ex[0] contains 5 while $exbet[0] contains 4, same scenario for sub-arrays #1.

Per the documentation (http://php.net/manual/en/function.array-combine.php)

Errors/Exceptions

Throws E_WARNING if the number of elements in keys and values does not match.

Once you have that sorted out, you may want to account for the duplicate (and empty) keys that will be generated from $ex. Both sub-arrays of $ex have two null values, so the second null value will overwrite the first. This will come into play again if you intend to combine all of this into a one-dimensional array at the end and you still have duplicate key values. Given all of that, my approach would then be something like this:

foreach($ex as $id=>$v){
  $newarray[] = array_combine($ex[$id], $exbet[$id]);
}

And then if you want one final array with a single set of key=>value pairs, I would add this (using the + operator rather than array_merge to preserve numeric keys):

$finalarray = array();
foreach($newarray as $ct=>$v){ $finalarray += $newarray[$ct];}

Upvotes: 1

Related Questions