I want to group data in PHP like so:
I have an array with dumped stuff in it
Array (
[0] => Array (
[0] => 123
[1] => 3000
[2] => 595
[3] => 1005
)
[1] => Array (
[0] => 53
[1] => 700
[2] => 195
[3] => 315
)
[2] => Array (
[0] => 82
[1] => 142
[2] => 342
[3] => 640
)
[3] => Array (
[0] => 82
[1] => 702
[2] => 200
[3] => 320
)
)
This array represents items in 3D. What I want to achieve is to group them by coordinates so that if (roughly) they are close together (let's say closer than 10 units on each axis).
So far I came up with following script: http://pastebin.com/wJ5HpTPL however it seems be failing - it groups some records for no reason at all and don't group those that I want. And I know how inefficient it is however I tried several methods that was failing and it evolved into that crappy code.
There is about 2000 of items however the script is going to be run for administrative purposes and is hosted on my local server so performance is not important.
To calculate the difference you can use Pythagoras' theorem and:
It's explained really well here: http://betterexplained.com/articles/measure-any-distance-with-the-pythagorean-theorem/
I have resolved my problem by simply using $row = $bulk[$i]; in my code instead of $row = next($bulk); which for some reason was causing problems.
Related
Hello im trying to create a semi MVC project and im new to this so what ive done so far and what ill explain as my problem here may be completely bad on itself so sorry if that is the case.
View 1 tables.phtml
View 2 Calc.phtml
View 3 Result.phtml
Controller Tables.php
Model Tables.php
On my first view is an Form where you input 2 numbers to generate an multiplication table and it puts this in an variable called $aTempArray it looks like this:
Array (
[0] => Array ( [0] => 10 [1] => 6 [2] => 60 )
[1] => Array ( [0] => 7 [1] => 6 [2] => 42 )
[2] => Array ( [0] => 10 [1] => 6 [2] => 60 )
[3] => Array ( [0] => 7 [1] => 6 [2] => 42 )
[4] => Array ( [0] => 2 [1] => 6 [2] => 12 )
[5] => Array ( [0] => 1 [1] => 6 [2] => 6 )
[6] => Array ( [0] => 10 [1] => 6 [2] => 60 )
[7] => Array ( [0] => 6 [1] => 6 [2] => 36 )
[8] => Array ( [0] => 3 [1] => 6 [2] => 18 )
[9] => Array ( [0] => 5 [1] => 6 [2] => 30 )
)
On my second view it generates a form based on the array like this:
10 * 6 = ... <(this is a textbox)
7 * 6 = ... <(this is a textbox)
So when the user submits this second form on the second view I want it to OR send the $aTempArray, OR generate a new one like this:
Array (
[0] => Array ( [0] => 10 [1] => 6 ['answer'] => ... )
[1] => Array ( [0] => 7 [1] => 6 ['answer'] => ... )
)
However, I have no clue on how to send the $aTempArray becouse on the second submit it is empty becouse of the new controller/model that gets generated(or just reset becouse it reloads), and also I dont have a specific idea on how to get the sum data back when I submit the form on the second page. (the 10 * 6 for example becouse this is just text at this point.)
Ive tried a lot and found some things that do make this work but I want to know if there are better ways to do this.
1 is Saving the array in a session variable for example works but I want to avoid using sessions.
2 is making hidden input fields where I save the sums numbers without the answer like this: <input type="hidden" name="rebuildArray" value="10,6/7,6 etc">
And to recreate the array I do something like:
Str_split on /
Put in array
str_split on ,
Put this in arrays inside the main array.
Foreach over every item in the array
if((currentsubarray[0] * currentsubarray[1]) == answersarray[currentpos])
Answer is correct
else
answer is not correct
So can someone say if one of these ways is a semi correct way to do it or give me a push in a direction on how to so this correctly in an MVC stylish way.
And again, I just started with MVC so if this is completely wrong in general im sorry.
I have a JSON array which Im trying to parse with PHP using array_column (PHP 5.5).
My objective is to check the value of a particular key in the array and execute some additional code dependent on the result.
For example with the array below...I would like to find field_number 335 in the array and take the value (Last name) and echo to the screen. The actual array [1] could be different each time as the array grows, where as field_number would always be 335.
Array
(
[0] => Array
(
[id] => 286
[lead_id] => 5
[form_id] => 4
[field_number] => 1
[value] => First Name
)
[1] => Array
(
[id] => 287
[lead_id] => 5
[form_id] => 4
[field_number] => 335
[value] => Last name
)
[2] => Array
(
[id] => 288
[lead_id] => 5
[form_id] => 4
[field_number] => 339
[value] => Australia
)
Hopefully that makes sense and with enough information to help someone point me in the right direction.
Many thanks all!
Cheers
you can use array_search && array_column
$key = array_search('335', array_column($array, 'field_number'));
this should give you array id
eg. echo $array[$key]['value'];
I have a problem visualizing arrays greater than one dimension. I did a query on a database table and stored the data in an array, then used mysqli_fetch_array() to create another array. Now this array has the table name and the table data in but I'm having trouble figuring out how to A) access just the data and B) visualize what is actually going on here.
This is the output of print_r($keystore);
Array ( [0] => Array ( [0] => 4 [key_projects] => 4 ) [1] => Array ( [0] => 26 [key_projects] => 26 ) [2] => Array ( [0] => 25 [key_projects] => 25 ) [3] => Array ( [0] => 52 [key_projects] => 52 ) [4] => Array ( [0] => 53 [key_projects] => 53 ) )
What exactly is going on here?
Sometimes it's helpful when developing/debugging code that has arrays, to insert the HTML <pre> tag before and after the print_r() command:
echo "<pre>";
print_r($keystore);
echo "</pre>";
This will force the output into a format similar to John Kugelman's answer (subject to CSS rules). I find, from experience, that each iteration of Array() will be indented, when viewing plain text (i.e. no HTML)
Array
(
[0] => Array ( [0] => 4 [key_projects] => 4 )
[1] => Array ( [0] => 26 [key_projects] => 26 )
[2] => Array ( [0] => 25 [key_projects] => 25 )
[3] => Array ( [0] => 52 [key_projects] => 52 )
[4] => Array ( [0] => 53 [key_projects] => 53 )
)
I've added some whitespace to make the structure of the nested arrays clearer. I didn't change anything aside from adding spaces and newlines.
The outer array contains five entries from [0] to [4]. Each of these entries represents one row from the SQL result set.
for ($keystore as $row) {
print_r($row[0]);
print_r($row['key_projects']);
}
You'll notice the two entries in each $row are redundant. They both have the same value (e.g. 53 for the final entry). What's happening is the data is being returned indexed both by column number (0) and column name (key_projects). You can access the values using whichever one you choose, number or name: $row[0] or $row['key_projects'].
I have the following array :
Array
(
[0] => Array
(
[Name] => first_data
[building] => A
[apt] => 16
)
[1] => Array
(
[Name] => first_data
[building] => B
[apt] => 16
)
[2] => Array
(
[Name] => second_data
[building] => A
[apt] => 17
)
[3] => Array
(
[Name] => second_data
[building] => B
[apt] => 18
)
and I need it to be returned as :
Array
(
[0] => Array
(
[Name] => first_data
[A] => 16
[B] => 16
)
[1] => Array
(
[Name] => second_data
[A] => 17
[B] => 18
)
Any ideas?
BTW the first array has hundreds of entries (not only first_data, but second and etc...) plus it has more than A and B.
Thanks in advance.
Not exactly what you want, but if you instead index the new array by the name, you can do this very easily. If the index number is some kind of ID, you can just create a field for it
foreach ( $oldarray as $index => $piece )
{
$newarray[$piece['Name']] = array($piece['building'] => $piece['apt'])
}
This will give you
Array
(
['first_data'] => Array
(
['A'] => 16,
['B'] => 16
)
['second_data'] => Array
(
['A'] => 17,
['B'] => 18
)
)
Since you have two entries with the same new, when you hit the 2nd loop, it will simply add the other building name. If you can work with this layout, then your solution is very easy, it will take more steps to do it exactly as you showed. If you absolutely have to do it the way you showed, you need extra code to loop through the new array, find the building name, add the key in the correct place, but this will be slower if you have a large amount of data.
In my opinion, the way I presented it is a far easier way to look around the array too. If you wanted to know the apt value for A in "second_data" you can just do
$newarray['second_data']['A']
with your array layout, it would require a loop to search the array for "second_data" because you have no idea where it is.
I am working on an ad management system, and recently, we launched on a high traffic website to do some testing. I am storing each impression as a record in the db with a timestamp, ad ID, zone ID, and website ID.
I would select all the data between a date range, and use PHP to organize the records (by days, etc). That was inefficient so I make two queries that
GROUP BY date(`impressions`.timestamp)
First query to retrieve a count of impressions, second query to get a list of associated labels.
My problem is this:
I need to query to get not just the overall impressions, but the impressions by zone AND by day, and I cannot use PHP to organize this array of data because it is millions of points.
SELECT `zones`.name as zoneName, `impressions`.*
from `impressions`, `zones`
WHERE `impressions`.website_id = "14"
AND `zones`.website_id="14"
AND `zones`.zone_id=`impressions`.zone_id
AND `impressions`.timestamp
BETWEEN "2012-10-08 00:00:00" AND "2012-10-15 23:59:59"
ORDER BY `impressions`.timestamp ASC
That may return millions of records. Then I use PHP to sort the returned data into zones.
For example, this is the resulting array after my PHP sorting. This array represents the zones and the impressions they received for the given days. What would be a good query to use to get as close to this as possible to minimize memory overhead when dealing with large arrays?
Array
(
[labels] => Array
(
[Oct 8th] => "Oct 8th"
[Oct 9th] => "Oct 9th"
[Oct 10th] => "Oct 10th"
)
[final] => Array
(
[0] => Array
(
[name] => Blog Skyscraper
[data] => Array
(
[0] => 449
[1] => 499
[2] => 558
)
)
[1] => Array
(
[name] => Latest News Right
[data] => Array
(
[0] => 805
[1] => 809
[2] => 760
)
)
[2] => Array
(
[name] => Photos Right
[data] => Array
(
[0] => 788
[1] => 786
[2] => 743
)
)
[3] => Array
(
[name] => Banner Zone
[data] => Array
(
[0] => 793
[1] => 796
[2] => 747
)
)
[4] => Array
(
[name] => Mini Right Bottom
[data] => Array
(
[0] => 784
[1] => 778
[2] => 742
)
)
[5] => Array
(
[name] => Mini Right Top
[data] => Array
(
[0] => 790
[1] => 787
[2] => 743
)
)
)
)