// This function prints the data out in the command line in a grid format pleasing to humans.
// This is where the spaceLength function comes into play.
function printArrayInCommandLine($contentsArray)
{
    $textLines = '';
    $unitsTitleSpace = spaceLength(8, 'Units');
    $fullnameTitleSpace = spaceLength(35, 'Full Name');
    foreach ($contentsArray as $key => $value) {
        $firstSpace = spaceLength(8, $value['units']);
        $secondSpace = spaceLength(35, $value['full name']);
        $thirdSpace = spaceLength(3, $value['employee number']);
        $textLines .= "{$value['units']}{$firstSpace}|     {$value['full name']}{$secondSpace}|   {$value['employee number']}{$thirdSpace}" . PHP_EOL;
    }
    return "Units{$unitsTitleSpace}|     Full Name{$fullnameTitleSpace}|   Employee Number" . PHP_EOL . $textLines;
}
// Each function is called and saved as $contentsArray to cut down on variables.
// Each time the updated version of the array is saved over the previous array of the same name.
$contentsArray = readTheFile('data/reports.txt');
$contentsArray = separateArray($contentsArray);
$contentsArray = reorderArray($contentsArray);
// This portion prints out human pleasing sentences of each variable.
$numOfEmployees = countEmployees($contentsArray);
echo "There are {$numOfEmployees} employees." . PHP_EOL;
$sumOfAllUnits = addAllUnits($contentsArray);
echo "The total number of units sold was {$sumOfAllUnits}." . PHP_EOL;
$averageUnitsSold = averageOfUnitsSold($sumOfAllUnits, $numOfEmployees);
echo "The average number of units sold by each employee was {$averageUnitsSold}." . PHP_EOL;
// This portion sorts the array by the employee array units value.
$contentsArray = sortByUnits($contentsArray);
// And this portion prints out the resulting array into a human pleasing format in the command line.
echo printArrayInCommandLine($contentsArray);
        foreach ($locations as $location) {
            if (!is_numeric($location)) {
                array_push($locArray, $location);
            } else {
                break;
            }
        }
        $locString = implode($locArray);
        unset($contentsArray[$index]['location']);
        $contentsArray[$index]['location'] = $locString;
    }
    return $contentsArray;
}
// This function formats the area by exploding on spaces for each area and then resetting the value of each area only to the first value in the areas array.
function formatArea($contentsArray)
{
    foreach ($contentsArray as $index => $value) {
        $areas = explode(' ', $contentsArray[$index]['area']);
        unset($contentsArray[$index]['area']);
        $contentsArray[$index]['area'] = $areas[0];
    }
    return $contentsArray;
}
// Each function is called and saved as $contentsArray to cut down on variables.
// Each time the updated version of the array is saved over the previous array of the same name.
$contentsArray = readTheFile('public/data/parks.csv');
$contentsArray = cutOne($contentsArray);
$contentsArray = reorderArray($contentsArray);
$contentsArray = formatLocation($contentsArray);
$contentsArray = formatArea($contentsArray);
// print_r($contentsArray);
示例#3
0
        $contentsArray[$index]['description'] = $valueArray[3];
        $contentsArray[$index]['image_url'] = $valueArray[4];
        $contentsArray[$index]['method'] = $valueArray[5];
        $contentsArray[$index]['categories'] = substr($valueArray[6], 0, -1);
    }
    return $contentsArray;
}
// Each function is called and saved as $contentsArray to cut down on variables.
// Each time the updated version of the array is saved over the previous array of the same name.
$contentsArray = readTheFile('data_ads1.txt');
$contentsArray = cutOne($contentsArray);
$contentsArray = reorderArray($contentsArray);
$contentsArray2 = readTheFile('reagan_spatulas.txt');
$contentsArray2 = cutOne($contentsArray2);
$contentsArray2 = reorderArray($contentsArray2);
$contentsArray3 = readTheFile('database_ads.txt');
$contentsArray3 = cutOne($contentsArray3);
$contentsArray3 = reorderArray($contentsArray3);
$masterArray = array_merge($contentsArray, $contentsArray2, $contentsArray3);
// print_r($contentsArray);
$query = "INSERT INTO ads (user_id, method, image_url, title, price, location, description, categories) \n            VALUES (:user_id, \n                :method,\n                :image_url,\n                :title, \n                :price, \n                :location, \n                :description,\n                :categories)";
$stmt = $dbc->prepare($query);
foreach ($masterArray as $ad) {
    $stmt->bindValue(':user_id', rand(1, 3), PDO::PARAM_INT);
    $stmt->bindValue(':method', $ad['method'], PDO::PARAM_STR);
    $stmt->bindValue(':image_url', $ad['image_url'], PDO::PARAM_STR);
    $stmt->bindValue(':title', $ad['title'], PDO::PARAM_STR);
    $stmt->bindValue(':price', $ad['price'], PDO::PARAM_STR);
    $stmt->bindValue(':location', $ad['location'], PDO::PARAM_STR);
    $stmt->bindValue(':description', $ad['description'], PDO::PARAM_STR);
    $stmt->bindValue(':categories', $ad['categories'], PDO::PARAM_STR);