Count values in specific position in multidimensional array - php

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) ));

Related

I have 2d array, and i want to swap the value of the last index of each array

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);

Creating an array reading from different files in PHP

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!

How to sort this type of complex 3d array in php

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"
}
}
}

PHP array_combine() function not working properly?

I'm trying to combine two array in PHP with array_combine() function, but sometimes it working fine and sometimes it's not. I can't understand why it's working like this!
My Code:
var_dump($selectedDuretion);
var_dump($selectedDuretionType);
$combination = array_combine($selectedDuretion, $selectedDuretionType);
return $combination;
Expected OUTPUT:
array(4)
{
[0]=> string(1) "3"
[1]=> string(2) "12"
[2]=> string(1) "4"
[3]=> string(1) "3"
}
array(4)
{
[0]=> string(4) "days"
[1]=> string(4) "days"
[2]=> string(5) "weeks"
[3]=> string(5) "weeks"
}
{"3":"days","12":"days","3":"weeks","4":"weeks"}
Actual OUTPUT :
array(4)
{
[0]=> string(1) "3"
[1]=> string(2) "12"
[2]=> string(1) "4"
[3]=> string(1) "3"
}
array(4)
{
[0]=> string(4) "days"
[1]=> string(4) "days"
[2]=> string(5) "weeks"
[3]=> string(5) "weeks"
}
{"3":"weeks","12":"days","4":"weeks"}
The combination of arrays it shocking, I'll be thankful if anyone tell me why is this happening and how to solve it.
PHP Does not allow you to have duplicate indices in an array while JSON does allow you to have that for whatever reasons.
Since you are trying to convert PHP arrays to JSON your duplicate key gets eliminated. Hence you will have to manually build the JSON string.
$json="";
for($i=0;$i<count($selectedDuration);$i++)
{
$json.='"'.$selectedDuration[$i].'":"'.$selectedDurationType[$i].'",';
}
$json=rtrim($json,",");
$json="{".$json."}";
echo $json;
Output
{"3":"days","12":"days","4":"weeks","3":"weeks"}
Fiddle

Sorting data in an array by keys

Good day.
We have array:
array(3) {
[1]=>
array(9) {
[1]=>
string(12) "aaandroid.ru"
[2]=>
string(1) "0"
[3]=>
string(1) "0"
[4]=>
string(1) "0"
[5]=>
string(1) "0"
[6]=>
string(1) "0"
[7]=>
string(5) "Test2"
[8]=>
string(10) "2012-03-27"
[9]=>
string(10) "2013-04-29"
}
[2]=>
array(9) {
[1]=>
string(7) "aaga.ru"
[2]=>
string(1) "0"
[3]=>
string(1) "0"
[4]=>
string(1) "0"
[5]=>
string(1) "0"
[6]=>
string(1) "0"
[7]=>
string(8) "Test1"
[8]=>
string(10) "2008-02-21"
[9]=>
string(10) "2013-04-29"
}
[3]=>
array(9) {
[1]=>
string(10) "aatrakc.ru"
[2]=>
string(1) "0"
[3]=>
string(1) "0"
[4]=>
string(1) "0"
[5]=>
string(1) "0"
[6]=>
string(1) "0"
[7]=>
string(8) "Test3"
[8]=>
string(10) "2012-03-27"
[9]=>
string(10) "2013-04-29"
}
Tell me please how sort data in array with key?
For example i would like get array where data sorting on element 7, ie. in result i would like get array:
array(3) {
[1]=>
array(9) {
[1]=>
string(7) "aaga.ru"
[2]=>
string(1) "0"
[3]=>
string(1) "0"
[4]=>
string(1) "0"
[5]=>
string(1) "0"
[6]=>
string(1) "0"
[7]=>
string(8) "Test1"
[8]=>
string(10) "2008-02-21"
[9]=>
string(10) "2013-04-29"
}
[1]=>
string(12) "aaandroid.ru"
[2]=>
string(1) "0"
[3]=>
string(1) "0"
[4]=>
string(1) "0"
[5]=>
string(1) "0"
[6]=>
string(1) "0"
[7]=>
string(5) "Test2"
[8]=>
string(10) "2012-03-27"
[9]=>
string(10) "2013-04-29"
}
[3]=>
array(9) {
[1]=>
string(10) "aatrakc.ru"
[2]=>
string(1) "0"
[3]=>
string(1) "0"
[4]=>
string(1) "0"
[5]=>
string(1) "0"
[6]=>
string(1) "0"
[7]=>
string(8) "Test3"
[8]=>
string(10) "2012-03-27"
[9]=>
string(10) "2013-04-29"
}
Tell me please it really an how make it?
Check the PHP usort function: http://www.php.net/manual/en/function.usort.php.
It provides (in place) sorting based on a callback, which you can create.
Example:
usort($myArray, function ($a, $b) {
return strcmp($a[7], $b[7]);
});
Continuing Hidde's answer, if you want to reverse the order of the sort you would still use usort(), but you reverse the order of the parameters in the call to strcmp(). This will reverse which of the two arrays usort() sees as the larger value.
usort($myArray, function ($a, $b) {
return strcmp($b[7], $a[7]);
});
This uses an anonymous function, so it will only work on Php 5.3 or later. If you have to work on 5.2 define a function to use as the callback.
function mySortFunction($a, $b) {
return strcmp($b[7], $a[7]);
}
usort($myArray, 'mySortFunction');
See:
http://php.net/manual/en/function.usort.php
http://php.net/manual/en/function.strcmp.php

Categories