This is my code
$pro_qty = '';
$se_pro = '';
$pro_id_nn = $this->getDataAll("SELECT session_pro_id,session_pro_qty FROM `jp_session` WHERE session_pro_id IN (".$pro_id.") AND order_status='3'");
foreach($pro_id_nn as $pro)
{
$pro_qty[] = $pro['session_pro_qty'];
$se_pro[] = $pro['session_pro_id'];
}
$proqty = array_combine($pro_qty,$se_pro);
echo '<br>';
print_r($se_pro);
echo '<br>';
print_r($pro_qty);
echo '<br>';
print_r($proqty);
OUTOUT
first array
$se_pro = Array ( [0] => 5 [1] => 1 [2] => 1 ) ;
second array
$pro_qty = Array ( [0] => 24 [1] => 24 [2] => 22 ) ;
Finally combine two array result is
$proqty = Array ( [5] => 24 [1] => 22 );
but my expecting result is
$proqty = Array ( [5] => 24 [1] => 24 [1] => 22 );
how can i get my expecting result . thanks in advance.
Your expected result is not possible, you cannot map one key (1) to two different values (24 and 22). Perhaps you should look at a different solution, such as a "jp_session" class which contains the two values, and then just store it in a list.
simple solution
foreach($pro_id_nn as $pro)
{
$pro_qty[$pro['session_pro_id']][] = $pro['session_pro_qty'];
}
Try this one
<?php
$se_pro = Array ( 0 => 5, 1 => 1, 2 => 1 ) ;
$pro_qty = Array ( 0 => 24, 1 => 24, 2 => 22 ) ;
$a=sizeof($se_pro);
for($i=0;$i<$a;$i++)
{
$b=$se_pro[$i];
$c=$pro_qty[$i];
$temp[$b]=$c;
$i++;
}
print_r($temp);
?>
But one condition '$se_pro' values not repeat and both array are same size
in array_combine() If two keys are the same, the second one prevails..
you can get the result like -
Array
(
[24] => Array
(
[0] => 5
[1] => 1
)
[22] => 3
)
the other way can be
$keys = array ( '24', '24', '22' );
$values = array ( '5', '1', '1' );
$output = array();
$size = sizeof($keys);
for ( $i = 0; $i < $size; $i++ ) {
if ( !isset($output[$keys[$i]]) ) {
$output[$keys[$i]] = array();
}
$output[$keys[$i]][] = $values[$i];
}
this will give the output like -
Array ( [24] => Array ( [0] => 5 [1] => 1 ) [22] => Array ( [0] => 1 ) )
or you can use
<?php
$keys = array ( '24', '24', '22' );
$values = array ( '5', '1', '1' );
function foo($key, $val) {
return array($key=>$val);
}
$arrResult = array_map('foo', $keys, $values);
print_r($arrResult);
?>
depending upon which output is more suitable for you to work with.
Related
This is a question for all the array specialists out there. I have an multi dimension array with a result as number (can be 0,1 or 2) and need the average for each grouped by parent.
In the example below the calculation would be:
subentry1_sub1 = 2 + 2 = 4 (4/2=2)
subentry1_sub2 = 1 + 1 = 2 (2/2=1)
So what I try to archive in PHP is the following result:
subentry1_sub1 average = 2
subentry1_sub2 average = 1
...
I already tried some solutions from similar questions. But with all the recursive functions I didn't managed to get it aggregated by the last child name (e.g. subentry1_sub1).
Any ideas?
EDIT:
subentry1_sub1 is 2 + 2 because its two times in the array
[entry1] => [subentry1] => [subentry1_sub1] => result
[entry2] => [subentry1] => [subentry1_sub1] => result
Array
(
[entry1] => Array
(
[subentry1] => Array
(
[subentry1_sub1] => Array
(
[value] => abc
[result] => 2
)
[subentry1_sub2] => Array
(
[value] => abc
[result] => 1
)
)
[subentry2] => Array
(
[subentry2_sub1] => Array
(
[value] => abc
[result] => 1
)
[subentry2_sub2] => Array
(
[value] => abc
[result] => 1
)
)
)
[entry2] => Array
(
[subentry1] => Array
(
[subentry1_sub1] => Array
(
[value] => abc
[result] => 2
)
[subentry1_sub2] => Array
(
[value] => abc
[result] => 1
)
)
[subentry2] => Array
(
[subentry2_sub1] => Array
(
[value] => abc
[result] => 1
)
[subentry2_sub2] => Array
(
[value] => abc
[result] => 1
)
)
)
)
Try this code. In this i have created a new array $sum which will add result value of same subentry childs with same key and another array $count which will count the number of times each key repeats
<?php
$data = array('entry1'=>array(
'subentry1'=>
array(
'subentry1_sub1'=>array('value'=>'abc','result'=>2),
'subentry1_sub2'=>array('value'=>'abc','result'=>1)
),
'subentry2'=>
array(
'subentry2_sub1'=>array('value'=>'abc','result'=>1),
'subentry2_sub2'=>array('value'=>'abc','result'=>1)
)
),
'entry2'=>array(
'subentry1'=>
array(
'subentry1_sub1'=>array('value'=>'abc','result'=>2),
'subentry1_sub2'=>array('value'=>'abc','result'=>1)
),
'subentry2'=>
array(
'subentry2_sub1'=>array('value'=>'abc','result'=>1),
'subentry2_sub2'=>array('value'=>'abc','result'=>1)
)
)
);
$sum = array();
$repeat = array();
foreach($data as $input){
foreach($input as $array){
foreach($array as $key=>$value){
if(array_key_exists($key,$sum)){
$repeat[$key] = $repeat[$key]+1;
$sum[$key] = $sum[$key] + $value['result'];
}else{
$repeat[$key] = 1;
$sum[$key] = $value['result'];
}}}}
echo "<pre>";
print_r($sum);
print_r($repeat);
foreach($sum as $key=>$value){
echo $key. ' Average = '. $value/$repeat[$key]."</br>";
}
Output
Array
(
[subentry1_sub1] => 4
[subentry1_sub2] => 2
[subentry2_sub1] => 2
[subentry2_sub2] => 2
)
Array
(
[subentry1_sub1] => 2
[subentry1_sub2] => 2
[subentry2_sub1] => 2
[subentry2_sub2] => 2
)
subentry1_sub1 Average = 2
subentry1_sub2 Average = 1
subentry2_sub1 Average = 1
subentry2_sub2 Average = 1
You can easily calculate avg now
Note : As you mentioned you are counting occurence of subentry1_sub1 etc so i did the same so it will also count whether key result exists or not
I know this is an old thread but im pretty sure there is a much easier way of doing this for anyone who is interested:
If you know the result will always be a number:
foreach($my_array as $entry_name => $entry_data)
{
foreach($entry_data as $sub_name => $sub_data)
{
$sub_results = array_column($sub_data, 'result');
$averages[$entry_name][$sub_name] = array_sum($sub_results)/count($sub_results);
}
}
If its possible the result could be NULL or empty, this will check it and return 'N/A' if there is no valid data to calculate an average from:
foreach($my_array as $entry_name => $entry_data)
{
foreach($entry_data as $sub_name => $sub_data)
{
$sub_results = array_filter(array_column($sub_data, 'result'));
$averages[$entry_name][$sub_name] = (count($sub_results) > 0 ? array_sum($sub_results)/count($sub_results) : 'N/A');
}
}
both of these solutions will give you an averages array that will output the average per subentry per entry.
Try this like,
<?php
$data=array('entry1'=>array(
'subentry1'=>
array(
'subentry1_sub1'=>array('value'=>'abc','result'=>3),
'subentry1_sub2'=>array('value'=>'abc','result'=>3)
),
'subentry2'=>
array(
'subentry2_sub1'=>array('value'=>'abc','result'=>2),
'subentry2_sub2'=>array('value'=>'abc','result'=>8)
)
),
'entry2'=>array(
'subentry1'=>
array(
'subentry1_sub1'=>array('value'=>'abc','result'=>6),
'subentry1_sub2'=>array('value'=>'abc','result'=>6)
),
'subentry2'=>
array(
'subentry2_sub1'=>array('value'=>'abc','result'=>10),
'subentry2_sub2'=>array('value'=>'abc','result'=>12)
)
)
);
foreach($data as $k=>$v){
echo "----------------$k---------------------\n";
if(is_array($v)){
foreach($v as $a=>$b){
if(is_array($b)){
echo $a.' average = ';
$c=array_keys($b);// now get *_sub*
$v1=isset($b[$c[0]]['result']) ? $b[$c[0]]['result'] : '';
$v2=isset($b[$c[1]]['result']) ? $b[$c[1]]['result'] : '';
echo ($v1+$v2)/2;
echo "\n";
}
}
}
}
Online Demo
In the meantime I found a simple working solution myself:
foreach ($data as $level2) {
foreach ($level2 as $level3) {
foreach ($level3 as $keyp => $level4) {
foreach ($level4 as $key => $value) {
if($key == 'result') $stats[$keyp] += $value;
}
}
}
}
With that you get the total for every key in an new array $stats.
But be sure to checkout the solution from user1234, too. It's working great and already includes the calculation of the average.
https://stackoverflow.com/a/39292593/2466703
I'm pulling an array from the database and it looks like so:
Array
(
[0] => Array
(
[tracker_id] => 28
[tracking_numbers] => hdkshwuy47937892hd
)
[1] => Array
(
[tracker_id] => 28
[tracking_numbers] => 797825464411
)
)
I need to reformat it to look like this:
Array
(
[0] => Array
(
[tracker_id] => 28
[tracking_numbers] => Array
(
[0] => hdkshwuy47937892hd
[1] => 797825464411
)
)
)
I can't seem find the right search in the array or keys to create an array of numbers for the single tracker id.
Use array_column() for < php V5.5
<?php
$a=array
( array
('tracker_id' => 28,
'tracking_numbers'=> "hdkshwuy47937892hd"
),
array('tracker_id' => 28,
'tracking_numbers' => "797825464411",
) );
$a[0]['tracking_numbers']=array_column($a,"tracking_numbers");
unset($a[1]);
print_r($a);
Demo
try this
$arr_output = array();
foreach($arr_input as $arr)
{
$tracker_id = $arr['tracker_id'];
$tracking_numbers = $arr['tracking_numbers'];
$arr_output[$traker_id][] = $tracking_numbers;
}
print_r($arr_output);
UPDATE 2:
$arr_output = array();
$arr_output1 = array();
foreach($arr_input as $arr)
{
$tracker_id = $arr['tracker_id'];
$tracking_numbers = $arr['tracking_numbers'];
$arr_output[$traker_id][] = $tracking_numbers;
}
$i=0;
foreach($arr_output as $key=>$value)
{
$arr_output1[$i]['tracker_id']=$key
$arr_output1[$i]['tracking_numbers']=$value
$i+=1;
}
print_r($arr_output1);
I have this array which has more then 100 results but some of these has same sub array key. I would like to sum array element which has same key which is [/xyx/888350] in this example. However, I want to keep the format as it is which is two dimensional.
Array
(
[0] => Array
(
[/xyx/888350] => /xyx/888350
[visitors] => 1
[pageviews] => 2
[uniquepageviews] => 1
)
[1] => Array
(
[/xyx/888350] => /xyx/888350
[visitors] => 1
[pageviews] => 3
[uniquepageviews] => 1
)
[2] => Array
(
[/xyx/888350] => /xyx/888350
[visitors] => 1
[pageviews] => 2
[uniquepageviews] => 1
)
[3] => Array
(
[/xyx/102254] => /xyx/102254
[visitors] => 1
[pageviews] => 2
[uniquepageviews] => 1
)
)
I am expecting out put something like below:
Array
(
[0] => Array
(
[/xyx/888350] => /xyx/888350
[visitors] => 2
[pageviews] => 7
[uniquepageviews] => 2
)
[1] => Array
(
[/xyx/102254] => /xyx/102254
[visitors] => 1
[pageviews] => 3
[uniquepageviews] => 1
)
)
Thanks in advance.
Loop the array, and store the results in a temporary array, by using the first value as a key:
$input = /*your example data here*/;
$result = array();
foreach($input as $data){
$keys = array_keys($data);
$key = $keys[0]; //get the first key of the array a.k.a '/xyx/888350'
if(isset($result[$key])){
//sum the values if we have this key
$result[$key]['visitors'] += $data['visitors'];
$result[$key]['pageviews'] += $data['pageviews'];
$result[$key]['uniquepageviews'] += $data['uniquepageviews'];
}else{
$result[$key] = $data;
}
}
//drop the extra keys and return a indexed array with the summed values
return array_values($result);
$results=$service->data_ga->get("");
$stats = array();
for ($i=0; $i < count($stats_results=$results->getRows()) ; $i++)
{
$stats[]=array(
'path'=>$stats_results[$i][1],
'visitors'=>$stats[$i]['visitors'] + $stats_results[$i][2],
'pageviews'=>$stats[$i]['visitors'] + $stats_results[$i][3],
'uniquepageviews'=>$stats[$i]['visitors'] + $stats_results[$i][4]
);
}
$result = array();
foreach($stats as $data){
$keys = array_keys($data);
$key = $keys[0]; //get the first key of the array a.k.a '/xyx/888350'
if(isset($result[$key]))
{
//sum the values if we have this key
$result[$key]['visitors'] += $data['visitors'];
$result[$key]['pageviews'] += $data['pageviews'];
$result[$key]['uniquepageviews'] += $data['uniquepageviews'];
}else{
$result[$key] = $data;
}
}
echo "<pre>";
print_r($result);
Output:
Array
(
[path] => Array
(
[path] => /path/888350
[visitors] => 3
[pageviews] => 7
[uniquepageviews] => 3
)
)
Thanks both of you. As time goes on I will also answer questions other people have. I have just started my career. Thank you guys :)
I've a series of arrays with values that goes from 1 to 5. Almost every array has missing values, some even dont have any values. My objective is to fill the missing values with 0. All those arrays are stored into a multidimensional array.
My array looks like:
Array
(
[1] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[2] => Array
(
[0] => 1
[1] => 5
)
[3] => Array
(
[0] => (this array has no values)
)
[4] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
etc...
)
How it should be:
Array
(
[1] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 0
[4] => 0
)
[2] => Array
(
[0] => 1
[1] => 0
[2] => 0
[3] => 0
[4] => 5
)
[3] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
)
[4] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
etc...
)
Any help would be appriciated!
For each of your subarrays loop through the numbers 1 to 5, and if that value exists set its key to be one less than its value:
$newarray = array();
foreach($arr as $key => $subarr) {
for ($i = 1; $i <= 5; $i++) {
if (in_array($i, $subarr)) $newarray[$key][$i - 1] = $i;
else $newarray[$key][$i - 1] = 0;
}
}
Where $newarray is your output and $arr is your input array.
You may want to note that PHP does not truly do multidimensional arrays. It only allows you to relate 2 flat arrays together which is not true multidimensionality.
This does not work and will produce results described above.
$menu[1] = "My Training"; //not $menu[1][0]
$menu[1][1] = "To Do List";
$menu[1][2] = "Catalog";
$menu[1][3] = "Self-Report";
$menu[1][4] = "Completions";
$menu[2] = "Manager";
$menu[2][1] = "Direct Reports";
$menu[2][2] = "Incompletes";
$menu[2][3] = "Completions";
$menu[3] = "Instructor";
$menu[3][1] = "My Classes";
$menu[3][2] = "Printables";
$menu[3][3] = "Qualifications";
This does work.
$menu[1] = "My Training"; //not $menu[1][0]
$submenu[1][1] = "To Do List";
$submenu[1][2] = "Catalog";
$submenu[1][3] = "Self-Report";
$submenu[1][4] = "Completions";
$menu[2] = "Manager";
$submenu[2][1] = "Direct Reports";
$submenu[2][2] = "Incompletes";
$submenu[2][3] = "Completions";
$menu[3] = "Instructor";
$submenu[3][1] = "My Classes";
$submenu[3][2] = "Printables";
$submenu[3][3] = "Qualifications";
$submenu is only related to $menu through the first key number as there are no first dimension values to $submenu.
Something like this (array_pad() won't do the trick). $myArray is your source array. Completed array is returned in $result:
$result = array();
foreach( $myArray as $subKey=>$subArray ) {
for( $i=0; $i<5; $i++ ) {
if( isset( $subArray[$i] )) {
$result[$subKey][$i] = $subArray[$i];
} else {
$result[$subKey][$i] = 0;
}
}
}
Note, we do copy of the array. You cannot fill array in-place.
It's been many years since I wrote any PHP but something like this might do the trick I guess?
for($i = 0; $i < 5; $i++)
{
if(empty($myArray[$i])
{
$myArray[$i] = 0;
}
}
how do i sort this array by the nums...
Array(
[nums] => Array
(
[0] => 34
[1] => 12
[2] => 13
)
[players] => Array
(
[0] => Mike
[1] => Bob
[2] => Mary
)
)
... so that i get this one?
Array(
[nums] => Array
(
[0] => 12
[1] => 13
[2] => 34
)
[players] => Array
(
[0] => Bob
[1] => Mary
[2] => Mike
)
)
array_multisort($x['nums'],$x['players']);
Try the sort function.
bool sort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
This function sorts an array. Elements will be arranged from lowest to highest when this function has completed.
Also check out asort and arsort
EDIT
I did not take into account your Multidimensional array.
<?php
//code derived from comments on the php.net/sort page.
// $sort used as variable function--can be natcasesort, for example
function sort2d( &$arrIn, $index = null, $sort = 'sort') {
// pseudo-secure--never allow user input into $sort
if (strpos($sort, 'sort') === false) {$sort = 'sort';}
$arrTemp = Array();
$arrOut = Array();
foreach ( $arrIn as $key=>$value ) {
reset($value);
$arrTemp[$key] = is_null($index) ? current($value) : $value[$index];
}
$sort($arrTemp);
foreach ( $arrTemp as $key=>$value ) {
$arrOut[$key] = $arrIn[$key];
}
$arrIn = $arrOut;
}
?>