I have an array that looks like this: and I would like to sort the teams by the number of points each team has. I have tried this: But that approach overrides the keys containing the teamnames and returns: Is there any way to sort the array without overwriting the teamnames as the array keys? Answer Use the u…
Tag: php
Inserting data in MySQL table only if it doesn’t exist already: trouble with INSERT IGNORE
so I’ve been looking around StackOverflow and the MySQL manual for a while, and I can’t seem to solve my problem. What I’m trying to do is simply make my data INSERT function such that it doesn’t add anything to my table if it already exists. I saw a few methods: the INSERT IGNORE func…
How to get total time from several date ranges in php
I have several date ranges in form of DateTime $begin, DateTime $end. Those ranges can overlap in every possible way: etc. What I am trying to do is to get length (in seconds or DateInterval) of those ranges between start of the first one and the end of the latest one (fourth in the case above), excluding reg…
PHP Convert Windows-1251 to UTF 8
I have a small html code and I need to convert it to UTF-8. I use this iconv(“windows-1251”, “utf-8”, $html); All text converts correctly, but if text for example in tag …, then …
WordPress database error MySQL server has gone away for query
I get this error so frequently that the php_error log file increases 1MB every 2 seconds. and the site is very slow. I have tried to add this line to wp-db.php but it did not help. The web server is IIS 7, latest version of mysql and wordpress Answer This trick will work for all WordPress versions. Open your …
Php foreach – separating each loop with comma except the last element
Heres what i mean: current output would be: i want the output to be like this: (all ‘a’ separated with comma and when it comes to the last loop it doesnt write a comma) Answer Try this:
Find difference between two datetimes and format at Y-m-d H:i:s
I’m trying to get the difference between two datetimes and return it as a datetime. I’ve found examples using diff but I can’t seem to get it right. However $totaltime logs 0000-00-00 00:00:00 to my DB. Is this because I’m not formatting my totaltime variable? Answer I’m not sure…
How to return a line number in a PHP script
Is it possible for a PHP script to return a line number in which some command is called? I’m having trouble describing what I want so maybe an example. I have PHP code that calls MySQL on many occasions. In line 49 is: The text “line 49” I wrote manually. Is it possible to get this number &#…
Add a new index to multidimensional array
I have a multidimensional array. ie. Array ( [0] => Array ( [item_id] => 1 [item_name] => x ) [1] => Array ( […
Get insert_id for all rows inserted in single mysqli query (multiple values)
Working in PHP and using MYSQLI. Trying to get the insert_id for all rows inserted from a single insert query with multiple values. Keeping it in a single call for efficiency. My actual code has hundreds of values in one insert query. However, here is some sample code for an insert with just 4 values: The abo…