I have an array which contains following values.
array(
'dates' => array(
(int) 0 => '2013-04-22',
(int) 1 => '2013-04-23',
),
'publisherName' => array(
(int) 0 => 'Comp1',
(int) 1 => 'Comp2',
),
'loaded' => array(
(int) 0 => (int) 2189,
(int) 1 => (int) 37,
),
'clicks' => array(
(int) 0 => (int) 0,
(int) 1 => (int) 0,
),
'ctr' => array(
(int) 0 => (int) 0,
(int) 1 => (int) 0,
)
)
What I want to produce is getting company based data on different dates like the array below.
How am I able to create an array which is like;
array (
'2013-04-22'=>array(
'publisherName'=>'Comp1',
'loaded'=>2189,
'clicks'=>0,
'ctr'=>0),
'2013-04-23'=>array(
'publisherName'=>'Comp2',
'loaded'=>37,
'clicks'=>0,
'ctr'=>0)
...
)
Which contains daily stats but which comes from publishername field.
Any ideas?
Here's an example that doesn't rely on any keys, the only requirement is that date is the first array in your original array:
// $array is your original array
$dates = array_shift($array);
$output = array();
foreach ($array as $k => $a) {
foreach ($a as $i => $v) {
$output[$i][$k] = $v;
}
}
$output = array_combine($dates, $output);
Let the initial array be $a and the desired array $b;
Code:
$b = array();
$count = 0;
foreach($a['dates'] as $date) {
$b[$date] = array(
'name' => $a['publisherName'][$count],
'loaded'=> $a['loaded'][$count],
'clicks'=> $a['clicks'][$count],
'ctr'=> $a['ctr'][$count]
);
$count++;
}
Result:
Array
(
[2013-04-22] => Array
(
[name] => Comp1
[loaded] => 2189
[clicks] => 0
[ctr] => 0
)
[2013-04-23] => Array
(
[name] => Comp2
[loaded] => 37
[clicks] => 0
[ctr] => 0
)
)
<?php
$data = $yourarray;
$new = array();
foreach($data['dates'] as $key=>$value) {
$new[$value] = array('name'=>$data['publisherName'][$key],'loaded'=>$data['loaded'][$key],'clicks'=>$data['clicks'][$key],'ctr'=>$data['ctr'][$key]);
}
echo '<pre>';
print_r($new);
?>
$newArr = array();
foreach($data['dates'] as $key=>$value) {
$new[$value] = array('name'=>$data['publisherName'][$key],'loaded'=>$data['loaded'][$key],'clicks'=>$data['clicks'][$key],'ctr'=>$data['ctr'][$key]);
}
echo '<pre>';
print_r($newArr);
$new_arr = your array;
$finalArray = array();
foreach($new_arr['dates'] as $key => $value){
$finalArray[$value] = array(
'publisherName'=>$new_arr['publisherName'][$key],
'loaded'=>$new_arr['loaded'][$key],
'clicks'=>$new_arr['clicks'][$key],
'ctr'=>$new_arr['ctr'][$key]
);
}
Output :
Array
(
[2013-04-22] => Array
(
[publisherName] => Comp1
[loaded] => 2189
[clicks] => 0
[ctr] => 0
)
[2013-04-23] => Array
(
[publisherName] => Comp2
[loaded] => 37
[clicks] => 0
[ctr] => 0
)
)
Related
The array is:
Array ( [0] => Array ( [dnu] => 121428 [d1] => 43 [d3] => 27 [d7] => 20 [d15] => 15 [d30] => 12 ) )
i want something like this:
[{"col":"dnu","value":121428},{"col":"d1","value":"43"},{"col":"d7","value":"20"}]
Try this.
$result = [];
foreach($array as $col => $value) {
$result[] = [
'col' => $col,
'value' => $value
];
}
$json = json_encode($result);
Try like this way with foreach() and json_encode()
<?php
$array = array ( array ( 'dnu' => 121428, 'd1' => 43, 'd3' => 27, 'd7' => 20, 'd15' => 15, 'd30' => 12 ) );
$result = [];
foreach($array[0] as $key=>$value){
$result[] = ['col'=>$key,'value'=>$value];
}
echo json_encode($result);
?>
DEMO: https://3v4l.org/vfG9k
I have an array
Array
(
[0] => Array
(
[hrg_lid] => 214291464161204318
[pecon] => 0
[col2pe] => Karam
[col4pe] => 1
[col6pe] => 2
[col8pe] => 264
[col9pe] => 42
[col10pe] => 85
[col11pe] => 2
)
[1] => Array
(
[hrg_lid] => 707581464079555092
[pecon] => 1
[col2pe] => Dummy
[col4pe] =>
[col6pe] =>
[col8pe] => 12
[col9pe] => 0
[col10pe] => 0
[col11pe] => 2
[col12pe] => 1
[col13pe] => 1
)
[2] => Array
(
[hrg_lid] => 707581464079555092
[col5risk] => 2
[col6risk] => 2
[col7risk] => 1
[col8risk] => 2
[col9risk] => 1
[col10risk] => 1
[col11risk] => 2
)
I want to merge those elements which has same hrg_lid.
Expected Output
Array
(
[0] => Array
(
[hrg_lid] => 214291464161204318
[pecon] => 0
[col2pe] => Karam
[col4pe] => 1
[col6pe] => 2
[col8pe] => 264
[col9pe] => 42
[col10pe] => 85
[col11pe] => 2
)
[1] => Array
(
[hrg_lid] => 707581464079555092
[pecon] => 1
[col2pe] => Dummy
[col4pe] =>
[col6pe] =>
[col8pe] => 12
[col9pe] => 0
[col10pe] => 0
[col11pe] => 2
[col12pe] => 1
[col13pe] => 1
[col5risk] => 2
[col6risk] => 2
[col7risk] => 1
[col8risk] => 2
[col9risk] => 1
[col10risk] => 1
[col11risk] => 2
)
I tried following code
foreach($arr as $key => $value) {
$finalArray[$value['hrg_lid']] = $value;
}
but fails
I would use hrg_lid as array key - otherwise you have to check every element already in the output array for matches every time you add a new element:
$finalArray = array();
foreach($arr as $value) {
$hrg_lid = $value['hrg_lid'];
if (isset($finalArray[$hrg_lid])) {
// merge if element with this $hrg_lid is already present
$finalArray[$hrg_lid] = array_merge($finalArray[$hrg_lid], $value);
} else {
// save as new
$finalArray[$hrg_lid] = $value;
}
}
If you want to get normalized array keys, you can reset them afterwards:
$finalArray = array_values($finalArray);
The hrg_lid value must be the key of the array, if you won't change the keys, Try this :
for($i=0; $i < count($arr);$i++)
{
for($j=0; $j < count($finalArray);$j++)
{
if($arr[$i]['hrg_lid'] == $finalArray[$j]['hrg_lid'])
{
$finalArray[$j] = array_merge($finalArray[$j],$arr[$i]);
break;
}
}
}
Try soomething like :
$finalArray = [];
foreach($arr as $singleArray) {
$id = $singleArray['hrg_lid'];
if (isset($finalArray[$id])) {
$finalArray = array_merge($finalArray[$id], $singleArray);
} else {
$finalArray[] = $singleArray;
}
}
You could try something like that :
$tmpArray = array();
$finalArray = array();
// We merge the arrays which have the same value in 'hrg_lid' col
foreach($source as $array){
$key = $array['hrg_lid'];
array_shift($array);
if(array_key_exists($key, $tmpArray)){
$tmpArray[$key] = array_merge($tmpArray[$key], $array);
}else{
$tmpArray[$key] = $array;
}
}
// We build the final array
foreach($tmpArray as $key => $value){
$finalArray[] = array_merge(array('hrg_lid' => $key), $value);
}
How to sum multidimensional array values then grouping with date as my code.
If any PHP code what should I try please tell me.
Please see the array code:
$array = array (
0 => array(
'date' => '2015-02-06 10:42:39',
'visit' => 1,
'bounce' => 0
),
1 => array(
'date' => '2015-02-06 13:23:21',
'visit' => 1,
'bounce' => 1
),
2 => array(
'date' => '2015-02-07 04:11:42',
'visit' => 1,
'bounce' => 1
),
3 => array(
'date' => '2015-02-08 11:35:28',
'visit' => 1,
'bounce' => 1
),
4 => array(
'date' => '2015-02-08 15:12:09',
'visit' => 1,
'bounce' => 1
),
5 => array(
'date' => '2015-02-09 15:12:09',
'visit' => 1,
'bounce' => 0
),
);
The result I expect must be when I do foreach:
date visit bounce
2015-02-06 2 1
2015-02-07 1 1
2015-02-08 2 2
2015-02-09 1 0
Here is the code what I've tried. But it just return the date count only.
$items = array_column($array, 'date');
$preg = preg_quote('2015-02-06', '~');
$result = preg_grep('~' . $preg . '~', $items);
echo 'Date <br/>' . count($result);
Please help, thank you in advanced.
Solution using array_walk, substr and array_values functions:
$date_keys = [];
array_walk($array, function($v) use(&$date_keys){
$datePart = $v['date'] = substr($v["date"], 0, 10);
if (isset($date_keys[$datePart])) {
$date_keys[$datePart]['visit'] += $v['visit'];
$date_keys[$datePart]['bounce'] += $v['bounce'];
} else {
$date_keys[$datePart] = $v;
}
});
print_r(array_values($date_keys));
The output:
Array
(
[0] => Array
(
[date] => 2015-02-06
[visit] => 2
[bounce] => 1
)
[1] => Array
(
[date] => 2015-02-07
[visit] => 1
[bounce] => 1
)
[2] => Array
(
[date] => 2015-02-08
[visit] => 2
[bounce] => 2
)
[3] => Array
(
[date] => 2015-02-09
[visit] => 1
[bounce] => 0
)
)
You can create a new array:
$newArr = [];
foreach($array as $arr){
$d = (new DateTime($arr['date']))->format('Y-m-d');
$newArr[$d] = [
"visit" => isset($newArr[$d]['visit']) ? $newArr[$d]['visit'] += $arr['visit'] : $arr['visit'],
"bounce" => isset($newArr[$d]['bounce']) ? $newArr[$d]['bounce'] += $arr['bounce'] : $arr['bounce']
];
}
echo "<pre>";
var_dump($newArr);
echo "</pre>";
Above returns a nice formatted array which you can easily read out in the example you posted:
array(4) {
["2015-02-06"]=>
array(2) {
["visit"]=>
int(2)
["bounce"]=>
int(1)
}
["2015-02-07"]=>
array(2) {
["visit"]=>
int(1)
["bounce"]=>
int(1)
}
["2015-02-08"]=>
array(2) {
["visit"]=>
int(2)
["bounce"]=>
int(2)
}
["2015-02-09"]=>
array(2) {
["visit"]=>
int(1)
["bounce"]=>
int(0)
}
}
You could merge your entries like this:
$items = [];
foreach ($array as $value) {
$date = substr($value['date'], 0, 10);
if (!isset($items[$date]) {
$items[$date] = [
'date' => $date,
'visit' => 0,
'bounce' => 0
];
}
$items[$date]['visit'] += $value['visit'];
$items[$date]['bounce'] += $value['bounce'];
}
By using date as a key in the $items array, we make sure we sum up the visit and bounce for each date instead of appending them.
If you'd ever want to get rid of the keys, you can simply use array_values.
If your array is called $myArray, and the new array your creating is $myDateArray:
$myDateArray = array();
foreach ($myArray as $value)
{
list($date_string, $other) = explode(" ", $value['date']);
if (array_key_exists($date_string, $myDateArray))
{
//adding the visits and bounces
$myDateArray[$date_string]['visit'] = $myDateArray[$date_string]['visit'] + $value['visit'];
$myDateArray[$date_string]['bounce'] = $myDateArray[$date_string]['bounce'] + $value['bounce'];
}
else
{
//putting the first values (of the key $date_string) into $myDateArray
array_push($myDateArray, array($date_string, $value['visit], $value['bounce']));
}
}
Let me know if that worked for you!
The code creates a new array which contains the summed result of your array.
$resultArray = [];
foreach($array as $row){
$dateObj = DateTime::createFromFormat('Y-m-d H:i:s', $row['date']);
$key = $dateObj->format('Y-m-d');
if(!array_key_exists($key, $resultArray)){
$resultArray[$key] = ['visit' => $row['visit'], 'bounce' => $row['bounce']];
}
else {
$resultArray[$key]['visit'] += $row['visit'];
$resultArray[$key]['bounce'] += $row['bounce'];
}
}
Result:
array (size=4)
'2015-02-06' =>
array (size=2)
'visit' => int 2
'bounce' => int 1
'2015-02-07' =>
array (size=2)
'visit' => int 1
'bounce' => int 1
'2015-02-08' =>
array (size=2)
'visit' => int 2
'bounce' => int 2
'2015-02-09' =>
array (size=2)
'visit' => int 1
'bounce' => int 0
Does this do the trick ?
<?php
$array = array (
0 => array(
'date' => '2015-02-06 10:42:39',
'visit' => 1,
'bounce' => 0
),
1 => array(
'date' => '2015-02-06 13:23:21',
'visit' => 1,
'bounce' => 1
),
2 => array(
'date' => '2015-02-07 04:11:42',
'visit' => 1,
'bounce' => 1
),
3 => array(
'date' => '2015-02-08 11:35:28',
'visit' => 1,
'bounce' => 1
),
4 => array(
'date' => '2015-02-08 15:12:09',
'visit' => 1,
'bounce' => 1
),
5 => array(
'date' => '2015-02-09 15:12:09',
'visit' => 1,
'bounce' => 0
),
);
$results = [];
foreach($array as $e)
{
$date = explode(' ',$e['date'])[0] ;
if(!isset($results[$date]))
$results[$date] = ['visit'=>0 , 'bounce'=>0] ;
$results[$date]['visit'] += $e['visit'];
$results[$date]['bounce'] += $e['bounce'];
}
print_r($results);
Technique/ Mechnism
$result = array();
$items = array_column($array, 'date');
for($i=0; $i < count($items); $i++){
$date = date("Y-m-d", strtotime($items[$i]));
$result[$date][0] += $array[$i]['visit'];
$result[$date][1] += $array[$i]['bounce'];
}
Result
Please design your output as required.
echo "date visit bounce<br/>";
foreach($result as $key => $value){
echo $key." ".$value[0]." ".$value[1]."<br/>";
}
Output
date visit bounce
2015-02-06 2 1
2015-02-07 1 1
2015-02-08 2 2
2015-02-09 1 0
please help me with a code. I have this multi-dimensional array and need to count the value of usuario_cidade case its same value like this:
array 52 = (2) Cidade_1, (2) Cidade_2, (1) Cidade_3
Array
(
[52] => Array
(
[0] => stdClass Object
(
[funcionario_id] => 52
[usuario_cidade] => Cidade_1
)
[1] => stdClass Object
(
[funcionario_id] => 52
[usuario_cidade] => Cidade_1
)
[2] => stdClass Object
(
[funcionario_id] => 52
[usuario_cidade] => Cidade_2
)
[3] => stdClass Object
(
[funcionario_id] => 52
[usuario_cidade] => Cidade_3
)
[4] => stdClass Object
(
[funcionario_id] => 52
[usuario_cidade] => Cidade_2
)
)
)
Try this code:
//Create object array format as per question scenario for testing...
$arrObject1 = new stdClass();
$arrObject1->funcionario_id = '52';
$arrObject1->usuario_cidade = 'Cidade_1';
$arrObject2 = new stdClass();
$arrObject2->funcionario_id = '52';
$arrObject2->usuario_cidade = 'Cidade_1';
$arrObject3 = new stdClass();
$arrObject3->funcionario_id = '52';
$arrObject3->usuario_cidade = 'Cidade_2';
$arrObject4 = new stdClass();
$arrObject4->funcionario_id = '52';
$arrObject4->usuario_cidade = 'Cidade_3';
$arrObject5 = new stdClass();
$arrObject5->funcionario_id = '52';
$arrObject5->usuario_cidade = 'Cidade_2';
//Finalize array...
$varArray = array('52' => array(
$arrObject1, $arrObject2, $arrObject3, $arrObject4, $arrObject5
));
$arrResult = array();
//Loop until main array...
foreach($varArray AS $arrKey => $arrObjVal){
//Loop for object values...
foreach($arrObjVal AS $ocjKey => $objVal){
//Check for specific key(i.e. value of usuario_cidade) exist into result array...
if(array_key_exists($objVal->usuario_cidade, $arrResult)){
//Increment value if exist...
$arrResult[$objVal->usuario_cidade] = $arrResult[$objVal->usuario_cidade] + 1;
}
else {
//Initialize value of result array...
$arrResult[$objVal->usuario_cidade] = 1;
}
}
}
print('<pre>');
print_r($arrResult);
print('</pre>');
This will give result:
[Cidade_1] => 2
[Cidade_2] => 2
[Cidade_3] => 1
Hope this help you!
Try this..
$your_array = array();
$usuario_cidade = array();
foreach ($your_array as $key => $values){
foreach($values as $value){
$usuario_cidade[$key][$value->usuario_cidade]=isset($usuario_cidade[$key][$value->usuario_cidade]) ? $usuario_cidade[$key][$value->usuario_cidade] : '0' + 1;
}
}
print_r($usuario_cidade);
Hey kindly check this code it is given the same answer as the conditions are given.
$arr = array();
$arr2 = array('funcionario_id' => 52,'usuario_cidade' => 'Cidade_1' );
$arr3 = array('funcionario_id' => 52,'usuario_cidade' => 'Cidade_1' );
$arr4 = array('funcionario_id' => 52,'usuario_cidade' => 'Cidade_2' );
$arr5 = array('funcionario_id' => 52,'usuario_cidade' => 'Cidade_3' );
$arr6 = array('funcionario_id' => 52,'usuario_cidade' => 'Cidade_2' );
$arr = [$arr2,$arr3,$arr4,$arr5,$arr6];
$cidaed = array('Cidade_1' => 0, 'Cidade_2' => 0 , 'Cidade_3' => 0 );
$count = 0;
//echo var_dump($arr);
foreach ($arr as $key => $value) {
foreach ($value as $keys => $values) {
if($keys == 'usuario_cidade')
{
$cidaed[$values] += 1;
}
}
}
echo var_dump($cidaed);
The answer for above will be.
array(3) { ["Cidade_1"]=> int(2) ["Cidade_2"]=> int(2) ["Cidade_3"]=> int(1) }
Can you please check this.
$main_array[52] = array(
0 => array(
'funcionario_id' => 52,
'usuario_cidade' => 'Cidade_1'
),
1 => array(
'funcionario_id' => 52,
'usuario_cidade' => 'Cidade_1'
),
2 => array(
'funcionario_id' => 52,
'usuario_cidade' => 'Cidade_2'
),
3 => array(
'funcionario_id' => 52,
'usuario_cidade' => 'Cidade_3'
),
4 => array(
'funcionario_id' => 52,
'usuario_cidade' => 'Cidade_2'
)
);
$check_array = array();
$count_array = array();
foreach ($main_array as $main){
foreach($main as $data){
if(in_array($data['usuario_cidade'], $check_array)){
$count_array[$data['usuario_cidade']] = $count_array[$data['usuario_cidade']] + 1;
}else{
array_push($check_array,$data['usuario_cidade']);
$count_array[$data['usuario_cidade']] = 1;
}
}
}
foreach($count_array as $key => $value){
echo $key.'='.$value.'<br />';
}
echo "<pre>"; print_r($count_array);
I want to create a list where if its already in the array to add to the value +1.
Current Output
[1] => Array
(
[source] => 397
[value] => 1
)
[2] => Array
(
[source] => 397
[value] => 1
)
[3] => Array
(
[source] => 1314
[value] => 1
)
What I want to Achieve
[1] => Array
(
[source] => 397
[value] => 2
)
[2] => Array
(
[source] => 1314
[value] => 1
)
My current dulled down PHP
foreach ($submissions as $timefix) {
//Start countng
$data = array(
'source' => $timefix['parent']['id'],
'value' => '1'
);
$dataJson[] = $data;
}
print_r($dataJson);
Simply use an associated array:
$dataJson = array();
foreach ($submissions as $timefix) {
$id = $timefix['parent']['id'];
if (!isset($dataJson[$id])) {
$dataJson[$id] = array('source' => $id, 'value' => 1);
} else {
$dataJson[$id]['value']++;
}
}
$dataJson = array_values($dataJson); // reset the keys - you don't nessesarily need this
This is not exactly your desired output, as the array keys are not preserved, but if it suits you, you could use the item ID as the array key. This would simplify your code to the point of not needing to loop through the already available results:
foreach ($submissions as $timefix) {
$id = $timefix['parent']['id'];
if (array_key_exists($id, $dataJson)) {
$dataJson[$id]["value"]++;
} else {
$dataJson[$id] = [
"source" => $id,
"value" => 1
];
}
}
print_r($dataJson);
You should simplify this for yourself. Something like:
<?
$res = Array();
foreach ($original as $item) {
if (!isset($res[$item['source']])) $res[$item['source']] = $item['value'];
else $res[$item['source']] += $item['value'];
}
?>
After this, you will have array $res which will be something like:
Array(
[397] => 2,
[1314] => 1
)
Then, if you really need the format specified, you can use something like:
<?
$final = Array();
foreach ($res as $source=>$value) $final[] = Array(
'source' => $source,
'value' => $value
);
?>
This code will do the counting and produce a $new array as described in your example.
$data = array(
array('source' => 397, 'value' => 1),
array('source' => 397, 'value' => 1),
array('source' => 1314, 'value' => 1),
);
$new = array();
foreach ($data as $item)
{
$source = $item['source'];
if (isset($new[$source]))
$new[$source]['value'] += $item['value'];
else
$new[$source] = $item;
}
$new = array_values($new);
PHP has a function called array_count_values for that. May be you can use it
Example:
<?php
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
?>
Output:
Array
(
[1] => 2
[hello] => 2
[world] => 1
)