Example #1
0
function actionsFillTable()
{
    $carton = array();
    $row = array();
    $remPos = array();
    generateNumRow($carton);
    generateNumRow($carton);
    generateNumRow($carton);
    enterEmpty($remPos);
    sort($carton);
    for ($x = 0; $x < 3; $x++) {
        for ($i = 0; $i < 9; $i++) {
            $row[$x][$i] = $carton[$i * 3 + $x];
        }
    }
    for ($i = 0; $i < 3; $i++) {
        combineArrays($row[$i], $remPos[$i]);
    }
    enterFile($row);
}
}
//This function calls the matches and prints the names of the matching people from original arrays
function matchingNames($firstArray, $secondArray)
{
    $matches = [];
    foreach ($firstArray as $query) {
        if (search($query, $secondArray)) {
            array_push($matches, $query);
        }
    }
    foreach ($matches as $matchNames) {
        echo $matchNames . " was a matched name." . PHP_EOL;
    }
}
//combines arrays with duplicates
function combineArrays($firstArray, $secondArray)
{
    $thirdArray = [];
    for ($i = 0; $i < count($firstArray); $i = $i + 1) {
        if ($firstArray[$i] == $secondArray[$i]) {
            array_push($thirdArray, $firstArray[$i]);
        } else {
            array_push($thirdArray, $firstArray[$i], $secondArray[$i]);
        }
    }
    print_r($thirdArray);
}
echo combineArrays($names, $compare);
echo compare_arrays($compare, $names);
echo compare_arrays($names, $compare);
echo matchingNames($names, $compare);
<?php

$names = ['Tina', 'Dana', 'Mike', 'Amy', 'Adam'];
$compare = ['Tina', 'Dean', 'Mel', 'Amy', 'Michael'];
function getUniqueValuesInBothArrays($firstArray, $secondArray)
{
    return array_values(array_unique(array_merge($firstArray, $secondArray)));
}
$together = getUniqueValuesInBothArrays($names, $compare);
print_r($together);
echo "=================\n";
function combineArrays($array1, $array2)
{
    $combined = [];
    foreach ($array1 as $value) {
        if ($array1[0] == $array2[0]) {
            $combined[] = array_shift($array1);
            array_shift($array2);
        } else {
            $combined[] = array_shift($array1);
            $combined[] = array_shift($array2);
        }
    }
    return $combined;
}
print_r(combineArrays($names, $compare));
Example #4
0
function addFile($fileContents, &$dataArray, &$addDateTime)
{
    $validHeaders = array();
    $csvArray = csvToArray($fileContents);
    if (validDateTime($csvArray)) {
        if ($addDateTime) {
            //If we need to add date time, leave it in the array -- we don't need it anymore after this point
            $addDateTime = false;
        } else {
            // Else remove the date/time column
            $dateTimes = getDateTime($dataArray);
            reorderArray($dateTimes, $csvArray);
            //reoder the array before combining so that the data will match previous data
            removeDateTime($csvArray);
        }
        combineArrays($dataArray, $csvArray);
        return true;
    } else {
        return false;
    }
}
<?php

// Create a function that returns TRUE or FALSE if an array value is found. Search for Tina and Bob in $names. Make sure it works as expected.
// Create a function to compare 2 arrays that returns the number of values in common between the arrays. Use the 2 example arrays and make sure your solution uses array_search().
$names = ['Tina', 'Dana', 'Mike', 'Amy', 'Adam'];
$compare = ['Tina', 'Dean', 'Mel', 'Amy', 'Michael'];
function combineArrays($firstArray, $secondArray)
{
    // Declare new array
    $newArray = [];
    foreach ($firstArray as $key => $value) {
        //If the arrays have the same value at the same index, then it should only be added once.
        if ($firstArray[$key] == $secondArray[$key]) {
            $newArray[] = $secondArray[$key];
        } else {
            //If the values differ, the value from the first array should be added and then the second.
            $newArray[] = $firstArray[$key];
            $newArray[] = $secondArray[$key];
        }
    }
    return $newArray;
}
var_dump(combineArrays($names, $compare));