Can anyone please help me out with that one. I'm getting the locale of Facebook page fans. The output from Facebook is in JSON format. After I json_decode($fb_output) I end up with array below. I'd like to echo a list with the top 10 languages (or all if there are less than 10) and the value in the key(number of people who speak that language). So far I've unsuccessfully tried to get it with foreach ($fb_output $key => $value)
Here is the array:
Array (
[0] => stdClass Object (
[value] => stdClass Object (
[de_DE] => 8527
[en_US] => 313
[en_GB] => 147
[tr_TR] => 106
[it_IT] => 79
[sr_RS] => 25
[hu_HU] => 24
[es_ES] => 15
[bs_BA] => 12
[es_LA] => 12
[sk_SK] => 11
[ro_RO] => 10
[ru_RU] => 9
[pt_BR] => 9
[nl_NL] => 8
[hr_HR] => 8
[fr_FR] => 7
[sv_SE] => 5
[cs_CZ] => 5
[bg_BG] => 5
)
[end_time] => 2012-03-05T08:00:00+0000 ) )
The number of keys in this array varies from user to user so it'd need to be flexible.
How about something like this:
$topTen = array_slice(array_keys( (array) $fb_output[0]->value ), 0, 10);
What it does:
Transforms the stdClass Object into an native array. ((array) operator)
Turns that array into another array, containing only its keys. (array_keys() function)
Extracts the first ten entries of that array. (array_slice() function)
If you want to extract the top ten including their values, skip the array_keys part and make sure, that the keys of the array don't get mixed up while slicing it (4th parameter of that function, see the docs for more information):
$topTen = array_slice( (array) $fb_output[0]->value, 0, 10, true);
echo $topTen['en_GB']; // 147
Related
This question already has answers here:
PHP Associative Array Duplicate Keys
(6 answers)
Closed last month.
I have an array with multiple key.
Array 1
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
Output
Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
Expected Output
$age = array("value"=>"35", "value"=>"37", "value"=>"43");
Array ( [value] => 35 [value] => 37 [value] => 43 )
You can't
Indeed, array keys must be unique. Otherwise, what should the program output when you try to access value ?
But ...
If you need to store a list of value for one key, you can use array of arrays.
$array = array("value" => array());
array_push($array["value"], 35, 40, 53);
print_r($array)
And the output will be:
Array
(
[value] => Array
(
[0] => 35
[1] => 40
[2] => 53
)
)
Only way is to turn this array into 2D array:
$age = array(
array("value" => "35"),
array("value" => "37"),
array("value" => "43")
);
-- Output --
Array
(
[0] => Array
(
[value] => 35
)
[1] => Array
(
[value] => 37
)
[2] => Array
(
[value] => 43
)
)
-- Usage --
$age[0]['value'];
$age[1]['value'];
$age[2]['value'];
But it completely depends if $age array is in our control and can be changed.
You can't
Array keys must be unique.
http://php.net/manual/en/language.types.array.php
If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten.
Yes, array key should be unique. So, whatever you are asking it's not possible. Can you tell us what's requirements? So that, people can suggest any alternate solution.
This cannot be possible with native php arrays. What you need is a multimap, and you can find several implementations of it on github.
For example: link
EDIT: the link above is an interface. you need to include also link2
Array(
[User1] => 162
[User2] => 15
[User3] => 158
[User4] => 92
[User5] => 2
[User6] => 3
[User7] => 2
[User8] => 25
[User9] => 10
[User10] => 6
[User11] => 14
)
Above is my array of arrays. This list is completely dynamic - the values(numbers) change, and all of the Users are not numbers, but have actual user names - they don't say User1, they say instead like TomJohnson4512 - on and on, they're all different
All I want to do, is some sort of loop to add up and display/print/echo the values. I don't care about the user names, all I want is a sum of the values.
Any ideas?
All you need to do is use array_sum ( $array ) so assuming your array is called $array, do this
$total = array_sum ( $array );
I have tried array_merge, to merge them based on similar keys, array_push, various [] combinations but I just can't figure this one out. I have two arrays, one looks like:
Array
(
[650] => Array
(
[Kampan] =>
[ZelvaUL] => 650
[ZelvaOV] =>
[OCS] =>
[Rezim] => Ruční
)
[651] => Array
(
[Kampan] => 3003C_DSL_upsell_TV_SU
[ZelvaUL] => 651
[ZelvaOV] =>
[OCS] => 21
[Rezim] => IN
)
[652] => Array
(
[Kampan] =>
[ZelvaUL] => 652
[ZelvaOV] =>
[OCS] => 22
[Rezim] => IN
)
And, I want to add one new key to each of 650, 651, 652... sub-arrays (I will call the key 'Barva'), and short set of values from another array (10 total) to periodically loop in each sub-array under that key, so that 1st and 11th value is the same, 2nd and 12th is the same and so on, and all to be under the same key.
It would look like:
Array
(
[650] => Array
(
[Kampan] =>
[ZelvaUL] => 650
[ZelvaOV] =>
[OCS] =>
[Rezim] => Ruční
[Barva] => 1
)
[651] => Array
(
[Kampan] => 3003C_DSL_upsell_TV_SU
[ZelvaUL] => 651
[ZelvaOV] =>
[OCS] => 21
[Rezim] => IN
[Barva] => 2
)
[652] => Array
(
[Kampan] =>
[ZelvaUL] => 652
[ZelvaOV] =>
[OCS] => 22
[Rezim] => IN
[Barva] => 3
)
...
[660] => Array
(
[Kampan] => ...
[ZelvaUL] => ...
[ZelvaOV] => ...
[OCS] => ...
[Rezim] => ...
[Barva] => 1
)
Seriously, I am out of ideas...
Thanks for any help guys.
edit:
This is the array I want to add:
$camp_barvy = array(
'background-color:#ffffff;color:#111111;',
'background-color:#ffcc02;color:#111111;',
'background-color:#ff7700;color:#ffffff;',
'background-color:#ff2323;color:#ffffff;',
'background-color:#ff00aa;color:#ffffff;',
'background-color:#aa44ff;color:#ffffff;',
'background-color:#1188ff;color:#ffffff;',
'background-color:#11ddff;color:#111111;',
'background-color:#00dd77;color:#111111;',
'background-color:#119911;color:#ffffff;'
);
I wanna do some large and extensive conditioned formatting and both javascript and php if statement make the loading too slow, so I figured I will make the format part of the array I already look in for the values based on which I choose the desired format.
Really, its the best choice :)
What you want to do is iterate over each value in your "input" array and insert in it a new value taken from your "data" array (those 10 values you mention). When your data array is exhausted, you want to loop back to its start and continue inserting values in the "input" array elements.
So you want something like:
foreach ($input as &$row) {
$row['Brava'] = $next_item_from_data_array;
}
which leaves just the problem of how to easily iterate and loop over the data array.
A convenient and modern way of doing this is by using the built-in SPL iterators: an ArrayIterator for your data array and an InfiniteIterator around that so that you loop back to the start automatically as required. This way you also don't have to assume anything about your data array (such as if it is numerically indexed or not).
For example:
$dataIterator = new InfiniteIterator(new ArrayIterator($data));
$dataIterator->rewind();
foreach ($input as &$row) {
$row['Brava'] = $dataIterator->current();
$dataIterator->next();
}
// After iterating by reference (&$row) it is always a good idea to unset
// the reference so that you don't reuse it later on by mistake -- although
// this is not required and the program will work correctly without it.
unset($row);
See it in action.
I looked at different options how to sort arrays. But somehow none of the given PHP commands suit my purpose.
Example - I have an array like this :
Array
(
[abc] => Array
(
[2] => 2
[3] => 3
[5] => 5
)
)
But I want to change the array to
[0] => 2
[1] => 3
[2] => 5
In other words I want to remove all keys - sort all values from LOW to HIGH and then just give em the keys from zero to X
It's much easier to work with such an array if you want to use some loops like (for, while, etc.)
Just use sort and array_values.
<?php
$array = array(
'abc' => array(
2 => 2,
5 => 5,
3 => 3,
),
);
sort($array['abc']);
$array = array_values($array['abc']);
print_r($array);
I've popped up an example at http://3v4l.org/51naW
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');