Multidimension array search compare addition

Hello all,

I have a db table and created an multidimension array that I want to compare using another multidimension array. If the element exists in the 2nd array, then I want to add the value to it’s counter.

The first problem that I am having is whether to use array_search or in_array.

Anyway, here is what the arrays look like:


$tablerray = array(
[hora] => Australia, [count] => 3, 
[hora] => Belgium, [count] => 1, 
[hora] => Canada, [count] => 2);

$cntry_multi = array(
"cc1" => "Austria", "cc2" => "Αυστρία", "cnt" => "0",
"cc1" => "Belgium", "cc2" => "Βέλγιο", "cnt" => "0",
"cc1" => "Canada", "cc2" => "Καναδάς", "cnt" => "0");

$i = 0; $k = 0;

    // put table data into array
    $tablerray = array(); 
    while ( $row = mysql_fetch_array($result, MYSQL_ASSOC) ){
        $tablerray[] = $row;
    }
    reset($tablerray);
    $tablerray_cnt = count($tablerray);
    //print '<pre>'; print_r($tablerray); print '</pre>'; print "<br />\
";

    // create secondary array to store values for elements that exist in table array
    $tablerray2 = array();

    // iterate table array, find country, assign values to new array
    foreach ($tablerray as $v1)    {
        // get country from 1st array assing to variable
        $cntry_table_name = $tablerray[$k]["hora"];
        // get data from array
        $cntry_array_name = $cntry_multi["cc1"];
        foreach ($v1 as $v2) {
            //if (in_array($cntry_name, $cntry_multi) === 1)
            if (array_search($cntry_table_name, $cntry_multi) == true){ 
                print "data found"."<br />\
";};
                $tablerray2[$i]["cc1"] = $cntry_multi["cc1"];
                $tablerray2[$i]["cc2"] = $cntry_multi["cc2"];
                $tablerray2[$i]["cnt"] += $cntry_cnt;
                $i++;
                break;
            }
        }
    print "<br />\
";
    $k++;
    }
    print "<br />\
";

    print "display table array row 0"."<br />\
";
    print_r ($tablerray[0]); print "<br /><br />\
";
    
    print "display table array 2"."<br />\
";
    print '<pre>'; print_r($tablerray2); print '</pre>'; print "<br />\
";

I am not sure if I needed to create the new array but I did it to separate the results. I want to be able to return the results for the country in either language.

The output that I am getting is the last row of the array table which is “Not Listed” and is not shown here.

What I am doing wrong or not seeing?

Thank you

The syntax on both or your arrays is incorrect. In the first one hora should be in quotes not brackets. On both arrays, you’re not creating multi-dimensional arrays. each line you have is just overwriting the previous row. You’re using the same indexes multiple times. Which means the value previously written to that index is being replaced.

So for example your $cntry_multi array just contains “cc1” => “Canada”, “cc2” => “Καναδάς”, “cnt” => "0. Austria and Belgium are lost. You can verify this with var_dump.

It should look something more like…



$cntry_multi = array(
array("cc1" => "Austria", "cc2" => "&#913;&#965;&#963;&#964;&#961;&#943;&#945;", "cnt" => "0"),
array("cc1" => "Belgium", "cc2" => "&#914;&#941;&#955;&#947;&#953;&#959;", "cnt" => "0"),
array("cc1" => "Canada", "cc2" => "&#922;&#945;&#957;&#945;&#948;&#940;&#962;", "cnt" => "0"));


Check here for more on multi-dimensional arrays…

http://php.net/manual/en/language.types.array.php

Where to begin with this… I’m ASSUMING that your definition of $cntry_multi is ACTUALLY a multidimensional array, and not the same array writing over itself over and over (nest array() statements, or use objects!). The way you have depicted it, there is only 1 entry in $cntry_multi. (Canada’s)

    $cntry_table_name = $tablerray[$k]["hora"];

Should really be called as
$cntry_table_name = $v1[“hora”];
Makign $k redundant and unnecessary.

    $cntry_array_name = $cntry_multi["cc1"];

Will always return Canada, in the code you gave us. If it’s an enumerated multidimensional array, then this returns Nothing, because there is no entry “cc1” in the array index.

    foreach ($v1 as $v2) {

This line will trigger 2 times per loop; once for the value of hora, and once for the value of count. I dont think this is what you want.

        if (array_search($cntry_table_name, $cntry_multi) == true){ 
            print "data found"."&lt;br /&gt;\

";};
$tablerray2[$i][“cc1”] = $cntry_multi[“cc1”];
$tablerray2[$i][“cc2”] = $cntry_multi[“cc2”];
$tablerray2[$i][“cnt”] += $cntry_cnt;
$i++;
break;
}

This… makes no sense to me whatsoever.

Alright, let me ask this… what are you trying to DO with this code?

Hello all,

Sorry for the copy / paste errors. The arrays are correct on my system.

I guess that it is best that I explain what I am doing. First I am pulling data from a table and creating the $tablerray.

I have a 2nd array $cntry_multi that consists of 240 rows with country names in two languages and a third element to be used for total counts.

What I want to do is to get each $tablerray row country name and compare it to the $cntry_multi, if it is there then I want to total the counts.
The reason for this is because there are counts associated for the same country but in both languages.
e.g.
Austria 5
Αυστρία 3
therefore I should display Austria with a total of 8.

I wasn’t sure if I needed the additional array to output the final listing or if it can be done without it.

Thanks for responding.