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!
Related
i want the last index value of each array for example 8 and 10 will be swap
array(2) {
[0]=>
array(5) {
[0]=>
string(1) "1"
[1]=>
string(1) "3"
[2]=>
string(1) "5"
[3]=>
string(1) "8"
}
[1]=>
array(5) {
[0]=>
string(1) "1"
[1]=>
string(1) "3"
[2]=>
string(1) "6"
[3]=>
string(2) "10"
}
}
so the result will be
array(2) {
[0]=>
array(5) {
[0]=>
string(1) "1"
[1]=>
string(1) "3"
[2]=>
string(1) "5"
[3]=>
string(2) "10"
}
[1]=>
array(5) {
[0]=>
string(1) "1"
[1]=>
string(1) "3"
[2]=>
string(1) "6"
[3]=>
string(1) "8"
}
}
if there are two value or more (even, not odd) will be swap its okay, as long as not all of the value will be swap. the size of array will be dynamic, thats just for example.
it swaps the last value:
$arr1 = array(1, 3, 5, 8);
$arr2 = array(1, 3, 6, 10);
$val1 = end($arr1);
$val2 = end($arr2);
array_pop($arr1);
array_pop($arr2);
array_push($arr1, $val2);
array_push($arr2, $val1);
print_r($arr1);
print_r($arr2);
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"
}
}
}
I have a multidimensional array that looks like this:
array(6) {
[0]=>
array(6) {
[0]=>
string(5) "email"
[1]=>
string(5) "vote1"
[2]=>
string(5) "vote2"
[3]=>
string(5) "vote3"
[4]=>
string(5) "vote4"
[5]=>
string(5) "vote5"
}
[1]=>
array(6) {
[0]=>
string(5) "a#a.a"
[1]=>
string(1) "A"
[2]=>
string(1) "B"
[3]=>
string(1) "C"
[4]=>
string(1) "D"
[5]=>
string(1) "E"
}
[2]=>
array(6) {
[0]=>
string(5) "b#b.b"
[1]=>
string(1) "A"
[2]=>
string(1) "B"
[3]=>
string(1) "C"
[4]=>
string(1) "D"
[5]=>
string(1) "E"
}
[3]=>
array(6) {
[0]=>
string(5) "c#c.c"
[1]=>
string(1) "A"
[2]=>
string(1) "B"
[3]=>
string(1) "C"
[4]=>
string(1) "D"
[5]=>
string(1) "E"
}
[4]=>
array(6) {
[0]=>
string(5) "d#d.d"
[1]=>
string(1) "A"
[2]=>
string(1) "B"
[3]=>
string(1) "C"
[4]=>
string(1) "D"
[5]=>
string(1) "E"
}
[5]=>
array(6) {
[0]=>
string(5) "e#e.e"
[1]=>
string(1) "A"
[2]=>
string(1) "B"
[3]=>
string(1) "C"
[4]=>
string(1) "D"
[5]=>
string(1) "E"
}
}
I want to count how many times each value occurs at position [1] of each array.
I have tried many things including array_count_values(), but this doesn't allow the option to count occurrences in just a certain position of each array. I'm not quite sure how to go about this issue because the array is multidimensional and I have never had to deal with just counting the values at a specific position.
I apologize if the answer to this is blatantly obvious, I've been working on this issue for a few hours but to no avail, but I also am a beginner when it comes to arrays, especially multidimensional. All ideas are welcome :)
What you can do is, you can loop through the array and put the values of [1] into another array, and apply array_count_values on the resultant array.
$a = array();
foreach ($arr as $ar) {
$a[] = $ar[1];
}
print_r(array_count_values($a));
This will give you:
vote1: 1
A: 5
If the above is what you are looking for? If you want a shorter version, you can also use array_column.
use array_column($array, 1) to get position[1] elements as array; Then use your array_count_values() to count them.
<?php
$array = [[1,2,3],[2,3,4]];
var_dump(array_count_values ( array_column($array,1) ));
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.
I need to count how many times caracter 1, 2 ,3 ,4, 5 is existing in this array e.g. for this i should get answer
1 - two times
2 - two times
3 - one time
4 - three times
<?php
array(8) {
[0]=>
array(2) {
[0]=>
string(1) "3"
["answer"]=>
string(1) "3"
}
[1]=>
array(2) {
[0]=>
string(1) "4"
["answer"]=>
string(1) "4"
}
[2]=>
array(2) {
[0]=>
string(1) "1"
["answer"]=>
string(1) "1"
}
[3]=>
array(2) {
[0]=>
string(1) "1"
["answer"]=>
string(1) "1"
}
[4]=>
array(2) {
[0]=>
string(1) "2"
["answer"]=>
string(1) "2"
}
[5]=>
array(2) {
[0]=>
string(1) "2"
["answer"]=>
string(1) "2"
}
[6]=>
array(2) {
[0]=>
string(1) "4"
["answer"]=>
string(1) "4"
}
[7]=>
array(2) {
[0]=>
string(1) "4"
["answer"]=>
string(1) "4"
}
}
?>
If this is data returned from a database, why not use the database query to do the counting for you?
SELECT answer,
COUNT(1) AS instances
FROM myTable
GROUP BY answer