stma
May 30, 2010, 5:59pm
1
I’m trying to grab the last categoryId from an array. End doesn’t seem to be working.
Here is a print_r of what I’m working with… any suggestions?
Array ( [0] => Array ( [categoryId] => 5 [parentId] => 0 [description] => Common Issues [categoryDescription] => [categoryName] => Common Issues [htmlUrl] => http://domain.com/folder/category/common-issues/ [rssUrl] => http://domain.com/folder/category/common-issues/feed/ ) [1] => Array ( [categoryId] => 3 [parentId] => 0 [description] => features [categoryDescription] => [categoryName] => features [htmlUrl] => http://domain.com/folder/category/features/ [rssUrl] => http://domain.com/folder/category/features/feed/ ) [2] => Array ( [categoryId] => 8 [parentId] => 0 [description] => installation [categoryDescription] => [categoryName] => installation [htmlUrl] => http://domain.com/folder/category/installation/ [rssUrl] => http://domain.com/folder/category/installation/feed/ ) [3] => Array ( [categoryId] => 11 [parentId] => 0 [description] => Installation Instructions [categoryDescription] => [categoryName] => Installation Instructions [htmlUrl] => http://domain.com/folder/category/installation-instructions/ [rssUrl] => http://domain.com/folder/category/installation-instructions/feed/ ) [4] => Array ( [categoryId] => 1 [parentId] => 0 [description] => DESCRIPTION STUFF [categoryDescription] => [categoryName] => DESCRIPTION STUFF [htmlUrl] => http://domain.com/folder/category/path/ [rssUrl] => http://domain.com/folder/category/path/feed/ ) [5] => Array ( [categoryId] => 6 [parentId] => 0 [description] => Support [categoryDescription] => [categoryName] => Support [htmlUrl] => http://domain.com/folder/category/support/ [rssUrl] => http://domain.com/folder/category/support/feed/ ) [6] => Array ( [categoryId] => 7 [parentId] => 0 [description] => Tutorials [categoryDescription] => [categoryName] => Tutorials [htmlUrl] => http://domain.com/folder/category/tutorials/ [rssUrl] => http://domain.com/folder/category/tutorials/feed/ ) [7] => Array ( [categoryId] => 12 [parentId] => 0 [description] => Updates [categoryDescription] => [categoryName] => Updates [htmlUrl] => http://domain.com/folder/category/updates/ [rssUrl] => http://domain.com/folder/category/updates/feed/ ) )
stma
June 2, 2010, 1:49am
2
In my case I probably worded that wrong. The asort method is returning the highest record for me… but in my data it’s always also the last.
I’m grabbing categories from a WP blog - so it’s working for my needs as of now. Is something inherently wrong with this:
$categories = $categories[asort($categories)][‘categoryId’];
asort returns a boolean value of true or false, depending on whether it has successfully sorted the array or not.
So it seems that the following is what is occurring:
$categories = $categories[TRUE][‘categoryId’];
which may also be interpreted as
$categories = $categories[1][‘categoryId’];
The following code demonstrates this:
<?php
$categories = array(
0 => array('categoryId' => 6),
1 => array('categoryId' => 7),
2 => array('categoryId' => 8),
3 => array('categoryId' => 5)
);
$categories = $categories[asort($categories)]['categoryId'];
var_dump($categories);
?>
$categories equals 7, which is in the [1] index of the original array.
Okay, let’s work backwards on this.
The max function gives you the highest value from an array. So if you had an array such as
$categoryIds = array(5, 3, 8, 11, 1, 6, 7, 12, 4);
then you can get the max with
$categoryIds = array(5, 3, 8, 11, 1, 6, 7, 12, 4);
$maxCategoryId = max($categoryIds);
// $maxCategoryId is now 12
So now all you need to do is to create that array, which is also a simple task.
$categoryIds = array();
foreach ($data as $row) {
$categoryIds[] = $row['categoryId'];
}
Now we can put those together to come up with an appropriate solution.
// $data comes from the database request
$categoryIds = array();
foreach ($data as $row) {
$categoryIds[] = $row['categoryId'];
}
$maxCategoryId = max($categoryIds);
// $maxCategoryId is now the highest categoryId value.
This… doesnt work, as far as i know.
Scallio or someone correct me if i’m wrong, but to me this line seems to work as follows:
asort($categories) returns 1, after ‘sorting’ the array (If i’m not mistaken, asort on a multidimensional array sorts by number of items in the sub-arrays). Assuming all of the elements of categories have the same number of elements, no sort is actually performed.
$categories gets assigned $categories[1][‘categoryId’], whatever that happens to be.
stma
May 30, 2010, 6:40pm
6
That makes sense. Thank you.
I started playing a little more after I wrote my earlier question. I’m actually trying to get the highest numbered category id there is. Figured this might help someone else with a stupid question
($category is the array I showed above)
$categories = $categories[asort($categories)][‘categoryId’];
//Returns just the category id number of the highest one
The last categoryId is $array[7][‘categoryId’]
Or, generically
$array[count($array)-1][‘categoryId’]
Always picks the last one, regardless of the length of the array.