I’m fairly new to PHP and I’ve been looking around and can’t seem to find the specific answer I’m looking for. I want to make a SQL query, such as this: Basically I want to take the entire contents of the result and create an array that I can access in a similar fashion as the row. For example, if
Tag: arrays
Implode array with array of glue strings
I have a awkward need but I need to interleave an array with another array before imploding the result. I guess my better option would be less talk more example Array number one Array number two I need to produce My question is can I do that with an implode or I must build my own function? Answer Adapted from
PHP rotate matrix counter-clockwise
I am solving a larger problem and at one step I need to rotate a 2D array counter-clockwise. So if I have this matrix: After the rotation it will be: I have found a solution to rotate it clockwise: The thing is that this has to work even when a is uni-dimensional or has only one element. So, 1 2
php for loop from array stops after 3 loops
I have an array that looks like this. I want to loop through array in the first key of [2] and echo all the values. this works, but stops after three loops and the output looks like this: I am expecting — hoping for this: I don’t understand why it is stopping. thanks. Answer Your problem lies here: You are
PHP retrieving array values using dash arrow “->”
I’ve been using PHP quite a while now, but never been an advanced programmer. I feel like this is dumb question but never understood why some array values can be retrieved using different methods: This: rather than normal: The standard $array[‘value’] always works, but the one using the -> method doesn’t at times. Why is that? Here’s an example. I
Convert array objects to string and separate the values
I have the following result What i want is to separate the object array values using a comma and return as a string, so as to have this result: I have tried the following This return : How best can this be achieved? Answer You are missing the fact that you are dealing with an array of objects. Looks like
PHP array – explode some comma separated elements into new items
How can I take an array with individual strings but also comma separated strings and explode the comma separated items into individual items? For instance: $product_array[] returns: How can I split item 3 into individual items 3, 4 and 5 so that $product_array returns: Answer Here is fast solution 🙂 if there is comma it will explode it before adding
What is better to use: in_array or array_unique?
I am in doubt what to use: foreach(){ // ….. if(!in_array($view, $this->_views[$condition])) array_push($this->_views[$condition], $view); // …. } OR foreach(){ /…
Collect all values in a multidimensional array with a specific key
I have an array like this: How can I get all [id] values from this array and store them in a flat array like this: Answer $a is your original array.
Sort array and keep values of keys
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 uasort function,