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.
Related
I am trying to loop through array of arrays in php. Usually get stalked with complex array sometimes but I need your kind assistance with this.
var_dump($array) produced the array below:
$arrayVal = array(6) {
["item_id"]=>
array(2) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
}
["request_explanation"]=>
array(2) {
[0]=>
string(7) "Welcome"
[1]=>
string(11) "Hello World"
}
["quantity"]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "4"
}
["unit_cost"]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "3"
}
["total_cost"]=>
array(2) {
[0]=>
string(1) "0"
[1]=>
string(1) "0"
}
["supporting_document"]=>
string(0) ""
}
My database table:
I want to be able to save each of the value in that array into the table above. Thanks for helping me.
Use the indexes of one of the sub-arrays to access all the other sub-arrays:
foreach ($array['item_id'] as $i => $item_id) {
$request_explanation = $array['request_explanation'][$i];
$quantity = $array['quantity'][$i];
// repeat this for all the columns
// Now you can insert all these variables into the database
}
Use a loop to build 2 separate arrays:
foreach($array['ExpensesList'] as $index => $val){
$array1[$index] = $array['ExpensesList'][$index][0];
$array2[$index] = $array['ExpensesList'][$index][1];
}
Then insert each array into your database individually.
This will not work if any sub array contains an index at 2, so this is explicitly for the example structure you provided.
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 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.
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.
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 );