-1

hi all i have 2 arrays

[0] => CD Alaves
[1] => Granada CF
[2] => Getafe
[3] => CD Leganes
[4] => Barcelona
[5] => Getafe
[6] => Atletico Madrid
[7] => Getafe
[8] => Sevilla
[9] => Athletic Bilbao
[10] => CD Leganes

and their corresponding values

[0] => 11
[1] => 11
[2] => 11
[3] => 11
[4] => 11
[5] => 10
[6] => 10
[7] => 10
[8] => 10
[9] => 10
[10] => 9

I am currently using

$teamdata=array_combine($clubs,$stats);


echo "<pre>";
print_r($teamdata);
echo "</pre>";

however it outputs as

[CD Alaves] => 1
[Granada CF] => 1
[Getafe] => 1
[CD Leganes] => 1
[Barcelona] => -
[Atletico Madrid] => 1
[Sevilla] => 1
[Athletic Bilbao] => -
[Real Betis] => 1
[Espanyol] => 1
[Osasuna] => 1
[Villarreal] => 1
[Real Madrid] => 1
[Levante] => 1

giving unique values on the left and 1s on the right

it should read

[CD Alves]=>11
[Granada CF]=>11
....

Thanks a lot to anyone who sees this and takes the time to respond.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
miikegunn
  • 1
  • 1
  • Please show a [mre] of your problem, including your actual array data (in _code_ form, not like you have currently shown them.) – CBroe May 04 '20 at 10:54
  • The array you are trying to use as the keys has duplicates, you can only have 1 key with a particular value in PHP. – Nigel Ren May 04 '20 at 11:10
  • What are you trying to do? Are you trying to print the array values like the print_r or are you trying to build a new array with the key => value. Depending on what you are going to do next the solution will be different. Give us some more context and the real code you used, just as CBroe said – Andreas May 04 '20 at 11:22

2 Answers2

0

As long as your two originating arrays are valid, then array_combine works just fine:

<?php
$arr1 = array(
    0 => "CD Alaves",
    1 => "Granada CF",
    2 => "Getafe",
    3 => "CD Leganes",
    4 => "Barcelona",
    5 => "Getafe",
    6 => "Atletico Madrid",
    7 => "Getafe",
    8 => "Sevilla",
    9 => "Athletic Bilbao",
    10 => "CD Leganes"
);

$arr2 = array(
    0 => 11,
    1 => 11,
    2 => 11,
    3 => 11,
    4 => 11,
    5 => 10,
    6 => 10,
    7 => 10,
    8 => 10,
    9 => 10,
    10 => 9
);

$res = array_combine($arr1, $arr2);
print_r($res);

// Gives:
Array
 (
    [CD Alaves] => 11
    [Granada CF] => 11
    [Getafe] => 10
    [CD Leganes] => 9
    [Barcelona] => 11
    [Atletico Madrid] => 10
    [Sevilla] => 10
    [Athletic Bilbao] => 10
)

A working example: http://sandbox.onlinephpfunctions.com/code/a6da80745eff9ff9b598fe4a2d7fbd8d71d3fb08

Even if your arrays are single values without explicit keys, it will work (php adds numeric indexes anyway)

<?php
$arr1 = array(
    "CD Alaves",
    "Granada CF",
    "Getafe",
    "CD Leganes",
    "Barcelona",
    "Getafe",
    "Atletico Madrid",
    "Getafe",
    "Sevilla",
    "Athletic Bilbao",
    "CD Leganes"
);

$arr2 = array(
    11,
    11,
    11,
    11,
    11,
    10,
    10,
    10,
    10,
    10,
    9
);

$res = array_combine($arr1, $arr2);
print_r($res);

// Prints the same output as above.
Stuart
  • 6,630
  • 2
  • 24
  • 40
  • Your working example is not working. You input 11 items and only get 8 in return. That is what OP says is the problem, so your code does not do anything. – Andreas May 04 '20 at 13:08
  • Yes, but it's not what OP wants. So is does that make the answer correct and/or useful? No. – Andreas May 04 '20 at 13:13
  • 1
    @Andreas if the OP has no idea what are they talking about, it's not the reason to blame the answer. – Your Common Sense Jul 23 '22 at 07:47
-2

I had the exact same problem and this worked for me. Please mark this answer as accepted if it works for you.

Code from @axiac on https://stackoverflow.com/a/46671863/19402660

// The input arrays
$Array1 = ['bus', 'bus', 'int'];
$Array2 = [2, 18, 10];

// Build the result here
$Array3 = [];

// There is no validation, the code assumes that $Array2 contains
// the same number of items as $Array1 or more
foreach ($Array1 as $index => $key) {
    // If the key $key was not encountered yet then add it to the result
    if (! array_key_exists($key, $Array3)) {
        $Array3[$key] = 0;
    }

    // Add the value associate with $key to the sum in the results array
    $Array3[$key] += $Array2[$index];
}

print_r($Array3);

Its output:

Array
(
    [bus] => 20
    [int] => 10
)
  • I gave proper credit, in case you missed. I tried to mark this as a duplicate, but I don't know how - I am new on this platform, as you can see by my user metrics. **Also: this answer gives the exact result OP and others might be after.** Give it a try. As an experienced member, you should be more courteous and prevent shamming on new users, instead constructively educating them. – williamadama Jul 25 '22 at 03:56