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.
Related
I'm trying to loop through a multidimensional array with foreach but sometimes there's 5 dimensions and sometimes there's 2, but I need to foreach every array. Here is an example:
array(16) {
["id"]=>
string(2) "1"
["name"]=>
string(1) "Bob"
["job"]=>
array(2) {
[0]=>
string(8) "software"
[1]=>
string(7) "plumber"
}
["kids"]=>
array(2) {
[1]=>
array(2) {
[0]=>
string(4) "Jane"
[1]=>
string(4) "girl"
}
[2]=>
array(2) {
[0]=>
string(3) "Sam"
[1]=>
string(4) "boy"
[2] => array(2) {
[0]=>
string(3) "123"
[1]=>
string(11) "Main Street"
}
}
}
}
you get the point.... but imagine if I had a dimension of 10 in the array. How can I dynamically loop through them and do trim() to each value in the whole array?
Here is what I have so far:
foreach ($array as $key => $value) {
$array[$key] = trim($value);
}
but I need it to go deeper into an array if there is an array and perform trim to all values in my $array.
I have this array, it could look something like this:
array(756) {
[0]=>
array(2) {
[0]=>
string(12) "joint_temps5"
[1]=>
string(4) "23.5"
}
[1]=>
array(2) {
[0]=>
string(12) "joint_temps4"
[1]=>
string(4) "23.5"
}
[2]=>
array(2) {
[0]=>
string(12) "joint_temps3"
[1]=>
string(2) "24"
}
[3]=>
array(2) {
[0]=>
string(12) "joint_temps2"
[1]=>
string(4) "24.5"
}
[4]=>
array(2) {
[0]=>
string(12) "joint_temps1"
[1]=>
string(2) "25"
}
[5]=>
array(2) {
[0]=>
string(12) "joint_temps0"
[1]=>
string(4) "25.5"
}
[6]=>
array(2) {
[0]=>
string(12) "joint_temps5"
[1]=>
string(4) "23.5"
}
[7]=>
array(2) {
[0]=>
string(12) "joint_temps4"
[1]=>
string(4) "23.5"
}
[8]=>
array(2) {
[0]=>
string(12) "joint_temps3"
[1]=>
string(2) "24"
}
[9]=>
array(2) {
[0]=>
string(12) "joint_temps2"
[1]=>
string(4) "24.5"
}
[10]=>
array(2) {
[0]=>
string(12) "joint_temps1"
[1]=>
string(2) "25"
}
[11]=>
array(2) {
[0]=>
string(12) "joint_temps0"
[1]=>
string(4) "25.5"
}
etc...};
How would i go about looping thru and splitting it up into arrays based on the value in the inner arrays[0] ex: "joint_temps5".
I have tested quite a few things but without success. My problem mainly is i dont know what might be in the string in the arrays.
I would like to end up with arrays like:
$array1[] = array(x_amount){
[0]=>
array(2) {
[0]=>
string(12) "joint_temps5"
[1]=>
string(4) "23.5"
}
[1]=>
array(2) {
[0]=>
string(12) "joint_temps5"
[1]=>
string(4) "23.5"
}
}
$array2[] = array(x_amount){
[0]=>
array(2) {
[0]=>
string(12) "joint_temps4"
[1]=>
string(4) "23.5"
}
[1]=>
array(2) {
[0]=>
string(12) "joint_temps4"
[1]=>
string(4) "23.5"
}
}
}
etc.
I would recommend to create a new array from your input array, using the value as an index of the array to be created, like so:
// test-set: input array is $a
$a[0] = array("joint_temps5","23.5");
$a[1] = array("joint_temps3","24");
$a[2] = array("joint_temps2","24.5");
$a[3] = array("joint_temps1","25");
$a[4] = array("joint_temps0","25.5");
$a[5] = array("joint_temps5","23.5");
$a[6] = array("joint_temps4","23.5");
$a[7] = array("joint_temps3","24");
$a[8] = array("joint_temps2","24.5");
$a[9] = array("joint_temps1","25");
foreach($a as $key => $value){
$b[$value[0]][] = $value; // *Explained below
}
*"Explained below": $a is the source array, $b is the newly created array.
$b[$value[0]][] means it wil create a new element for array $b[$value[0]]. And $value[0] will be substituted by the first value in the element of $a that the foreach loop hits.
Example: the first element of $a is this array: array("joint_temps5","23.5"). So in the foreach loop, the text "joint_temps5" ($value[0] in the foreach) will be used as a key/index to create a new element for array $b. The [] means that with every new execution of this line, a new element, with that key value $value[0], will be added.
It will result in:
array(6) {
["joint_temps5"]=>
array(2) {
[0]=>
array(2) {
[0]=>
string(12) "joint_temps5"
[1]=>
string(4) "23.5"
}
[1]=>
array(2) {
[0]=>
string(12) "joint_temps5"
[1]=>
string(4) "23.5"
}
}
["joint_temps3"]=>
array(2) {
[0]=>
array(2) {
[0]=>
string(12) "joint_temps3"
[1]=>
string(2) "24"
}
[1]=>
array(2) {
[0]=>
string(12) "joint_temps3"
[1]=>
string(2) "24"
}
}
["joint_temps2"]=>
array(2) {
[0]=>
array(2) {
[0]=>
string(12) "joint_temps2"
[1]=>
string(4) "24.5"
}
[1]=>
array(2) {
[0]=>
string(12) "joint_temps2"
[1]=>
string(4) "24.5"
}
}
["joint_temps1"]=>
array(2) {
[0]=>
array(2) {
[0]=>
string(12) "joint_temps1"
[1]=>
string(2) "25"
}
[1]=>
array(2) {
[0]=>
string(12) "joint_temps1"
[1]=>
string(2) "25"
}
}
["joint_temps0"]=>
array(1) {
[0]=>
array(2) {
[0]=>
string(12) "joint_temps0"
[1]=>
string(4) "25.5"
}
}
["joint_temps4"]=>
array(1) {
[0]=>
array(2) {
[0]=>
string(12) "joint_temps4"
[1]=>
string(4) "23.5"
}
}
}
You could loop through your array, and populate a new array using the string as a key, so something like:
foreach ($array as $working_array) {
$new_array[$working_array[0]][] = $working_array[1]; }
Which would give you an array something like :
$new_array["joint_temps5"]=> array(2) {
[0]=> "23.5"
[1]=> "23.5"}
If you needed to you could then parse that into an array in the format you desire quite easily.
Im a bit lost with this array here, and i need some "light" if you could.
My situation is: change some static data from a array to one generated by an foreach.
The static php code example is the following:
$p->data = array(
array(
array("A",148), //value_1 for letter A
array("B",238), //value_1 for letter B
array("C",151) //value_1 for letter C
),
array(
array("A",238), //value_2 for letter A
array("B",338), //value_2 for letter B
array("C",285) //value_2 for letter C
),
array(
array("A",278), //value_3 for letter A
array("B",138), //value_3 for letter B
array("C",205) //value_3 for letter C
)
);
My problem is: when i try to generate the same result with a foreach, i'm doing it wrong.. i already tried nested foreach's and get nothing (or doing it wrong too), but this is the closest I reach from the result i need:
foreach($result as $reg){
$p->data = array(
array(
array($reg['letters'],$reg['value_1'])
),
array(
array($reg['letters'],$reg['value_2'])
),
array(
array($reg['letters'],$reg['value_3'])
)
);
};
Instead, i get only the first Letter with the right values. If i use "$p->data[]" to show the other letters, it doesn't work.
The var_dump() i get from the code above is the following:
array(3) {
[0]=> array(1) {
[0]=> array(2) {
[0]=> string(1) "A"
[1]=> int(148) }
}
[1]=> array(1) {
[0]=> array(2) {
[0]=> string(1) "A"
[1]=> int(238) }
}
[2]=> array(1) {
[0]=> array(2) {
[0]=> string(1) "A"
[1]=> int(278) }
}
}
As you see, only the first variable 'letter' comes from the array. I need the others, like this:
array(3) {
[0]=> array(3) {
[0]=> array(2) {
[0]=> string(1) "A"
[1]=> int(148)
}
[1]=> array(2) {
[0]=> string(1) "B"
[1]=> int(238)
}
[2]=> array(2) {
[0]=> string(1) "C"
[1]=> int(151)
}
}
[1]=> array(3) {
[0]=> array(2) {
[0]=> string(1) "A"
[1]=> int(238)
}
[1]=> array(2) {
[0]=> string(1) "B"
[1]=> int(338)
}
[2]=> array(2) {
[0]=> string(1) "C"
[1]=> int(285)
}
}
[2]=> array(3) {
[0]=> array(2) {
[0]=> string(1) "A"
[1]=> int(278)
}
[1]=> array(2) {
[0]=> string(1) "B"
[1]=> int(138)
}
[2]=> array(2) {
[0]=> string(1) "C"
[1]=> int(205)
}
}
}
Explaing the code: in the foreach im using.. im getting the values from a database. Each variable "Letter" has 3 variable "Values". What im trying to do is show one same variable Value for each Letter separated on each array.
Can someone point me to the right direction? Thank you all o/
what is $reg['letters']? where does $result come from?
you will need an array of letters, and you will need to iterate that. with a little more info i could provide an example.
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 grouped an array using the following script
$grouped_array = array();
foreach($ungrouped_array as $item) {
//group them by id
$grouped_array[$item['id']][] = $item;
}
Now this grouped array looks like this
array(3) {
[1]=>
array(2) {
[0]=>
array(1) {
["id"]=>
string(1) "1"
}
[1]=>
array(1) {
["id"]=>
string(1) "1"
}
}
[6]=>
array(1) {
[0]=>
array(1) {
["id"]=>
string(1) "6"
}
}
[2]=>
array(4) {
[0]=>
array(1) {
["id"]=>
string(1) "2"
}
[1]=>
array(2) {
["id"]=>
string(1) "2"
["sub"]=>
string(1) "1"
}
[2]=>
array(2) {
["id"]=>
string(1) "2"
["sub"]=>
string(1) "2"
}
[3]=>
array(1) {
["id"]=>
string(1) "2"
}
}
}
I have deleted the most part of the array to make it shorter but there is no [0] field in this grouped array
All array fields are given the name of [id]'s value. I have no problem with that, I just have to short it again by [ID]
any suggestion will be great.
This should work to get 1, 2, 6:
<?php
$grouped_array = array();
foreach($ungrouped_array as $item) {
$grouped_array[$item['id']][] = $item;
}
// sort by key.
ksort( $grouped_array, SORT_NUMERIC );
print_r( $grouped_array );