How to fetch array in correct order - php

How to fetch array in correct order ?
I am printing a array but it is not fetching in correct order.
I want that each track value contain array of date and array of
number respectively. please give a solution .Any help would be
highly appreciated.
My code for fetch array are:
<?php
foreach($posts as $post)
{
$array['track_value'][] = $post->track_value;
$array['track_value']['date'][] = $post->date;
$array['track_value']['num'][] = $post->num;
}
?>
From this i am getting wrong value like these:
<?php
Array (
[track_value] => Array
(
[0] => mobile
[date] => Array
(
[0] => 2015-08-23
[1] => 2015-08-24
[2] => 2015-08-23
[3] => 2015-08-24
[4] => 2015-08-22
[5] => 2015-08-23
[6] => 2015-08-24
)
[num] => Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 2
[4] => 1
[5] => 1
[6] => 1
)
[1] => mobile
[2] => laptop
[3] => laptop
[4] => pc
[5] => pc
[6] => pc
)
)
?>
The output should be like:
<?php
Array (
[track_value] => Array
(
[0] => mobile
Array
(
[date] => Array
(
[0] => 2015-08-23
[1] => 2015-08-24
)
[num] => Array
(
[0] => 1
[1] => 1
)
)
[1] => laptop
Array
(
[date] => Array
(
[0] => 2015-08-23
[1] => 2015-08-24
)
[num] => Array
(
[0] => 1
[1] => 2
)
)
[2] => pc
Array
(
[date] => Array
(
[0] => 2015-08-23
[1] => 2015-08-24
[2] => 2015-08-23
)
[num] => Array
(
[0] => 1
[1] => 1
[2] => 1
)
)
)
)
?>

I'm not sure what you are trying to do, but maybe this will help you on the way:
foreach($posts as $post)
{
$array['track_value'][$post->track_value]['date'][] = $post->date;
$array['track_value'][$post->track_value]['num'][] = $post->num;
}
This will give you an output like:
Array(
'track_value' => Array(
'mobile' => Array(
'date' => Array(
0 => '2015-08-25',
1 => '2015'08'26',
),
'num' => Array(
0 => 1337,
1 => 13337,
),
),
'pc' => ...
),
)

Try this:
$arr = [];
foreach($posts as $key => $val)
$arr['track_value'][$val->track_value][] = ['date' => $val->date, 'num' => $val->num];
}
print_r($arr);

Try this:
$tracks = [];
foreach($posts as $k1 as $v1) {
if (is_array($value)){
foreach ($value as $k2 => $v2) {
$tracks[$k1][$k2] = $v2;
}
}
$tracks[$k1] = $v1;
}

Related

Splitting array to use with jquery flot

