last array value coming blank? - php

my array is (coming from database)
Array ( [0] => Array ( [date] => 2011-05-12 [user_id] => 48 ) [1] => Array ( [date] => 2011-05-31 [user_id] => 77 ) [2] => Array ( [date] => 2011-05-03 [user_id] => 318 ) [3] => Array ( [date] => 2011-05-20 [user_id] => 619 ) [4] => Array ( [date] => 2011-05-21 [user_id] => 619 ) [5] => Array ( [date] => 2011-05-25 [user_id] => 619 ) [6] => Array ( [date] => 2011-05-28 [user_id] => 619 ) [7] => Array ( [date] => 2011-05-11 [user_id] => 747 ) [8] => Array ( [date] => 2011-05-12 [user_id] => 747 ) [9] => Array ( [date] => 2011-05-29 [user_id] => 747 ) [count] => 10 )
and when i use
foreach($appVenueAmbassador as $kk => $venueUserId){
$unique[] = $appVenueAmbassador[$kk]['user_id'];
}
on that query then i get
Array ( [0] => 48 [1] => 77 [2] => 318 [3] => 619 [4] => 619 [5] => 619 [6] => 619 [7] => 747 [8] => 747 [9] => 747 [10] => )
means last element is coming blank
how can i solve this problem ?

That would be because the last element is [count] => 10 and doesn't have a user_id. You should be seeing a warning regarding this if you'd switch on error reporting. Also, the way you're grabbing that value is quite overcomplicated. Try this:
foreach ($appVenueAmbassador as $venue){
if (isset($venue['user_id'])) {
$unique[] = $venue['user_id'];
}
}

Check to see that the key is a number before using it, or remove the count index from the array before processing.

Here is a way to solve your problem
<?php
for ($i = 0; $i < $appVenueAmbassador['count']; $i++) {
$unique[] = $appVenueAmbassador[$i]['user_id'];
}
?>
It will not use the last key count

Related

PHP trimensional array

I was wondering if someone could help me with this looping array in PHP as I am still learning the language.
I need to loop into the 3-dimensional (3D) array below in order to:
Check the [room_type_id] values and if its true to a specific value, I would return [check_in_times] array value for this same parent array.
For example:
if [room_type_ids] = 261
I would need to return [check_in_times] = 1
if [room_type_ids] = 36
I would need to return [check_in_times] = 2
My array is:
$myarray = Array (
[0] => Array (
[check_in_times] => Array (
[0] => 1
)
[room_type_ids] => Array (
[0] => 261
[1] => 281
[2] => 283
[3] => 292
[4] => 296
[5] => 365
[6] => 381
[7] => 387
[8] => 389
[9] => 730
)
[season_ids] => Array (
[0] => 0
)
)
[1] => Array (
[check_in_times] => Array (
[0] => 2
)
[room_type_ids] => Array (
[0] => 36
[1] => 38
[2] => 300
[3] => 318
[4] => 336
[5] => 889
[6] => 897
[7] => 728
[8] => 745
[9] => 747
)
[season_ids] => Array (
[0] => 0
)
)
)
Thank you!
Something like:
$room = 261;
$checks = 0;
foreach ($myarray as $subset)
{
if (in_array($room, $subset['room_type_ids']))
{
$checks = $subset['check_in_times'][0];
break;
}
}

Function token_get_all not showing any tokens

I've got this simple code to test the output of token_get_all...
$arr = token_get_all("<?php $array=array(1,2,3); foreach($array as $key => $value) print($value); ?>");
print("<pre>");
print_r($arr);
print("</pre>");
But what ends up being displayed is this:
Array
(
[0] => Array
(
[0] => 372
[1] => 1
)
[1] => =
[2] => Array
(
[0] => 362
[1] => array
[2] => 1
)
[3] => (
[4] => Array
(
[0] => 305
[1] => 1
[2] => 1
)
[5] => ,
[6] => Array
(
[0] => 305
[1] => 2
[2] => 1
)
[7] => ,
[8] => Array
(
[0] => 305
[1] => 3
[2] => 1
)
[9] => )
[10] => ;
[11] => Array
(
[0] => 375
[1] =>
[2] => 1
)
[12] => Array
(
[0] => 322
[1] => foreach
[2] => 1
)
[13] => (
[14] => Array
(
[0] => 375
[1] =>
[2] => 1
)
[15] => Array
(
[0] => 326
[1] => as
[2] => 1
)
[16] => Array
(
[0] => 375
[1] =>
[2] => 1
)
[17] => Array
(
[0] => 360
[1] => =>
[2] => 1
)
[18] => Array
(
[0] => 375
[1] =>
[2] => 1
)
[19] => )
[20] => Array
(
[0] => 375
[1] =>
[2] => 1
)
[21] => Array
(
[0] => 266
[1] => print
[2] => 1
)
[22] => (
[23] => )
[24] => ;
[25] => Array
(
[0] => 375
[1] =>
[2] => 1
)
[26] => Array
(
[0] => 374
[1] => ?>
[2] => 1
)
)
From everything I've read about token_get_all, I'd expect the [0] key of these arrays to be the token names. What's going on with my code/server that I'm getting this instead?
I've also tried doing:
$arr = token_get_all(file_get_contents('someOtherValidPHPFile.php'));
And I get the same kind of result.
I'm using PHP version 5.4.19
Yes the token type is on index 0.
This is just a numeric value which identifies the token type. You can then compare them against the following list of token types: List of Parser Tokens
You can get the token name by using the token_name() function.
Tokens are defined as constants. E.g. the constant is named T_ARRAY and its value is 362. You can compare the tokens to that constant:
if ($token[0] == T_ARRAY) ...
If you want to get the readable name, use token_name.

