23
votes
7answers
2k views

is_numeric_array() is missing!

I found that in PHP (or I probably can't find it) a proper is_numeric_array($array) function is missing. So I created one. The problem is that I don't think it's ...
7
votes
2answers
321 views

Merge some child values back into the parent multidimensional array

This is a wonky PHP function I came up with to merge some select values from child arrays into the original. It could really use some help simplifying / making it more elegant. Is there a built-in ...
6
votes
1answer
269 views

Combining 3 or more arrays in php

Assuming I have 5 arrays, all just indexed arrays, and I would like to combine them, this is the best way I can figure, has anyone found a more efficient solution? ...
5
votes
3answers
317 views

Expanding sizes between given values

This a very simple function to expand the sizes between the given values. For instance, if the input variable is "S-XL" then the function returns "S, M, L, XL". Basically I'm not happy with this ...
5
votes
2answers
114 views

Check for correct number of elements in exploded string

Let's say I've got this string: $str = '1-2-3'; and I explode this like so: $strSplit = explode('-', $str); what would be ...
5
votes
1answer
87 views

Is there a more elegant solution than this spaghetti code without completely revamping the whole page?

I use 'GROUP_CONCAT' to get comma separated lists of a person's educational background. Included in those lists are the type of degree, major, issuing establishment, and year received. ...
5
votes
1answer
39 views

explode textarea and commas [closed]

Am attempting to recreate a common feature, where a user can type several items in to a textarea, separated by commas and newlines and then submit them, i.e. for multiple record entry. The data does ...
5
votes
2answers
572 views

Array find min value with user-defined comparison compare function

My user defined comparison compare function is defined with an array: ...
4
votes
2answers
205 views

Checking login from associative array vulnerabilities

For a very simple databaseless website, I'm using an associative array to store logins like this: ...
4
votes
1answer
40 views

Multidimentional array initialization

I am initializing a PHP array as follows $newArray = array(); $newArray['2014-13-03']['SMD']['IMPR'] = 5; $newArray['2014-13-03']['SMD']['CLICK'] = 10; I just ...
4
votes
4answers
318 views

Creating an array that loops out on the frontend - Don't Repeat Yourself?

Entire Code Here Don't repeat yourself I know of the "Don't repeat yourself". Still it's getting messy in some places. The code below contains three blocks of code. They are similar in some ways ...
4
votes
1answer
4k views

Better way of handling data returned from fetch_assoc() (mysqli)

So I connected to the db, pull the data using fetch_assoc() (fetch_all(NUMB) is not available on the machine we are working with else this would be less of an issue). So I get the returned data and ...
4
votes
2answers
42 views

Dynamic array of Years

I wanted to make a dynamic select statement from this year to 10 years. This gets the job done, but I feel like it's a little much. ...
4
votes
1answer
57 views

Simplifying an associative array()

Would it be better to convert the array in the following code to $langs = array("en", "de", "fr"); and then reusing the values for both ...
4
votes
1answer
320 views

How should I efficiently delete elements from a PHP array?

In this case I have an array that is parsed from a JSON file. Normally I use all the elements in the array, but for AJAX crawling I only show the parts the search engine asked for: ...
4
votes
2answers
213 views

Extract a joined result set into a parent-child hierarchy

I'm writing a quiz application in PHP and am querying the DB for questions and their associated answers. I then wrangle the result set into a usable array for my front end. However, it always seems ...
3
votes
3answers
114 views
3
votes
1answer
87 views

Display PHP Menu Stored in an Array and Looped

I'm creating a PHP website for a non-profit. They have some restrictions (no MySQL or pre-installed CMS) so I'm creating a CSS menu displayed by an unordered list where all of the elements are stored ...
3
votes
1answer
244 views

Getting an array from one property of an associative array best practice

I have an array of objects eg. [ array( 'id' => 1, 'name' => "simon" ), ... ] and I need to get an array of IDs eg. [1,2,3,4,5]; Currently I'm doing this: ...
3
votes
2answers
60 views

Array offset without losing values

After looking through PHP.net documentation for hours, is this the best way to offset an array from start to finish by using array_slice() with ...
3
votes
1answer
140 views

Small class to manage config data

I made this small class the allows me to navigate large arrays using something like: key1/key2/key3 I made this because I was creating large arrays to hold my classes configs and needed a simpler way ...
3
votes
0answers
48 views

Sorting a PHP array in a discontinuous manner

I start with an array containing some 'ids' and some 'values'. What I need is to organize the 'ids' in a new array to be, if possible, apart when the 'values' they are associated with are alike. So ...
2
votes
2answers
534 views

Performance: getting first value from comma delimited string

I've got a string that has values that are delimited by comma's, like so: $var = '1,23,45,123,145,200'; I'd like to get just the first value, so what I do is ...
2
votes
2answers
80 views

Calculating golf score against course par

This PHP script will translate shots on 18 holes to a total under par or E which is 0. ...
2
votes
2answers
80 views

Assigning array keys based on the values of another array

An optimisation question: Currently I have entities stored in text files in the following format: ...
2
votes
2answers
62 views

Is there a better way to combine these arrays?

Is there a better way to do this kind of array combination with a PHP native function like array_combine? I'm trying to improve my skills, and at certain points I ...
2
votes
1answer
3k views

Routing in MVC with PHP

I am in the progress of creating a MVC structured website. I've opted to make my own instead of using pre-constructed MVC systems as a way to teach myself better coding and understand how applications ...
2
votes
2answers
173 views

PHP Validation with functions and list()

I have multiple form validation checks around my site so I decided to write them as functions to simplify the process of creating the validation pages, here is a snippet for an understanding of what ...
2
votes
1answer
57 views

Two script completing the same task, which is the best way?

I have the following two methods of completing the same task, however I'd like to know which is the preferred method and WHY? Or if there are any suggestions to complete the task in an even better, ...
2
votes
1answer
975 views

Building an histogram array in PHP

I'm visualizing my stored data in an histogram similar to this one: However, I'm sure that my code smells a lot! So I'd like to have your opinion on how to make it better. Here's my current code: ...
2
votes
1answer
1k views

Generic array group by using lambda

This is K.I.S.S, but how about some error and misuse control? I am wishing it were .NET. ...
2
votes
1answer
73 views

Optimized solution for a rolling dice code puzzle

I was asked to provide the solution of this rolling dice problem in an interview. When I showed him my solution, he said that there is another way to find the solution. I am looking for answer in PHP ...
2
votes
2answers
242 views

Storing Hierarchical Data in a Database

in a extension tho this post Pages system PHP/SQL I've created this class/script to handle Multi-dimensional Menus wich data it's stored in a DB... I need some feedback and new ideas... (all of this ...
2
votes
3answers
255 views

Japanese rehearsal system

At the moment I am trying to create a Japanese rehearsal system, and it's working perfectly. However, I am not pleased with the code. Basically I had a .txt file called lesson1.txt. I saved it as ...
2
votes
1answer
124 views

Setting configuration arrays

I've created a website that displays weather and currency rates for London and New York. The following is an abstract from my cityConfig.php script. Is this a ...
1
vote
2answers
148 views

Which PHP code is faster?

Time is very important from times immemorial. Doing great things and making it happen in less time is more important as it can mean less effort. So I really like to know which way is faster and more ...
1
vote
2answers
261 views

Am I on the right track with this random number array?

Is this the best way of doing this? I wanted to make sure the grammar is correct when the array is output. 1 car, 2 cars. The output is correct but is there an easier way generally? I just want to ...
1
vote
2answers
3k views

Checking for array key exists in multi-dimensional array in PHP

Say I have a data structure like this: ...
1
vote
1answer
113 views

Referring to nested arrays and array_merge (php)

In such an array: ...
1
vote
2answers
157 views

Looking For a More Efficient/Elegant Way To Write a MySQL Query

I'm querying a MySQL database and displaying the info in order of timestamp (DESC). Essentially, I'm grabbing the last record in the database, displaying it's date and year as the header of it's ...
1
vote
1answer
65 views

Copying a lot of code to filter four PHP arrays

I have made this search filter and it works fine, but I think the code looks a little messy and I have no idea how to clean it up. I hope the following makes sense, otherwise please tell me which ...
1
vote
1answer
90 views

Is there any better way to fetch two tables in one statement or query?

The 'Do' method is for prepared statements and returns (by fetchall()). I am also using two looping statements. Is there any better way to fetch two tables in one statement or query? If not, what ...
1
vote
1answer
217 views

Pure PHP array_diff_assoc_recursive function

For my application, I was using array_diff_assoc, when I noticed it was returning the wrong value. I was using multidimensional arrays, therefore I needed a ...
1
vote
1answer
148 views

PHP illegal offset type in multiple loop structure

For reference, the function in question takes a URL structure which is most often a query string (but sometimes not) and generates filters to remove query var's from the URL. So if you're looking at ...
1
vote
1answer
101 views

Grouping a CSV by column

I've started writing a small CSV parser class as I needed a method to group a CSV file by a given column. ...
1
vote
1answer
55 views

Pushing instantiated class in an array

I've just stumbled upon something today that seemed "unethical" in PHP. What I originally thought as malpractice, turned out to work just fine. Basically what I was wondering is if pushing an ...
1
vote
1answer
71 views

Simplify code using array

I am trying to simplify the amount of code required here using arrays, I feel like my foreach loop is still excessively long. Any advice would be appreciated. ...
1
vote
2answers
751 views

What is a better way to get unique array Items based on key in PHP?

I'm trying to achieve a set of unique array with my following function. The Function: ...
1
vote
1answer
215 views

Turning multiple PHP arrays into one array ready for conversion to Javascript

I am creating a multiple choice quiz game with PHP and Javascript. Questions can have 2 to 6 answers. I have been using the PHP queries in the code below to get arrays of the questions of a quiz, and ...
1
vote
1answer
191 views