3

I used array_merge_recursive on the arrays below:

Array ( [111-23456-789] => 0 [2380600] => 1963 [8123123] => 0...)

Array ( [111-23456-789] => 0 [2380600] => 3860 [8123123] => 0...)

note: Because array_merge_recursive works on string keys, the keys for the arrays above are actually string-type values of another array. Also, the array is actually bigger than this hence the '...' before the end brackets of the arrays.

Expected output (from what I understand in the W3Schools.com and php.net):

Array ( [111-23456-789] => Array ( [0] => 0 [1] => 0 ) [2380600] => Array ( [0] => 1963 [1] => 3860 ) [8123123] => Array ( [0] => 0 [1] => 0 )...)

Instead I get this nonsense:

Array ( [111-23456-789] => Array ( [0] => 0 [1] => 0 ) [0] => 1963 [1] => 0 [2] => 365...)

Below is how I got my array of keys:

while ($nrows = mysqli_fetch_assoc($resultSetNames)) {
    $name = $nrows['name'];
    array_push($arrayNamesRes, "$name");
}

This is how I made my first associative array:

$nov = 0;

while ($novrows = mysqli_fetch_assoc($resultSetNov)) {
    $counter = $novrows['count(playsms_tblsmsoutgoing.uid)'];

    $arrayNovRes[$arrayNamesRes[$nov]] = $counter;
    $nov++;
}

...and my second associative array:

$dec = 0;

while ($decrows = mysqli_fetch_assoc($resultSetDec)) {
    $counter = $decrows['count(playsms_tblsmsoutgoing.uid)'];
    $arrayDecRes[$arrayNamesRes[$dec]] = $counter;
    $dec++;
}

What can I do to get my expected output? I'm still new at using stackoverflow. If this post is too vague please state so. Thank you in advance.

n3094
  • 31
  • 2
  • This is almost certainly an XY Problem. Your coding attempts indicate that you are making one query to get the keys, then another query to get November data, then yet another query to get December data. You should only be making one trip to the database. Improving your SQL to implement JOINs will eliminate all of this messy business. We don't have enough detail to demonstrate how to craft your single query, but that is certainly what you should do. In the meantime, you could `fetchAll()` and iterate multiple arrays at once. – mickmackusa Sep 15 '22 at 03:33

2 Answers2

1

Problem is, that even if your keys are represented as strings, and are consisted of only numbers, PHP treats them as numeric keys. What you can do, is add a non-numeric character to the key name, i.e. a blank space in the end, this will force PHP to treat the keys as strings. Which of course is quite a bit hacky.

Here is my proposal on how you can do this:

$merged = [];
foreach ($firstArray as $key => $value) {
    if (array_key_exists($key, $secondArray)) {
        // key exists in second array as well, merge the values
        $merged[$key] = [$value, $secondArray[$key]];
    } else {
        // not found in second array, just overwrite to the merged array
        $merged[$key] = $value;
    }
}

Or the little hacky way to prepend the keys which can be found in the accepted answer of this question: PHP's array_merge_recursive behaviour on integer keys

Community
  • 1
  • 1
slax0r
  • 688
  • 4
  • 8
0

This can be done in many way please have a look on below solutions:

1) If both array have same keys at same position then use array_map function:

$array1 = array ( '111-23456-789' => 0, '2380600' => 1963, '8123123' => 0);
$array2 = array ( '111-23456-789' => 0, '2380600' => 3860, '8123123' => 0);

$new_array = array_map(function($a, $b, $key){
    return array($key => array($a, $b));
}, $array1, $array2, array_keys($array1));

$new_array = call_user_func_array('array_merge', $new_array);
print_r($new_array);

2) If keys are not on same position use traditional way i.e. foreach loop as below:

$new_array = array();
foreach($array1 as $key => $value){
    $new_array[$key][] = $value;
    if(isset($array2[$key])){
        $new_array[$key][] = $array2[$key];
    }
}

print_r($new_array);

Output:

Array
    (
        [111-23456-789] => Array
            (
                [0] => 0
                [1] => 0
            )

        [2380600] => Array
            (
                [0] => 1963
                [1] => 3860
            )

        [8123123] => Array
            (
                [0] => 0
                [1] => 0
            )

    )
Chetan Ameta
  • 7,696
  • 3
  • 29
  • 44