I have an array stored in a value: $data
The array's structure is as follows:
[0]=>
array(1447) {
[0]=>
array(3) {
[0]=>
string(10) "ga:country"
[1]=>
string(7) "ga:date"
[2]=>
string(11) "ga:sessions"
}
[1]=>
array(3) {
[0]=>
string(11) "Afghanistan"
[1]=>
string(8) "20151129"
[2]=>
string(1) "1"
}
[2]=>
array(3) {
[0]=>
string(7) "Algeria"
[1]=>
string(8) "20160413"
[2]=>
string(1) "1"
}
[3]=>
array(3) {
[0]=>
string(6) "Angola"
[1]=>
string(8) "20160511"
[2]=>
string(1) "1"
}
[4]=>
array(3) {
[0]=>
string(6) "Angola"
[1]=>
string(8) "20160524"
[2]=>
string(1) "5"
}
The array has 1400 entries and I need to make a top 5 of countries
[0]=>
string(11) "Afghanistan"
with highest values:
[2]=>
string(1) "1"
The top must also be of the entries from the last 30 days
I tried to achieve that like this:
function last30days($data)
{
foreach( $data->data[0] as $key => $item ){
$days = [];
for ($i = 1; $i <= 30
; $i++) {
$days[] = date("Y-m-d", strtotime(date('Y-m-01') . " -$i days"));
}
return $days;
}
But I don't know how to make the top 5 values of
[2]=>
string(1) "1"
. Also tell me please if I have approached the problem properly with that function. Thank you in advance.
So to make it clear I need to make top of [2] for every country...in this example Angola has the highest value
[2]=>
string(1) "5"
I can reach that value like this:
$data->data[0][4][2]
And I get 5
I have to loop through the array for the [2] value of every country and get the 5 highest values and the name of those 5 countries.
Related
I'm trying the following,
I have different files (8 files) that stores values like this:
1 2 3 4 5 6
7 8 9 10 11 12
....................
I would like to read 8 files at the same time and create an array that will store the first value of each file, another array with the second element, and so on.
For example, if first element of file1 is 1, in file2 8, ..., in file8 23; the resulting array in this iteration would be:
first_array = [1, 8, ....., 23]
I was making some tests on reading files in PHP like this:
$myfile = fopen("textoPrueba.txt", "r",
"/home/berni/Documentos/Vesta1");
// Output one character until end-of-file
while(!feof($myfile))
{
echo fgetc($myfile);
}
fclose($myfile);
This code just show me the elements of a file, but I would like to take specific elements in an iteration.
Someone can give me a hint? Thanks in advance
(NOTE: files have more than a million of elements)
Another option is that, after we would file_get_content or read our files, we would use a simple expression and collect our numbers using a preg_match_all:
$re = '/([0-9]+)/m';
$str = '1 2 3 4 5 6
7 8 9 10 11 12
1 2 3 4 5 6
1 2 3 4 5 6
';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
foreach ($matches as $key => $numbers) {
foreach ($numbers as $key2 => $number) {
echo $number . "\n";
}
}
var_dump($matches);
array(24) {
[0]=>
array(2) {
[0]=>
string(1) "1"
[1]=>
string(1) "1"
}
[1]=>
array(2) {
[0]=>
string(1) "2"
[1]=>
string(1) "2"
}
[2]=>
array(2) {
[0]=>
string(1) "3"
[1]=>
string(1) "3"
}
[3]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "4"
}
[4]=>
array(2) {
[0]=>
string(1) "5"
[1]=>
string(1) "5"
}
[5]=>
array(2) {
[0]=>
string(1) "6"
[1]=>
string(1) "6"
}
[6]=>
array(2) {
[0]=>
string(1) "7"
[1]=>
string(1) "7"
}
[7]=>
array(2) {
[0]=>
string(1) "8"
[1]=>
string(1) "8"
}
[8]=>
array(2) {
[0]=>
string(1) "9"
[1]=>
string(1) "9"
}
[9]=>
array(2) {
[0]=>
string(2) "10"
[1]=>
string(2) "10"
}
[10]=>
array(2) {
[0]=>
string(2) "11"
[1]=>
string(2) "11"
}
[11]=>
array(2) {
[0]=>
string(2) "12"
[1]=>
string(2) "12"
}
[12]=>
array(2) {
[0]=>
string(1) "1"
[1]=>
string(1) "1"
}
[13]=>
array(2) {
[0]=>
string(1) "2"
[1]=>
string(1) "2"
}
[14]=>
array(2) {
[0]=>
string(1) "3"
[1]=>
string(1) "3"
}
[15]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "4"
}
[16]=>
array(2) {
[0]=>
string(1) "5"
[1]=>
string(1) "5"
}
[17]=>
array(2) {
[0]=>
string(1) "6"
[1]=>
string(1) "6"
}
[18]=>
array(2) {
[0]=>
string(1) "1"
[1]=>
string(1) "1"
}
[19]=>
array(2) {
[0]=>
string(1) "2"
[1]=>
string(1) "2"
}
[20]=>
array(2) {
[0]=>
string(1) "3"
[1]=>
string(1) "3"
}
[21]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "4"
}
[22]=>
array(2) {
[0]=>
string(1) "5"
[1]=>
string(1) "5"
}
[23]=>
array(2) {
[0]=>
string(1) "6"
[1]=>
string(1) "6"
}
}
Demo
Making the following assumptions:
each file has a string of numbers separated by spaces
numbers are not bigger than a billion
code to extract the first element of each file, starting with the code you've provided:
$myfile = fopen("textoPrueba.txt", "r", "/home/berni/Documentos/Vesta1");
// get the first 10 elements one at a time
$str = array();
for($i=0; $i<10; $i++) {
// get the first 10 elements one at a time
$str[] = fgetc($myfile);
}
fclose($myfile);
// squish them into a single string
$temp = join($str);
// explode it into an array separated by spaces
$split = explode(' ', $temp);
// get the first number
$first_element_of_file = $split[0];
For learning purposes, this will do what you asked. I make no claims about it being the best way to approach it!
The array is like this (I am using PHP)
array(2){[0]=>
{[0]=>"DEF"=>
{[0]=>"a",[1]=>"c",[2]=>"b"},
[1]=>"ABC"=>
{[0]=>"f",[1]=>"d",[2]=>"e"}},
[1]=>
{[0]=>"DEF"=>
{[0]=>"h",[1]=>"i",[2]=>"g"},
[1]=>"ABC"=>
{[0]=>"k",[1]=>"l",[2]=>"j"}
}
}
I wish to be sort it like the first entry i.e. [0] index has two entries DEF and ABC so it should be sorted ABC and DEF then in ABC also a b c should be sorted.
The final result should be this
array(2){[0]=>
{[0]=>"ABC"=>
{[0]=>"d",[1]=>"e",[2]=>"f"},
[1]=>"DEF"=>
{[0]=>"a",[1]=>"b",[2]=>"c"}},
[1]=>
{[0]=>"ABC"=>
{[0]=>"j",[1]=>"k",[2]=>"l"},
[1]=>"DEF"=>
{[0]=>"g",[1]=>"h",[2]=>"i"}
}
}
Thanks in advance
PHP has a custom sort option. Try this:
http://php.net/manual/en/function.usort.php
I haven't used it but I had found it previously and remember it existed.
Using sort will solve problem
$a = array(
array(
"ABC"=>array("d","e","f"),
"DEF"=>array("a","b","c")
),
array(
"ABC"=>array("j","k","l"),
"DEF"=>array("g","h","i")
)
);
sort($a);
var_dump($a);
The result
array(2) {
[0]=>
array(2) {
["ABC"]=>
array(3) {
[0]=>
string(1) "d"
[1]=>
string(1) "e"
[2]=>
string(1) "f"
}
["DEF"]=>
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
}
}
[1]=>
array(2) {
["ABC"]=>
array(3) {
[0]=>
string(1) "j"
[1]=>
string(1) "k"
[2]=>
string(1) "l"
}
["DEF"]=>
array(3) {
[0]=>
string(1) "g"
[1]=>
string(1) "h"
[2]=>
string(1) "i"
}
}
}
So I have an array like this:
array(6) { [0]=> string(11) "12323423423" [1]=> string(4) "tito" [2]=> string(6) "235345" [3]=> string(14) " 564534534534" [4]=> string(5) "kralj" [5]=> string(6) "435345" }
Depending on number of elements from another array called $anotherArray, let's say $anotherArray has 3 elements, I should take first 3 elements of first array, then if there are second 3 elements and so on, and put them into another array. I tried it like so:
$lengthManuelni=count($string);// $string being array displayed uphere
$lengthAnothera=count($anotherArray);
for ($i = 0; $i < $lengthManuelni; $i += $lengthAnothera) {
for ($j = 0; $j < $lengthAnothera; $j++) {
$restructured [$j] = $string[$i + $j];
var_dump($restructured);
}
}
So i would like this $restructured array to look like this:
array(2) { [0]=> string(23) "12323423423,tito,235345" [1]=> string(28) " 564534534534,kralj,435345" }
Instead it when I do var_dump($restructured) it looks like this:
array(1) { [0]=> string(11) "12323423423" } array(2) { [0]=> string(11) "12323423423" [1]=> string(4) "tito" } array(3) { [0]=> string(11) "12323423423" [1]=> string(4) "tito" [2]=> string(6) "235345" } array(3) { [0]=> string(14) " 564534534534" [1]=> string(4) "tito" [2]=> string(6) "235345" } array(3) { [0]=> string(14) " 564534534534" [1]=> string(5) "kralj" [2]=> string(6) "235345" } array(3) { [0]=> string(14) " 564534534534" [1]=> string(5) "kralj" [2]=> string(6) "435345" }
Please help, I'm stuck with this.
It's much simpler to achieve this using array_chunk and array_map functions:
$restructured = array_map(function($v){
return implode(",", $v);
}, array_chunk($lengthManuelni, 3));
print_r($restructured);
The output:
Array
(
[0] => 12323423423,tito,235345
[1] => 564534534534,kralj,435345
)
http://php.net/manual/en/function.array-chunk.php
Good day.
Code:
array(4) {
[0]=> array(1) {
[0]=> array(3) {
[0]=> string(11) "art_7880" [1]=> string(1) "1" [2]=> int(2950)
}
[1]=> array(3) {
[0]=> string(8) "art_7880" [1]=> string(1) "1" [2]=> int(2955)
}
[2]=> array(3) {
[0]=> string(8) "art_7880" [1]=> string(1) "1" [2]=> int(1335)
}
[3]=> array(3) {
[0]=> string(8) "art_7883" [1]=> string(1) "1" [2]=> int(4335)
}
}
I get array unique elements:
$arr_uniq = array();
foreach ($all_array as $keys => $elms ) {
if(!in_array($elms[0], $arr_uniq)) {
$arr_uniq[] = $elms[0];
}
}
Tell me pleasse how to get a count each unique element in the general array?
result should been next:
art_7880 - 3
art_7883 - 1
Assuming $all_array is subarray of your main array in your var_dump snipett, the general idea is
$result = array();
foreach ($all_array as $elms)
$result[$elms[0]]++;
array_count_values()
http://php.net/array_count_values
You should be able to easily apply this function.
Using WP I'm building the breakdown of a score. Each breakdown is a custom select field with values from 1 to 10. The final result i'm trying to achieve is to show the total score inside the WP loop, by calculating the sum of all breakdown scores and dividing it by the number of added breakdown scores.
The problem is that each post i add with the total score it taking the array from a previous posts also. For example:
Post 1
array(6) { [0]=> array(0) { } [1]=> string(1) "7" [2]=> string(2) "10" [3]=> string(1) "9" [4]=> string(2) "10" [5]=> string(1) "1" }
Post 2
array(9) { [0]=> array(0) { } [1]=> string(1) "7" [2]=> string(2) "10" [3]=> string(1) "9" [4]=> string(2) "10" [5]=> string(1) "1" [6]=> array(0) { } [7]=> string(1) "1" [8]=> string(1) "3" }
Post 3
array(13) { [0]=> array(0) { } [1]=> string(1) "7" [2]=> string(2) "10" [3]=> string(1) "9" [4]=> string(2) "10" [5]=> string(1) "1" [6]=> array(0) { } [7]=> string(1) "1" [8]=> string(1) "3" [9]=> array(0) { } [10]=> string(1) "9" [11]=> string(2) "10" [12]=> string(1) "3" }
As you can see Post 2 got it's own array and the array from Post 1, Post 3 got it's own array and arrays from Post 1 and Post 2.
This is the code i'm using inside the WP while loop
<?php
$score_rows = get_post_meta($post->ID, 'review_score', true);
$score[] = array();
foreach( $score_rows as $row ):
$score[] = $row['score_number'];
endforeach;
$score_items = count( $score );
$score_divide = $score_items;
$score_sum = array_sum( $score );
$score_total = $score_sum / $score_divide;
?>
This code works great in single.php where i have only one post, but what to do where i have more then one? How to prevent from posts to combine arrays like this?
UPDATE
I have found that all the values are strings, maybe this is what causing it act like this. Is it possible to convert the values to integers?
change
$score[] = array();
to
$score = array();