I am building a jquery flot line chart with google analytics api data. I have the analytics working and it is giving me an array like so:
[3] => Array ( [0] => 20160922 [1] => 2217 [2] => 911 ) [4] => Array ( [0] => 20160923 [1] => 2047 [2] => 845 ) [5] => Array ( [0] => 20160924 [1] => 2152 [2] => 924 ) [6] => Array ( [0] => 20160925 [1] => 2502 [2] => 1028 ) [7] => Array ( [0] => 20160926 [1] => 2234 [2] => 877 ) [8] => Array ( [0] => 20160927 [1] => 2020 [2] => 755 ) [9] => Array ( [0] => 20160928 [1] => 1978 [2] => 793 ) [10] => Array ( [0] => 20160929 [1] => 2080 [2] => 867 ) [11] => Array ( [0] => 20160930 [1] => 1632 [2] => 747 ) [12] => Array ( [0] => 20161001 [1] => 1934 [2] => 913 ) [13] => Array ( [0] => 20161002 [1] => 2210 [2] => 1023 ) [14] => Array ( [0] => 20161003 [1] => 2068 [2] => 971 ) [15] => Array ( [0] => 20161004 [1] => 1738 [2] => 918 ) [16] => Array ( [0] => 20161005 [1] => 1787 [2] => 793 ) [17] => Array ( [0] => 20161006 [1] => 1694 [2] => 815 ) [18] => Array ( [0] => 20161007 [1] => 1583 [2] => 813 ) [19] => Array ( [0] => 20161008 [1] => 1906 [2] => 954 ) [20] => Array ( [0] => 20161009 [1] => 1936 [2] => 1047 ) [21] => Array ( [0] => 20161010 [1] => 2188 [2] => 1097 ) [22] => Array ( [0] => 20161011 [1] => 1892 [2] => 938 ) [23] => Array ( [0] => 20161012 [1] => 2036 [2] => 1022 ) [24] => Array ( [0] => 20161013 [1] => 1970 [2] => 915 ) [25] => Array ( [0] => 20161014 [1] => 2044 [2] => 1024 )
I cant seem to figure out how to get this separated out to use in a jquery.flot chart. The above array has a date followed by visits and pageviews that I want to put into a line-chart. All i need help with is the separating of the above array into one for visits and one for pageviews. The rest I can work out. Any help to get me pointed in the right direction will be great!
EDIT
I wanted to give some additional info as I cant seem to get the above array out of the results variable here is what I am using to process the google analytics api:
function getResults($analytics, $profileId) {
// Calls the Core Reporting API and queries for the number of sessions
// for the last seven days.
$optParams = array(
'dimensions' => 'ga:date'
);
return $analytics->data_ga->get(
'ga:' . $profileId,
'30daysAgo',
'today',
'ga:pageviews, ga:sessions',
$optParams
);
}
//$profile = $r['google_analytics'];
$profile = '69903642';
$results = getResults($analytics, $profile);
I believe the code will work that was given in the first answer but right now it is producing an empty array.
This code should work for you:
$pageViews = [];
$visits = [];
foreach($data as $key => $item) {
$pageViews[$key][$item[0]] = $item[2];
$visits[$key][$item[0]] = $item[1];
}
It will give you two separate arrays. Output:
array(
0 => array(
'20160922' => 911
)
)
array(
0 => array(
'20160922' => 2217
)
)
You can modify the code a little bit:
$pageViews = [];
$visits = [];
foreach($data as $key => $item) {
$pageViews[$item[0]] = $item[2];
$visits[$item[0]] = $item[1];
}
for output like this one:
array(
'20160922' => 2217,
......
)

array one format to another

I have one array in two format. I want to change array from
Array
(
[step_number] => 4
[app_id] => Array
(
[0] => 2
[1] => 3
)
[formdata] => Array
(
[0] => Array
(
[name] => app_id[]
[value] => 2
)
[1] => Array
(
[name] => app_id[]
[value] => 3
)
[2] => Array
(
[name] => fieldval[2][2][]
[value] => 1
)
[3] => Array
(
[name] => fieldval[3][3][]
[value] => 200
)
[4] => Array
(
[name] => fieldval[3][3][]
[value] => day
)
[5] => Array
(
[name] => title
[value] => new plan
)
[6] => Array
(
[name] => feature_plan
[value] => 3
)
[7] => Array
(
[name] => plan_type
[value] => free
)
[8] => Array
(
[name] => price
[value] =>
)
[9] => Array
(
[name] => sell_type
[value] => us
)
)
)
this format to
Array
(
[app_id] => Array
(
[0] => 2
[1] => 3
)
[fieldval] => Array
(
[2] => Array
(
[2] => Array
(
[0] => 1
)
)
[3] => Array
(
[3] => Array
(
[0] => 200
[1] => day
)
)
)
[title] => new plan
[feature_plan] => 3
[plan_type] => free
[price] =>
[sell_type] => us
)
these are are one array into two format. i have data in to first array format and i want to change that format to second array type format.
please tell me how i am trying this for 2 days but not succeed.
Here is a function you could use to produce that conversion:
function convert_formdata($input) {
$output = array();
foreach($input['formdata'] as $data) {
$keys = preg_split("#[\[\]]+#", $data['name']);
$value = $data['value'];
$target = &$output;
foreach($keys as $key) {
// Get index for "[]" reference
if ($key == '') $key = count($target);
// Create the key in the parent array if not there yet
if (!isset($target[$key])) $target[$key] = array();
// Move pointer one level down the hierarchy
$target = &$target[$key];
}
// Write the value at the pointer location
$target = $value;
}
return $output;
}
You would call it like this:
$output = convert_formdata($input);
See it run on eval.in for the given input. The output is:
array (
'app_id' =>
array (
0 => 2,
1 => 3,
),
'fieldval' =>
array (
2 =>
array (
2 =>
array (
0 => 1,
),
),
3 =>
array (
3 =>
array (
0 => 200,
1 => 'day',
),
),
),
'title' => 'new plan,',
'feature_plan' => 3,
'plan_type' => 'free',
'price' => NULL,
'sell_type' => 'us',
)

