I have been working on a project recently for fun.
It retrieves JSON from a game server, decodes it into an array, and then saves the array values into a SQLite DB (for display / manipulation later). I am new to programming in general, and have never touched PHP prior to this.
My question: Is there a better, more efficient way to handle this?
Basically, this section of code, loops through the large multidimensional array, and replaces values that are equal to a string. It does this prior to insertion into the DB so that I can have fields formatted to be more readable.
The problem is that in the actual script I have a huge list of defined variables now, and like 3 foreach loops with a combined 15 or so if/else if/else statements.
$sr = "Summoners Rift";
$rs = "Ranked Solo";
$rt = "Ranked Team";
$nr = "Normal";
foreach ($history['games'] as &$typeMode)
{
if ($typeMode['subType'] == 'RANKED_SOLO_5x5')
{
$typeMode['gameMode'] = $sr;
$typeMode['subType'] = $rs;
}
elseif ($typeMode['subType'] == 'RANKED_TEAM_5x5')
{
$typeMode['gameMode'] = $sr;
$typeMode['subType'] = $rt;
}
elseif ($typeMode['subType'] == 'NORMAL')
{
$typeMode['gameMode'] = $sr;
$typeMode['subType'] = $nr;
}
}
The problem is that in the actual script I have a huge list of defined
variables now, and like 3 foreach loops with a combined 15 or so IF /
ELSEIF / ELSE statements.
The best solution I can recommend based on the data your are showing is to just create a basic array of structures connected to the data you have and then just use the foreach loop to assign values based on that initial array structure:
// Set structured array values.
$array_values = array();
$array_values['RANKED_SOLO_5x5']['gameMode'] = "Summoners Rift";
$array_values['RANKED_SOLO_5x5']['subType'] = "Ranked Solo";
$array_values['RANKED_TEAM_5x5']['gameMode'] = "Summoners Rift";
$array_values['RANKED_TEAM_5x5']['subType'] = "Ranked Team";
$array_values['NORMAL']['gameMode'] = "Summoners Rift";
$array_values['NORMAL']['subType'] = "Normal";
// Set structured array values based on the sub type.
foreach ($history['games'] as &$typeMode) {
$typeMode['gameMode'] = $array_values[$typeMode['subType']]['gameMode'];
$typeMode['subType'] = $array_values[$typeMode['subType']]['subType'];
}
That way $array_values always has the preset values in place to begin with. And the assignment just happens via an array key access of $typeMode['subType'] in the foreach loop.
For things like this I prefer to use the switch control structure instead of if/elseif/else, although your approach is perfectly fine, if a little verbose maybe:
foreach ($history['games'] as &$typeMode)
{
$typeMode['gameMode'] = $sr;
switch($typeMode['subType'])
{
case 'RANKED_SOLO_5x5':
$typeMode['subType'] = $rs;
break;
case 'RANKED_TEAM_5x5':
$typeMode['subType'] = $rt;
break;
case 'NORMAL':
$typeMode['subType'] = $nr;
break;
}
}
Also if $typeMode['gameMode'] always equals $sr; then you only need that line once.
Related
I am building a web scraper in PHP and I am not so experimented with all this stuff. What I am trying to achieve is as following:
Split an array of values into strings using foreach
Search any value in a predefined MYSQL table. If value is identical with one of the defined ones, it should be replaced. Otherwise it should remain the same
Put the new values back into an array
Below is my snippet. Basic structure of database is "ID, Marime, Inlocuire". "Marime" is the column to search on, and "Inlocuire" is the column to replace value with.
foreach ($marimi as $marime) {
$sizes[]=trim(strtok($marime->innertext, '-'));
$newArray = array_filter($sizes, 'myFilter');
foreach ($newArray as $marimeFixa) {
$marimeDefinita = $conn->query("SELECT * FROM oc_1_tabelmarimi WHERE Marime = '$marimeFixa'");
if($marimeDefinita->num_rows == 0) {
$marimeFixa = $marimeFixa;
} else {
$marimeFixa = $marimeDefinita['Inlocuire'];
}
$arrayMarimi[] = $marimeFixa;
}
print_r($arrayMarimi);
}
However this doesn't seem to work. Any help is greatly appreciated. Thanks!
try:
$marimeDefinita = $marimeDefinita->fetch_assoc();
before if($marimeDefinita->num_rows == 0) {
or
$marimeFixa = $marimeDefinita->Inlocuire;
So basically i'm trying to create a complex timetable and i have these two methods that each perform a different check function for me:
Checks if i have a unique array
function tutorAllot($array,$check,$period){
//check for clashes and return non colliding allotment
shuffle($array);
$rKey = array_rand($array);
if(array_key_exists($array[$rKey]['teacher_id'], $check[$period])) {
return $this->tutorAllot($array,$check,$period);
}
return $tutor = array($array[$rKey]['teacher_id'] => $array[$rKey]['subject_code']);
}
checks that each subject does not appear more than twice in a day
function checkDayLimit($data,$check){
//check double day limit
$max = 2;
$value = array_values($check);
$tempCount = array_count_values($data);
return (array_key_exists($value[0], $tempCount) && $tempCount[$value[0]] <= $max) ? true : false;
}
I'm calling the functions from a loop and populating timetable array only if all conditions area satisfied:
$outerClass = array();
foreach ($value as $ky => $val) {
$innerClass = array(); $dayCount = array();
foreach ($periods[0] as $period => $periodData) {
$innerClass[$period] = array();
if(!($periodData == 'break')){
$return = $this->Schedule->tutorAllot($val,$clashCheck,$period);
if($return){
//check that the returned allocation hasnt reached day limit
if($this->Schedule->checkDayLimit($dayCount,$return)){
$innerClass[$period] += $return;
$clashCheck[$period] += $return;
}else{
}
}
}else{
$innerClass[$period] = '';
}
}
//debug($innerClass);
$outerClass[$ky] = $innerClass;
}
My requirements
If the checkDayLimit returns false , i want to go back and call tutorAllot function again to pick a new value.
I need to do this without breaking the loop.
I was thinking maybe i could use goto statement but only when am out of options.
Is there a way i can achieve this without using goto statement.
PHP v5.5.3 Ubuntu
Your architecture seems overly complex. Instead of
pick at random >> check limit >> if at limit, go to re-pick...
Why not incorporate both checks into a single function? It would
Filter out data that is not eligible to be picked, and return an array of legitimate choices
Pick at random from the safe choices and return the pick
addendum 1
I don't think there is any need for recursion. I would use array_filter to pass the data through a function that returns true for eligible members and false for the rest. I would then take the result of array_map and make a random selection from it
I have a json feed with 6 objects all which have objects inside of them. I have an ID of something I am trying to search and count in another object.
if (isset($_GET['steamusr'])) {
$user = $_GET['steamusr'];
$myinv = 'http://steamcommunity.com/id/'.$user.'/inventory/json/295110/1/';
$content2 = file_get_contents($myinv);
$json2 = json_decode($content2, true);
$imgurlbase = 'http://steamcommunity-a.akamaihd.net/economy/image/';
foreach($json2['rgDescriptions'] as $i){
$item = $i['market_name'];
$icon = $i['icon_url'];
$fetchdata = 'http://steamcommunity.com/market/priceoverview/?appid=295110¤cy=1&market_hash_name=' . urlencode($item);
$grab = file_get_contents($fetchdata);
$id = json_decode($grab, true);
$itemid = $i['classid'];
foreach($json2->rgInventory as $i2){
if($i2->$itemid == $itemid){
$ci = 0;
$count = $ci++ ;
}
}
All the data comes from rgDescriptions first, then rgInventory has the number of objects to count within it. The item ID comes from $itemid which I then need to search rgInventory for and then count the amount of matching id's in there from the set value $itemid.
My biggest issue is rgInventory has unique objects so I am trying to do a recursive/wildcard search of matching classid's.
The json structure can be found here: http://www.jsoneditoronline.org/?url=http://steamcommunity.com/id/fuigus/inventory/json/295110/1/
I think your code is correct in essence but you're not comparing the right things.
$json = json_decode($content2);
foreach ($json["rgDescriptions"] as $item) {
$num = 0;
foreach ($json["rgInventory"] as $inventory_entry) {
if ($inventory_entry["classid"] === $item["classid"]) {
$num += 1;
}
}
// do something with $num
var_dump($item["classid"] . ": " . $num);
}
The line:
if($i2->$itemid == $itemid){
Is bad, $i2->$itemid resolves to $i2->1107865473 which doesn't exist. I think you intended $i2->classid.
Error like this happen because you're using meaningless, abstract variable names. $i, $i2 and $content2, these are meaningless. You're also mixing the terms itemid and classid, it's easy to get confused.
Also, you're mixing bracket notation and object notation. Pick one and stick to it.
I know how to get a mysql-row and convert it to json:
$row = mysqli_fetch_assoc(mysqli_query($db, "SELECT * FROM table WHERE id=1"));
echo json_encode($row); // it's an ajax-call
but:
the db-row has different types like int, float, string.
by converting it using json_encode() all results are strings.
Is there a better way to correct the types than this:
$row['floatvalue1'] = 0+$row['floatvalue1'];
$row['floatvalue2'] = 0+$row['floatvalue2'];
$row['intvalue1'] = 0+$row['intvalue1'];
I would like to loop through the keys and add 0 because:
first coding rule: DRY - dont repeat yourself
but i can't because:
row has also other types than numbers (string, date)
there are many columns
design is in dev, so columns-names often changes
Thanks in advance and excuse my bad english :-)
EDIT (to answer the comment-question from Casimir et Hippolyte):
I call this php-code using ajax to get dynamically sql-values. in my javascript-code i use the results like this:
result['intvalue1'] += 100;
lets say the json-result of intval1 is 50, the calculated result is:
"50100", not 150
The code below is just a proof of concept. It needs encapsulation in a function/method and some polishing before using it in production (f.e. call mysqli_fetch_field() in a loop and store the objects it returns before processing any row, not once for every row).
It uses the function mysqli_fetch_field() to get information about each column of the result set and converts to numbers those columns that have numeric types. The values of MYSQLI_TYPE_* constants can be found in the documentation page of Mysqli predefined constants.
// Get the data
$result = mysqli_query($db, "SELECT * FROM table WHERE id=1");
$row = mysqli_fetch_assoc($result);
// Fix the types
$fixed = array();
foreach ($row as $key => $value) {
$info = mysqli_fetch_field($result);
if (in_array($info->type, array(
MYSQLI_TYPE_TINY, MYSQLI_TYPE_SHORT, MYSQLI_TYPE_INT24,
MYSQLI_TYPE_LONG, MYSQLI_TYPE_LONGLONG,
MYSQLI_TYPE_DECIMAL,
MYSQLI_TYPE_FLOAT, MYSQLI_TYPE_DOUBLE
))) {
$fixed[$key] = 0 + $value;
} else {
$fixed[$key] = $value;
}
}
// Compare the results
echo('all strings: '.json_encode($row)."\n");
echo('fixed types: '.json_encode($fixed)."\n");
something like
$row['floatvalue1'] = reset( sscanf ( $row['floatvalue1'] , "%f" ));
$row['floatvalue2'] = reset( sscanf ( $row['floatvalue2'] , "%f" ));
$row['intvalue1'] = reset( sscanf ( $row['intvalue1'] , "%d" ));
json_encode($row);
If you're simply trying to make sure that your values are operable with respect to their type, you need to first cast their type correctly.
Unless you need them server-side, I would just pass-on the json directly to the front-end and do the work there.
In Javascript, you could make an attempt at casting the numbers like so:
function tryNumber(string){
return !isNaN( parseInt(string) ) ? parseInt(string) : string;
}
function tryDate(string){
return !isNaN( new Date(string).getTime() ) ? new Date(string) : string;
}
tryNumber('foo'); // "hello"
tryNumber('24'); // 24
tryDate('bar'); // "bar"
tryDate('December 17, 1995'); // "Sun Dec 17 1995 00:00:00 GMT+0000 (GMT)"
These two lines attempt to cast the values as a Date/Number. If they can't be cast, they will remain String's.
A MySQLi OO version based on #axiac's answer, that produces a JSON array ($jsnAll) containing all records. In this code snippet, the method FixSQLType is called to fix a row. Note, it should be wrapped in a try{}catch{} block and "objMySQLi" has already been instantiated:
$lcAllRows = array();
// Make an SQL SELECT statement
$SQL = "SELECT * FROM $lcName WHERE $lcWhere";
// Run the query
$this->sqlResult = $this->objMySQLi->query($SQL);
// Fetch the result
while( $row = $this->sqlResult->fetch_assoc()){
$lcCount = count($lcAllRows) ;
// Call to fix, row
$fixedRow = $this->FixSQLType($row);
$lcAllRows[$lcCount]= $fixedRow;
}
$jsnAll = json_encode($lcAllRows);
The FixSQLType method. This is almost identical to #axiac's answer, except for the call to $this->sqlResult->fetch_field_direct($i). "fetch_field" seemed to get itself lost, using "fetch_field_direct" overcame that problem.
private function FixSQLType($pRow){
// FROM https://stackoverflow.com/a/28261996/7571029
// Fix the types
$fixed = array();
$i = 0;
foreach ($pRow as $key => $value) {
$info = $this->sqlResult->fetch_field_direct($i);
$i++;
if (in_array($info->type, array(
MYSQLI_TYPE_TINY, MYSQLI_TYPE_SHORT, MYSQLI_TYPE_INT24,
MYSQLI_TYPE_LONG, MYSQLI_TYPE_LONGLONG,
MYSQLI_TYPE_DECIMAL,
MYSQLI_TYPE_FLOAT, MYSQLI_TYPE_DOUBLE
))) {
$fixed[$key] = 0 + $value;
} else {
$fixed[$key] = $value;
}
}
return $fixed;
}
I am building up an array with a set of database fields with information about table, actual field name and descriptive field name as a multi-dimensional array. Here is what it currently looks like:
$Fields['User']['ID'] = "User ID";
$Fields['User']['FirstName'] = "First Name";
$Fields['Stats']['FavouriteOrder'] = "Favourite Item Ordered";
$Fields['Geographic']['Location'] = "Current Location";
$Fields['Geographic']['LocationCode'] = "Current Location Code";
Okay, this is fine, but I am piping this into a system that allows exporting of selected fields, and in the end I want to foreach() through the different levels, extract the data and then ultimately have all the descriptive fields to be displayed sorted alphabetically using their descriptive name. So ultimately in the order: Current Location, Current Location Code, Favorite Item Ordered, First Name then User ID - obviously keeping index associations.
I can't use usort() and I can't use array_multisort()... or maybe I can and I just don't know how. usort() seems to need a key to sort by, but I have variable keys. array_multisort() just seems to do the same as sort() really.
This is for a 2D array only. Not the most elegant piece of code I've written, but it works...
foreach($Fields as $key=>$var) {
ksort($var);
$Fields[$key]=$var;
}
ksort($Fields);
Let me rather give a real-life data example, as opposed to fake data because the fake data nearly confused me. So, fake data is commented.
/*
$Fields['User']['ID'] = "User ID";
$Fields['User']['FirstName'] = "First Name";
$Fields['Stats']['FavouriteOrder'] = "Favourite Item Ordered";
$Fields['Geographic']['Location'] = "Current Location";
$Fields['Geographic']['LocationCode'] = "Current Location Code";
*/
$Fields['Product']['ReferenceNumber'] = "Product Reference Number";
$Fields['Product']['Halaal'] = "Halaal Status";
$Fields['Product']['Kosher'] = "Kosher Status";
$Fields['Product']['KosherType'] = "Kosher Type";
$Fields['Product']['CuringSalts'] = "Curing Salts Status";
$Fields['Product']['ProductVisibility'] = "Product Visibility";
$Fields['Product']['ProductStatus'] = "Product Status";
$Fields['Product']['PackBarCode'] = "Barcode";
$Fields['Product']['ProductDescription'] = "Product Description";
$Fields['Pack']['PackSize'] = "Pack Size";
$Fields['Pack']['PackSizeNumeric'] = "Numeric Pack Size";
$Fields['Allergens']['ContainsNuts'] = "Product Contains Nuts";
foreach ($Fields as $key => $value) {
ksort($value);
$Fields[$key] = $value;
}
ksort($Fields);
I'm having one of 'those' Fridays... print_r($Fields) reveals that the keys are being sorted and values are associated, but it's still sorting by the keys and not the end value.
It's almost like i need a reverse sorting system which checks all values first, sorts them and then says 'okay where do you belong ... ah you belong to FieldX in Table Y'
I was hoping there was a sneaky clever way to do it, perhaps there is, but I guess I'll write a function to parse through the data, write a reversed array and then re-write the original in value-order. Hectically inefficient, but it'll do.
Still open to better suggestions though!
I literally had to work this out yesterday for a project I was working on - here's my code:
Resource array looks like this:
$resource[$count]['title']
$resource[$count]['filepath']
$resource[$count]['filename']
$resource[$count]['taxonomy'][0]
A couple of sort functions to sort by title ASC or DESC
function compare_asc($a, $b) {
return strcmp($a['title'], $b['title']);
}
function compare_desc($a, $b) {
if ($a['title'] == $b['title']) {
return 0;
}
return ($a['title'] > $b['title']) ? -1 : 1;
//return strcmp($a['title'], $b['title']);
}
And finally use usort to do the dirty before you loop through $resource and output whatever it is you need.
usort($resource, "compare_asc");
Okay, it's not elegant at all. So I don't encourage using this and will look for a better way down the line. But here's the solution I've got that works with the examples above and below.
Unfortunately, the way I want it to be, the array HAS to contain a preceding 'ordering' number so, I suppose it's a fail on my part from the very beginning. But it works now.
$TempDescArray = array();
$TempFieldArray = array();
$TempTableArray = array();
$Pointer = 0;
foreach ($Fields as $Table => $FieldsArray) {
foreach ($FieldsArray as $Field => $Description) {
$TempDescArray[$Pointer] = $Description;
$TempFieldArray[$Pointer] = $Field;
$TempTableArray[$Pointer] = $Table;
$Pointer++;
}
}
asort($TempDescArray);
$Fields = array();
$Pointer2 = 0;
foreach ($TempDescArray as $Pointer => $Description) {
$Fields[$Pointer2][$TempTableArray[$Pointer]][$TempFieldArray[$Pointer]] = $Description;
$Pointer2++;
}