create a simple two dimensinal array from unknown depth array

I have unknown depth of array but i want to make simple two dimensional array as following , earlier i think my question doesn't make exact sense so i edited this , please help
private function arrayDepth($a) {
$arr = array();
foreach ($a as $key => $val) {
if (is_array($val)) {
$this->arrayDepth($val);
}
else {
$arr[] = $a;
}
}
}
my current array is
Array
(
[0] => Array
(
[0] => Array
(
[0] => 41
[uid] => 41
[1] => 16
[pid] => 16
[2] => 30
[oid] => 30
[3] => 1
[value] => 1
[4] => 0
[optval] => 0
[5] => 2014-05-26 16:41:31
[updateDate] => 2014-05-26 16:41:31
)
[1] => Array
(
[0] => Array
(
[0] => 42
[uid] => 42
[1] => 16
[pid] => 16
[2] => 31
[oid] => 31
[3] => 1
[value] => 1
[4] => 0
[optval] => 0
[5] => 2014-05-26 16:45:49
[updateDate] => 2014-05-26 16:45:49
)
[1] => Array
(
[0] => 44
[uid] => 44
[1] => 16
[pid] => 16
[2] => 31
[oid] => 31
[3] => 1
[value] => 1
[4] => 0
[optval] => 0
[5] => 2014-05-26 16:42:01
[updateDate] => 2014-05-26 16:42:01
)
[2] => Array
(
[0] => 47
[uid] => 47
[1] => 16
[pid] => 16
[2] => 31
[oid] => 31
[3] => 1
[value] => 1
[4] => 0
[optval] => 0
[5] => 2014-05-26 16:45:19
[updateDate] => 2014-05-26 16:45:19
)
[3] => Array
(
[0] => Array
(
[0] => 51
[uid] => 51
[1] => 16
[pid] => 16
[2] => 32
[oid] => 32
[3] => 1
[value] => 1
[4] => 0
[optval] => 0
[5] => 2014-05-26 16:47:27
[updateDate] => 2014-05-26 16:47:27
)
)
)
)
)
i need my output like all array which contain some value into a single array like
array(
[0] => Array
(
[0] => 41
[uid] => 41
[1] => 16
[pid] => 16
[2] => 30
[oid] => 30
[3] => 1
[value] => 1
[4] => 0
[optval] => 0
[5] => 2014-05-26 16:41:31
[updateDate] => 2014-05-26 16:41:31
)
[1] => Array
(
[0] => 42
[uid] => 42
[1] => 16
[pid] => 16
[2] => 31
[oid] => 31
[3] => 1
[value] => 1
[4] => 0
[optval] => 0
[5] => 2014-05-26 16:45:49
[updateDate] => 2014-05-26 16:45:49
)
[2] => Array
(
[0] => 44
[uid] => 44
[1] => 16
[pid] => 16
[2] => 31
[oid] => 31
[3] => 1
[value] => 1
[4] => 0
[optval] => 0
[5] => 2014-05-26 16:42:01
[updateDate] => 2014-05-26 16:42:01
) ...... and so on .....
so finally i solved this issue, if you guys have any better solution please provide
function arrayDepth($a,&$arr) {
foreach ($a as $key => $val) {
if(is_array($val))
if (is_array(#$val[0])) {
arrayDepth($val,$arr);
}
else {
$arr[] = $val;
}
}
}
arrayDepth($aNonFlat,$arr);
var_dump($arr);

Group by on a multidimensional array

I'm afraid that it is simply not possible what I'm trying to do, but I hope you can help me to find a nice way to solve this problem.
I've got the following PHP-array:
Array (
[0] => Array ( [article] => 10.499-1 [operation] => KN_KABEL [date] => 31-05-2013 [hours] => 0 [quantity] => 1 )
[1] => Array ( [article] => 10.499-1 [operation] => LAS_LABEL [date] => 31-05-2013 [hours] => 0 [quantity] => 1 )
[2] => Array ( [article] => 10.499-1 [operation] => ASS_HARNES [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[3] => Array ( [article] => 10.499-1 [operation] => CONTROLE [date] => 07-06-2013 [hours] => 0 [quantity] => 1 )
[4] => Array ( [article] => 24.030 [operation] => LAS_LABEL [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[5] => Array ( [article] => 24.030 [operation] => ZAGEN-RAIL [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[6] => Array ( [article] => 24.030 [operation] => KN_KABEL [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[7] => Array ( [article] => 24.030 [operation] => ASS_RAIL [date] => 05-06-2013 [hours] => 0 [quantity] => 1 )
[8] => Array ( [article] => 791 070-6/GS/P [operation] => GS_UNIT [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[9] => Array ( [article] => 791 070-6/GS/P [operation] => PR_UNIT [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[10] => Array ( [article] => 791 070-6/GS/P [operation] => LAS_LABEL [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[11] => Array ( [article] => ESS-0834E/LE-CD200 [operation] => MAGAZIJN [date] => 10-06-2013 [hours] => 0 [quantity] => 1 )
[12] => Array ( [article] => ESS-0834E/LE-CD200 [operation] => PR_UNIT [date] => 11-06-2013 [hours] => 0 [quantity] => 1 )
[13] => Array ( [article] => ESS-0834E/LE-CD200 [operation] => LAB_PLAKKE [date] => 11-06-2013 [hours] => 0 [quantity] => 1 )
)
What I'm trying to do is to count for each date (key "datum") the sum of hours (in this example they are all 0 but I still want to do this because this will change in the future). What would be very practicle is to do a query like sql like SELECT SUM(hours) GROUP BY date but this is no SQL unfortunately.
Is there a way to group (and order) my array by a specific key (in this case "date") or, if not, is there an other way to get the result what I want?
EDIT
I recently added a key "department" which should be grouped by to. Thereby I do not only want to count the sum of "hours", but "quantity" too
Just create simply foreach for this.
$arr = array();
$sums = array();
foreach($arr as $k=>$v)
{
if(!isset($sums[$v['date']][$v['department']]['hours'])) $sums[$v['date']][$v['department']]['hours'] = 0;
if(!isset($sums[$v['date']][$v['department']]['quantity'])) $sums[$v['date']][$v['department']]['quantity'] = 0;
$sums[$v['date']][$v['department']]['hours'] += $v['hours'];
$sums[$v['date']][$v['department']]['quantity'] += $v['quantity'];
}
print_r($sums);
it will create array $sum where keys are your dates. If value doesn't exists it will add the value of hours to 0, else if it exists it will add to existing value.
edit
Fitted to OP needs.

Generate a new array that groups by key and gets a SUM of another key

I would like to take the following array and generate a new array that will group all identical keys with the same ID while getting a SUM of the views and grabbing the last DATE only.
Original Array
Array (
[0] => Array (
[id] => 10
[views] => 276
[date] => 2012-01-30 10:55:00
[total] => N
)
[1] => Array (
[id] => 40
[views] => 287
[date] => 2012-01-27 10:00:29
[total] => Y
)
[2] => Array (
[id] => 40
[views] => 824
[date] => 2012-01-29 14:40:45
[total] => Y
)
[3] => Array (
[id] => 42
[views] => 723
[date] => 2012-01-28 20:15:58
[total] => N
)
[4] => Array (
[id] => 43
[views] => 428
[date] => 2012-01-28 17:14:31
[total] => N
)
[5] => Array (
[id] => 45
[views] => 174
[date] => 2012-01-20 18:01:11
[total] => N
)
)
New Array
Array (
[0] => Array (
[id] => 10
[views] => 276
[date] => 2012-01-30 10:55:00
[total] => N
)
[1] => Array (
[id] => 40
[views] => 1111
[date] => 2012-01-29 14:40:45
[total] => Y
)
[2] => Array (
[id] => 42
[views] => 723
[date] => 2012-01-28 20:15:58
[total] => N
)
[3] => Array (
[id] => 43
[views] => 428
[date] => 2012-01-28 17:14:31
[total] => N
)
[4] => Array (
[id] => 45
[views] => 174
[date] => 2012-01-20 18:01:11
[total] => N
)
)
function last($a, $b) {
if (strtotime($a) > strtotime($b))
return $a;
else
return $b;
}
$new_array = array();
foreach ($original_array as $data) {
if (array_key_exists($data['id'], $new_array)) {
$new_array[$data['id']]['views'] += $data['views'];
$new_array[$data['id']]['date'] = last($new_array[$data['id']]['date'], $data['date']);
} else
$new_array[$data['id']] = $data;
}
$new_array = array_values($new_array);

Categories