How to change this kind of array arrangement into like this

I am getting data from Google Analytics API. The GA API returns these data:
Array
(
[0] => Array
(
[0] => 00
[1] => bing
[2] => 1
)
[1] => Array
(
[0] => 00
[1] => google
[2] => 12
)
[2] => Array
(
[0] => 00
[1] => yahoo
[2] => 1
)
[3] => Array
(
[0] => 01
[1] => google
[2] => 7
)
[4] => Array
(
[0] => 02
[1] => google
[2] => 5
)
[5] => Array
(
[0] => 03
[1] => bing
[2] => 1
)
[6] => Array
(
[0] => 03
[1] => google
[2] => 4
)
[7] => Array
(
[0] => 04
[1] => google
[2] => 7
)
[8] => Array
(
[0] => 05
[1] => google
[2] => 5
)
[9] => Array
(
[0] => 05
[1] => yahoo
[2] => 1
)
[10] => Array
(
[0] => 06
[1] => bing
[2] => 1
)
[11] => Array
(
[0] => 06
[1] => google
[2] => 2
)
[12] => Array
(
[0] => 07
[1] => google
[2] => 4
)
[13] => Array
(
[0] => 08
[1] => bing
[2] => 1
)
[14] => Array
(
[0] => 08
[1] => google
[2] => 8
)
[15] => Array
(
[0] => 09
[1] => bing
[2] => 4
)
[16] => Array
(
[0] => 09
[1] => google
[2] => 13
)
[17] => Array
(
[0] => 10
[1] => bing
[2] => 1
)
[18] => Array
(
[0] => 10
[1] => google
[2] => 19
)
[19] => Array
(
[0] => 10
[1] => yahoo
[2] => 1
)
[20] => Array
(
[0] => 11
[1] => bing
[2] => 1
)
[21] => Array
(
[0] => 11
[1] => google
[2] => 23
)
[22] => Array
(
[0] => 11
[1] => yahoo
[2] => 1
)
[23] => Array
(
[0] => 12
[1] => bing
[2] => 1
)
[24] => Array
(
[0] => 12
[1] => google
[2] => 18
)
[25] => Array
(
[0] => 13
[1] => bing
[2] => 1
)
[26] => Array
(
[0] => 13
[1] => google
[2] => 17
)
[27] => Array
(
[0] => 13
[1] => yahoo
[2] => 1
)
[28] => Array
(
[0] => 14
[1] => bing
[2] => 3
)
[29] => Array
(
[0] => 14
[1] => google
[2] => 30
)
[30] => Array
(
[0] => 14
[1] => yahoo
[2] => 2
)
[31] => Array
(
[0] => 15
[1] => google
[2] => 15
)
[32] => Array
(
[0] => 15
[1] => yahoo
[2] => 2
)
[33] => Array
(
[0] => 16
[1] => bing
[2] => 1
)
[34] => Array
(
[0] => 16
[1] => google
[2] => 22
)
[35] => Array
(
[0] => 16
[1] => yahoo
[2] => 1
)
[36] => Array
(
[0] => 17
[1] => google
[2] => 18
)
[37] => Array
(
[0] => 17
[1] => yahoo
[2] => 2
)
[38] => Array
(
[0] => 18
[1] => google
[2] => 15
)
[39] => Array
(
[0] => 19
[1] => bing
[2] => 1
)
[40] => Array
(
[0] => 19
[1] => google
[2] => 18
)
[41] => Array
(
[0] => 19
[1] => yahoo
[2] => 3
)
[42] => Array
(
[0] => 20
[1] => google
[2] => 15
)
[43] => Array
(
[0] => 21
[1] => google
[2] => 18
)
[44] => Array
(
[0] => 21
[1] => yahoo
[2] => 1
)
[45] => Array
(
[0] => 22
[1] => bing
[2] => 1
)
[46] => Array
(
[0] => 22
[1] => google
[2] => 21
)
[47] => Array
(
[0] => 23
[1] => google
[2] => 8
)
[48] => Array
(
[0] => 23
[1] => yahoo
[2] => 1
)
)
I want that array to form like this:
$example_data = array(
array('00',1,12,1),//bing,google,yahoo
array('01',0,7,0),
array('02',0,5,0),
array('03',1,4,0),
array('04',0,7,0),
array('05',0,5,1),
array('06',1,2,0),
array('07',0,7,0),
array('08',1,8,0),
array('09',0,13,0),
array('10',1,19,1),
//should have more arrays in here, but I hope you got my point...
);
So basically, all the 00 of bing,google, and yahoo is being grouped by on a single array, then the other 01,02,03,etc.. are to be grouped by on a single array again.
So if you noticed that we have a zero on this array array('01',0,7,0), it because bing doesn't have a 01 value in the array, and yahoo also doesn't have a 01 value in the array, except for google which has a 01 that has a value of 7.
Any help how to transform this kind of array to the one I posted.
Your help will be greatly appreciated! Thanks!
<?php
$array=json_decode('[["00","bing","1"],["00","google","12"],["00","yahoo","1"],["01","google","7"],["02","google","5"],["03","bing","1"],["03","google","4"],["04","google","7"],["05","google","5"],["05","yahoo","1"],["06","bing","1"],["06","google","2"],["07","google","4"],["08","bing","1"],["08","google","8"],["09","bing","4"],["09","google","13"],["10","bing","1"],["10","google","19"],["10","yahoo","1"],["11","bing","1"],["11","google","23"],["11","yahoo","1"],["12","bing","1"],["12","google","18"],["13","bing","1"],["13","google","17"],["13","yahoo","1"],["14","bing","3"],["14","google","30"],["14","yahoo","2"],["15","google","15"],["15","yahoo","2"],["16","bing","1"],["16","google","22"],["16","yahoo","1"],["17","google","18"],["17","yahoo","2"],["18","google","15"],["19","bing","1"],["19","google","18"],["19","yahoo","3"],["20","google","15"],["21","google","18"],["21","yahoo","1"],["22","bing","1"],["22","google","21"],["23","google","8"],["23","yahoo","1"]]');
$temp=array();
$temp1=array();
foreach($array as $arr){
if(array_search($arr[0], $temp)===FALSE)
$temp[]=$arr[0];
}
foreach($temp as $t){
$bing=0;
$google=0;
$yahoo=0;
foreach($array as $arr){
if($t==$arr[0]){
if($arr[1]=='bing'){
$bing=$arr[2];
}else if($arr[1]=='google'){
$google=$arr[2];
}else if($arr[1]=='yahoo'){
$yahoo=$arr[2];
}
}
}
$temp1[]=array($t,$bing,$google,$yahoo);
}
var_dump($temp1);
?>
I managed to answer it on my own, not sure if this is efficient but it works! :)
//$new variable is the array that I the google analytics api returned
foreach($new as $key => $value){
if($value[1] == 'bing')
$combine['bing'][$value[0]] = $value[2];
if($value[1] == 'google')
$combine['google'][$value[0]] = $value[2];
if($value[1] == 'yahoo')
$combine['yahoo'][$value[0]] = $value[2];
}
$example_data = array();
for($i=0;$i<=23;$i++){
$tempI = $i;
if($i < 10)
$tempI = "0".$i;
$bing = 0;
$google = 0;
$yahoo = 0;
if(isset($combine['bing'][$tempI])){
$bing = $combine['bing'][$tempI];
}
if(isset($combine['google'][$tempI])){
$google = $combine['google'][$tempI];
}
if(isset($combine['yahoo'][$tempI])){
$yahoo = $combine['yahoo'][$tempI];
}
//time,bing,google,yahoo
$example_data[$i][0] = $tempI;
$example_data[$i][1] = $bing;
$example_data[$i][2] = $google;
$example_data[$i][3] = $yahoo;
}
echo "<pre>";
print_r($example_data);
echo "</pre>";
Thanks again StackOverflow! :)
How about this:
<?php
$initial = [
[ '00', 'bing', 1 ],
[ '00', 'google', 12 ],
[ '00', 'yahoo', 1 ],
[ '01', 'google', 7 ],
[ '02', 'google', 5 ],
[ '03', 'bing', 1 ],
[ '03', 'google', 4 ],
];
$searchEngines = ['bing', 'google', 'yahoo' ];
$temp = [];
foreach ( $initial as $data )
{
// Create array of the type $data['00']['google'] = 12
$temp[ $data[0] ][ $data[1] ] = $data[2];
}
$final = [];
// Now go over the newly created array
foreach( $temp as $code => $entry )
{
// Initialize a new array holding initially that $code ( I don't know if $code is correct term though )
$temp2 = [ $code ];
// And now for each of the defined search engines
foreach( $searchEngines as $se )
{
// Check if we have a value set
if (isset( $entry[$se] ) )
{
// If we do - use it
$temp2[] = $entry[$se];
}
else
{
// Otherwise use 0
$temp2[] = 0;
}
}
// Set it to the final resulting array
$final[] = $temp2;
}
echo '<pre>';print_r( $final );echo '</pre>';
And if someday you decide to add another search engine to the list all you'll have to do will be to add it to the $searchEngines array.

