This is what i've got now:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 53
[date] => 18 Sep 2010 10:29
[user] => 52
[post] => ytiuy
)
[1] => Array
(
[id] => 55
[date] => 11 Sep 2010 11:14
[user] => 52
[post] => this is a test post :]
)
)
[1] => Array
(
[0] => Array
(
[id] => 56
[date] => 4 Sep 2010 03:19
[user] => 55
[post] => pppost :DD:D:D:D
)
)
)
I want to remove the first two "steps" in the array, and then sort the array by the 'date' value, like this:
Array
(
[0] => Array
(
[id] => 56
[date] => 4 Sep 2010 03:19
[user] => 55
[post] => pppost :DD:D:D:D
)
[1] => Array
(
[id] => 55
[date] => 11 Sep 2010 11:14
[user] => 52
[post] => this is a test post :]
)
[2] => Array
(
[id] => 53
[date] => 18 Sep 2010 10:29
[user] => 52
[post] => ytiuy
)
)
Any ideas?
Thanks a bunch, appreciate all help! :)
EDIT: I should also mention that the amount of arrayitems will not always be the same.
You should be able to use an accumulator pattern with the array_merge function to merge all the lower level arrays together.
$result = array();
foreach ($oldarray as $child)
{
$result = array_merge($result, $child);
}
Finally, you can use the user defined sort function to sort the whole thing.
An alternative to Don Kirby's solution would be to use an SplMaxHeap which would allow you to iterate and sort in one go:
class PostHeap extends SplMaxHeap
{
public function compare($post, $other)
{
return strtotime($post['date']) - strtotime($other['date']);
}
}
$postHeap = new PostHeap;
foreach($posts as $subArray) {
foreach($subArray as $post) {
$postHeap->insert($post);
}
}
The $postHeap would then contain the posts in descending date order, e.g. newest date first. You can use the code in the compare function if you want to use usort instead. The order will be ascending then though.
Do you have two arrays? Or more? Are they already sorted? If so, you can use that to combine them more efficiently. If not, you probably need to sort them first.
Roughly:
Sort your input arrays (optionally)
Scan your input arrays for the lowest value, copy that value into your new array, delete the value from the input array.
Repeat until all your input arrays are empty.
Of course, if you don't care about performance at all you could simply combine all the arrays and then sort that.
And for sorting you may want to use: http://www.php.net/manual/en/function.sort.php#99700
#Don Kirkby: Indeed: It's basically a mergesort, but it only works on already sorted arrays. If they're both unsorted you're probably better off with combining them and using quicksort instead.
Related
Array
(
[0] => Array
(
[0] => 17
[1] => 111
)
[1] => Array
(
[0] => 17
[1] => 10
)
[2] => Array
(
[0] => 20
[1] => 111
)
)
I want to convert this array as:
array
(
[17]=>121
[20]=>111
)
is there any php array function which can do it easily. I know the other way, but want to know if any ready made function can do that or not??
Please Help.
Here I actually wanted to convert
[0] => Array
(
[0] => 17
[1] => 111
)
17 to key and take 111 as value then in next array
[1] => Array
(
[0] => 17
[1] => 10
)
do the same thing get first value 17 as key and add 10 into previous value 111
which is 121 and then
[2] => Array
(
[0] => 20
[1] => 111
)
take 20 as key and assign value 111 to that key so basically, I want first value as a key and second value as value and for all same keys I want to add values as I stated before.
I thought there might be any PHP ready-made function for that as I have seen there are lots of array processing functions available in PHP. Now, I realized there is no any such kind of function available. I can do my own custom code for this purpose but was looking for good logical solution.
No builtin function for that but it is a simple foreach loop. Assume your array is stored in variable $arr;
$return = array();
foreach ($arr as $a) {
$return[$a[0]] += $a[1];
}
echo "<pre>"; print_r($return);
if you are calling multiple times then you can easily write your own function
$arr[0]= array(17,111);
$arr[1]= array(17,10);
$arr[2]= array(20,111);
$return = subArr($arr);
echo "<pre>"; print_r($return);
function subArr($arr) {
$result = array();
foreach ($arr as $a) {
$result[$a[0]] += $a[1];
}
return $result;
}
I have an array which has a timestamp consistently on [3] ( an example of the array data below)
I would like the array to be sorted using the timestamp. I've seen a few stackoverflow posts that apparently do this using two methods, array_multisort() and usort() but I've been unable to replicate either.
Here is what I've tried based on my own code:
Attempt 1 - I pass the array to usort, then attempt to take it apart with the foreach. Applying this method definitely changes the order of the results, but the dates seems to be no specific order (ascending, descending).
function sortArray($a1, $a2){
if ($a1[3] == $a2[3]) return 0;
return ($a1[3] > $a2[3]) ? -1 : 1;
}
usort($the_array, "sortArray");
foreach($the_array as $sh) {
$uid = $sh['uid'];
$username = $sh['username'];
$datetime = $sh['datetime'];
$type = $sh['type'];
echo "<p> $uid , $username, $datetime, $type </p>";
}
Attempt 2 - tried using array_multisor() which again gives me results in a different order but I don't understand what it is sorting by exactly.
foreach ($the_array as $key => $node) {
$timestamps[$key] = $node[3];
}
array_multisort($timestamps, SORT_ASC, $the_array);
//process the array with a foreach to show results
My theory here is that it isn't properly processing the unix timestamp and I'm not sure what I can do about that. Is it smart to take out all the characters of the timestamp so it is a simple line of numbers ( 2014-01-02 03:02:12 becomes 20140102030212 )? Or is there another way to process it with the timestamp in it's current form?
Here is an example of the data in the array:
Array
(
[0] => Array
(
[uid] => 20013
[0] => 20013
[username] => myhipswontlie
[1] => myhipswontlie
[rating] => 4.00
[2] => 4.00
[datetime] => 2014-01-27 23:40:56
[3] => 2014-01-27 23:40:56
[type] => rated
[4] => rated
)
[1] => Array
(
[uid] => 20025
[0] => 20025
[username] => brasilchika
[1] => brasilchika
[rating] => 4.00
[2] => 4.00
[datetime] => 2014-01-02 03:02:12
[3] => 2014-01-02 03:02:12
[type] => rated
[4] => rated
)
[2] => Array
(
[uid] => 10002
[0] => 10002
[username] => crtten
[1] => crtten
[datetime] => 2014-01-25 01:33:34
[2] => 2014-01-25 01:33:34
[type] => visits
[3] => visits
)
)
Your issue is your numeric keys don't seem to match up in your arrays. Sometimes the datetime is found in the 3rd key, sometime it is found in the second key (which is odd considering they look like they come from mysql_fetch_array() which will make uniform arrays).
To solve this you should use the associative array key instead:
function sortArray($a1, $a2){
if ($a1['datetime'] == $a2['datetime']) return 0;
return ($a1['datetime'] > $a2['datetime']) ? -1 : 1;
}
usort($the_array, "sortArray");
I want to change the number in the first array in a multidimensional array. I have a code that outputs the value to an array and there is no chance for it to start counting from one - in my code. So my idea is to change the value starting from one - after it has been declared. My array look like this:
Array
(
[53] => Array
(
[name] => Volkswagen
[regularePrice] => 2139.00
)
[54] => Array
(
[name] => BMW
[regularePrice] => 2219.00
)
[55] => Array
(
[name] => Chrysler
[regularePrice] => 2399.00
)
)
I want - through a while or for - go through the array and change the values 53 to 1, 54 to 2, 55 to 3 and so on depending on how long the array is.
How do I accomplish this?
The answer is:
array_values($arr);
did you try:
$array = array_values($array);
I var_dump and array and got a value printed, how do i create an array from the result. the array is generated a method and i clearly dont know the structure of the array.
Array ( [0] => gapiReportEntry Object ( [metrics:gapiReportEntry:private] => Array ( [visits] => 4 ) [dimensions:gapiReportEntry:private] => Array ( [year] => 2011 [month] => 07 [day] => 20 ) ) [1] => gapiReportEntry Object ( [metrics:gapiReportEntry:private] => Array ( [visits] => 32 ) [dimensions:gapiReportEntry:private] => Array ( [year] => 2011 [month] => 07 [day] => 13 ) ))
the above is the var_dump result.
I tried to recreate it
$nuarr = array();
$nuarr[0] = array("metrics:gapiReportEntry:private"=>array("visits"=>4),"dimensions:gapiReportEntry:private"=>array("year"=>2011,"months"=>07,"day"=>20));
$nuarr[1] = array("metrics:gapiReportEntry:private"=>array("visits"=>10),"dimensions:gapiReportEntry:private"=>array("year"=>2011,"months"=>07,"day"=>10));
but it doesn't return the same var_dunp value.
Could anyone structure the array for me...
Just assign the new array using assignment operator =
$nuarr = $first_array;
Now the $nuarr is an identical copy of your $first_array.
You can also use the var_export
$nuarr = var_export($first_array, true);
You don't mention why you want to do this. If what you need is just an array-to-string and vice versa mechanism, consider using serialize() and unserialize() rather than var_dump().
If you want to print out an array so that you can clearly see its structure, couldn't you do the following?
echo '<pre>'.print_r($array,1)',</pre>';
I know it isn't using var_dump(), but it would produce the desired result, wouldn't it?
Thought I will point out first that I have looked around on Stackoverflow and the internet already and although they are plenty of examples none of them are explained into such a way for me to understand how to convert the code to work with my array structure.
I believe I need to use one of the functions uksort or uasort but unsure on this as well.
My array looks like the following.
Array
(
[0] => Array
(
[Result] => Array
(
[id] => 260
[event_id] => 72
[year] => 2010
[york_score] => 27
[york_points] => 0.0
[lancs_score] => 51
[lancs_points] => 4.0
[winner] => L
[confirmed] => Y
[updated] => 2010-05-01 16:10:03
)
[Event] => Array
(
[id] => 72
[sport_id] => 25
[event_title] => 1st Team
[Sport] => Array
(
[id] => 25
[sport_title] => Netball
)
)
)
And where its [0] means it continues on.
I need to sort all of the arrays [0,1,2,3,...] by the sport_title key found in [Event][Sport]
Does anyone know how to create a sorting function to do this?
Some explanation of how the function is working would also be helpful if I later need to adapter/alter the code to work else where on my site.
Where $array is the name of the array which holds the array you posted in your question.
function sort_multi($item_1, $item_2)
{
// strcmp looks at two strings and, based off the characters' and their order,
// determines which one is numerically greater. When this function returns a
// negative, for example, it means the first item it's comparing is less that the
// second item (ef and eg, for example). The usort function then rearranges
// the array based off these comparisons.
return strcmp($item_1['Event']['Sport']['sport_title'], $item_2['Event']['Sport']['sport_title']);
}
usort($array, 'sort_multi');