fill an array with missing values and keys

I have this array $all_zones that comes sometimes with missing keys and values and I would like to fill the array with empty values for the messing keys, here's the array:
Array
(
[0] => Array
(
[id_zone] => 1
[name] => Europe
[price] => Array
(
[0] => 3.00
[1] => 6.00
)
[id_delivery] => Array
(
[0] => 1
[1] => 2
)
)
[1] => Array
(
[id_zone] => 3
[name] => Asia
)
[2] => Array
(
[id_zone] => 4
[name] => Africa
[price] => Array
(
[0] => 3.00
[1] => 6.00
)
[id_delivery] => Array
(
[0] => 3
[1] => 4
)
)
[3] => Array
(
[id_zone] => 5
[name] => Oceania
)
)
The thing is the $all_zones[$key]['price'] depend on how many ranges there's for each Zone, inthis case $range_count = count($all_ranges); will display 2, so I'd like to fill the missing keys for 2 times : Here's the output:
Array
(
[0] => Array
(
[id_zone] => 1
[name] => Europe
[price] => Array
(
[0] => 3.00
[1] => 6.00
)
[id_delivery] => Array
(
[0] => 1
[1] => 2
)
)
[1] => Array
(
[id_zone] => 3
[name] => Asia
[price] => Array
(
[0] =>
[1] =>
)
[id_delivery] => Array
(
[0] =>
[1] =>
)
)
[2] => Array
(
[id_zone] => 4
[name] => Africa
[price] => Array
(
[0] => 3.00
[1] => 6.00
)
[id_delivery] => Array
(
[0] => 3
[1] => 4
)
)
[3] => Array
(
[id_zone] => 5
[name] => Oceania
[price] => Array
(
[0] =>
[1] =>
)
[id_delivery] => Array
(
[0] =>
[1] =>
)
)
)
Here's what I've tried so far and didn't succeed:
$range_count = count($all_ranges);
$i=0;
foreach ($all_zones as $key => $value) {
if(isset($value['id_zone']) && isset($value['name']) && (!isset($value['price']) || !isset($value['id_delivery']))){
if($range_count>$i){
$disabled[]=$key;
$all_zones[$key]['price'][] = '';
$all_zones[$key]['id_delivery'][] = '';
}
$i++;
}
}
Any help with this? Much appreciated.
try this
$range_count = count($all_ranges);
foreach ($all_zones as $key => $value) {
if(isset($value['id_zone']) && isset($value['name']) && (!isset($value['price']) || !isset($value['id_delivery']))){
$disabled[]=$key;
if((!isset($value['price']))
{
for($i=0; $i<$range_count<$i++)
{
$all_zones[$key]['id_delivery'][] = '';
}
}
if((!isset($value['id_delivery']))
{
for($i=0; $i<$range_count<$i++)
{
$all_zones[$key]['id_delivery'][] = '';
}
}
}
}
You might have operator precedence problem.
( (!isset($value['price']) || !isset($value['id_delivery'])) )
The way to do this is to loop through the array, with an array_merge() on each array within the parent array to set your 'defaults'.
$zone_template = array(
'id_zone' => '',
'name' => '',
'price' => array(
0 => '',
1 => ''
),
'id_delivery' = array(
0 => '',
1 => ''
)
);
foreach ($all_zones as $zone) {
array_merge($zone_template, $zone);
}
Can also be done with array_walk()

Split an array into mutiple arrays based on a value - PHP

What I've tried so far : I have an array as shown below, what i wanted to do is, create multiple arrays based on the value BEGIN_DAY:
Array
(
[0] => Array
(
[0] => BEGIN_DAY
[1] => 15
)
[1] => Array
(
[0] => 20130701
[1] => 4
[2] => 4
[3] => 3060
[4] => 1
)
[2] => Array
(
[0] => 20130702
[1] => 270
[2] => 757
[3] => 13812810
[4] => 4
)
[3] => Array
(
[0] => 20130703
[1] => 5
[2] => 123
[3] => 3894971
[4] => 2
)
[4] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
[5] => Array
(
[0] => END_DAY
[1] => 15
)
[6] => Array
(
[0] => BEGIN_DAY
[1] => 16
)
[7] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
and so on ...
I want to split this array into multiple different arrays when ever it begin with BEGIN_DAY so it should look like this:
The first :
Array
(
[0] => Array
(
[0] => BEGIN_DAY
[1] => 15
)
[1] => Array
(
[0] => 20130701
[1] => 4
[2] => 4
[3] => 3060
[4] => 1
)
[2] => Array
(
[0] => 20130702
[1] => 270
[2] => 757
[3] => 13812810
[4] => 4
)
[3] => Array
(
[0] => 20130703
[1] => 5
[2] => 123
[3] => 3894971
[4] => 2
)
[4] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
[5] => Array
(
[0] => END_DAY
[1] => 15
)
The second :
[0] => Array
(
[0] => BEGIN_DAY
[1] => 16
)
[1] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
What I've tried so far :
$days=array();
$split_by='BEGIN_DAY';
foreach ($day_all as $key => $value) {
if($day_all[$key][0]==$split_by){
$days=array_slice($day_all, 0,$split_by);
}
}
var_dump($days);
Much Appreciated!
$sk = "first";
foreach ($array as $key=>$value)
{
if(in_array(BEGIN_DAY, $value)&&$key!=0)
{
$sk = "second";
}
$result[$sk][] = $value;
}
echo "<pre>";
print_r($result);
$first = $result['first'];
$second = $result['second'];
Something like this :
$yourArray = ...; //is your array as described above
$finalArray = [];
$tempArray = [];
for($idx = 0; idx<$yourArray .length; idx++){
$tempArray = $yourArray[$idx]
if($yourArray[$idx][0] == 'BEGIN_DAY'){
$finalArray[] = $tempArray;
$tempArray = [];
}
}
The final array will contain the arrays. Each time the BEGIN_DAY is found, the array will be inserted into the finalArray and a new one is created.
To improve the code, check the existence of the cell too, to avoid index exception.

Categories