create a simple two dimensinal array from unknown depth array - php

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);

Related

PHP Subtract multidimensional arrays based on 2 values

I need to subtract the qt from two arrays based on id and type, keeping the array complete.
How do I do in php to be able to subtract these arrays?
I have 2 arrays:
=> this is the first array with multiple key "down"
Array
(
[0] => Array
(
[id] => 26
[loc] => 1
[type] => down
[qt] => 12
)
[1] => Array
(
[id] => 32
[loc] => 1
[type] => down
[qt] => 34
)
[2] => Array
(
[id] => 26
[loc] => 2
[type] => down
[qt] => 5
)
[3] => Array
(
[id] => 86
[loc] => 3
[type] => down
[qt] => 45
)
[4] => Array
(
[id] => 23
[loc] => 9
[type] => down
[qt] => 3
)
[5] => Array
(
[id] => 23
[loc] => 3
[type] => down
[qt] => 99
)
)
=> this is the second array with multiple key "up"
Array
(
[0] => Array
(
[id] => 26
[loc] => 1
[type] => up
[qt] => 5
)
[1] => Array
(
[id] => 86
[loc] => 3
[type] => up
[qt] => 27
)
[2] => Array
(
[id] => 23
[loc] => 9
[type] => up
[qt] => 3
)
)
=> I need cubtract "qt" (if "id" and "loc" are the same then subtract the "qt")
Array
(
[0] => Array
(
[id] => 26
[loc] => 1
[type] => total
[qt] => 7
)
[1] => Array
(
[id] => 32
[loc] => 1
[type] => total
[qt] => 34
)
[2] => Array
(
[id] => 26
[loc] => 2
[type] => total
[qt] => 5
)
[3] => Array
(
[id] => 86
[loc] => 3
[type] => total
[qt] => 18
)
[4] => Array
(
[id] => 23
[loc] => 9
[type] => total
[qt] => 0
)
[5] => Array
(
[id] => 23
[loc] => 3
[type] => down
[qt] => 99
)
)
Try this out
function findMatch($array, $id, $loc)
{
foreach ($array as $key => $item) {
if ($item["id"] == $id and $item["loc"] == $loc) {
return $key;
}
}
return false;
}
$total = [];
foreach($down as $d) {
$matched = findMatch($up, $d["id"], $d["loc"]);
if($matched !== false){
$total[] = array_replace($d, [
"type" => "total",
"qt" => ($d["qt"] - $up[$matched]["qt"])
]);
} else {
$total[] = array_replace($d, ["type" => "total"]);
}
}
print_r($total);

multidimensional array sort based on database column value compare

I have following multidimensional array
Array
(
[1] => 22
[2] => 12
[3] => Array
(
[0] => 4
[1] => 21
)
[5] => Array
(
[0] => 9
[1] => 3
)
[7] => 7
[8] => 6
[9] => 1
[10] => 2
[11] => 10
[12] => 0
[13] => 23
[14] => 18
[15] => 16
[16] => 19
[17] => 15
[18] => 5
[19] => 8
[20] => 11
[21] => 13
[22] => 17
[23] => 14
[24] => 20
)
I want to sort morethan one value below array from main array.
Array
(
[3] => Array
(
[0] => 4
[1] => 21
)
[5] => Array
(
[0] => 9
[1] => 3
)
)
Each key value compare with database column and sort an array. If not found value then array keep as it is.
For example:
The below key value is 21 have in database column then am expected results as,
[3] => Array
(
[0] => 21
[1] => 4
)
look as next array, the next array key value not found in the database then expected results as it is,
[5] => Array
(
[0] => 9
[1] => 3
)
The final output look for,
Array
(
[1] => 22
[2] => 12
[3] => Array
(
[0] => 21
[1] => 4
)
[5] => Array
(
[0] => 9
[1] => 3
)
[7] => 7
[8] => 6
[9] => 1
[10] => 2
[11] => 10
[12] => 0
[13] => 23
[14] => 18
[15] => 16
[16] => 19
[17] => 15
[18] => 5
[19] => 8
[20] => 11
[21] => 13
[22] => 17
[23] => 14
[24] => 20
)
Could you please help me
Check this.
$dump_array=Array('1' => 2,'2' => 12,'3' => Array
('0' => 8,'1' => 21 ),
'5' => Array ('0' => 9,'1' => 3),
'7' => 7,
'8' => 64,
'9' => 15,
'10' => 22,
);
echo "<pre> Soure :"; print_r($dump_array);echo "</pre>";
sort($dump_array);
$result_array=array();
$array_unlist=array();
foreach($dump_array as $key=>$value){
if(is_array($value)){
sort($value);
array_push($array_unlist,$value);
}
else {array_push($result_array,$value);}
}
foreach($result_array as $index=>$val){
foreach($array_unlist as $key=>$digit){
if($val<$digit[0])
{
$res=$index.".5";
$result_array[$res]=$digit;
}
}
}
ksort($result_array);
echo "<pre> Result :"; print_r(array_values($result_array));echo "</pre>";
output:
Soure :Array
(
[1] => 2
[2] => 12
[3] => Array
(
[0] => 8
[1] => 21
)
[5] => Array
(
[0] => 9
[1] => 3
)
[7] => 7
[8] => 64
[9] => 15
[10] => 22
)
Result :Array
(
[0] => 2
[1] => Array
(
[0] => 3
[1] => 9
)
[2] => 7
[3] => Array
(
[0] => 8
[1] => 21
)
[4] => 12
[5] => 15
[6] => 22
[7] => 64
)
try this
foreach($array as $k => $v){
if(is_array($v)){
rsort($array[$k]);
}
}
print_r($array);

Parsing a Two level - array, PHP

What I have
I have two-level array as follows:
<?php $myarray = Array (
[0] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[1] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[2] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[3] => Array ( [1] => 0 [2] => 1 [3] => 0 [4] => 0 )
[4] => Array ( [1] => 0 [2] => 1 [3] => 0 [4] => 0 )
[5] => Array ( [1] => 1 [2] => 0 [3] => 0 [4] => 0 )
[6] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[7] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[8] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[9] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[10] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[11] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[12] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[13] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[14] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 [color] => red )
[15] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 [color] => yellow )
[16] => Array ( [1] => 0 [2] => 0 [3] => 1 [4] => 0 [color] => yellow )
[17] => Array ( [1] => 0 [2] => 0 [3] => 1 [4] => 0 [color] => yellow )
[18] => Array ( [1] => 0 [2] => 0 [3] => 1 [4] => 0 [color] => red )
[19] => Array ( [1] => 0 [2] => 0 [3] => 1 [4] => 0 [color] => yellow )
[20] => Array ( [1] => 0 [2] => 0 [3] => 1 [4] => 0 [color] => yellow )
[21] => Array ( [1] => 0 [2] => 1 [3] => 0 [4] => 0 [color] => yellow )
[22] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 [color] => yellow )
[23] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 [color] => yellow )
[24] => Array ( [1] => 0 [2] => 0 [3] => 1 [4] => 0 [color] => yellow )
[25] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 [color] => red )
[26] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 [color] => yellow )
) ?>
The Inner arrays (index 0 - 26 ) contain 4 or 5 entries each corresponding to positions 1,2,3,4 and entry 5 which can be red, yellow or green. A position can be 0 or 1.
What I want to do:
1) I want to add all values at position 1, position 2, position 3 and position 4 of the inner arrays (Total of a column )
2) If any inner array has more than one "position" that is 1, the whole "row" (entry) needs to be flagged and "unset from $myarray
If it is not clear, please ask some more and I will try my best to clarify.
Thanks
total of a column is pretty simple by using array_sum() combined with array_column():
$position1Sum = array_sum(array_column($myarray,'1'));
$position2Sum = array_sum(array_column($myarray,'2'));
$position3Sum = array_sum(array_column($myarray,'3'));
$position4Sum = array_sum(array_column($myarray,'4'));
to clean the array of anything with more than one position active:
/* editing the same array you are looping thru is typically bad - lets make a copy first.*/
$cleanArray = $myarray;
/* loop thru - and unset the rows where more than one position are 1
(which will make their sum more than 1) */
foreach($myarray as $key => $value){
if($value[1] + $value[2] + $value[3] +$value[4] > 1){
unset($cleanArray[$key]);
}
}
$cleanArray will now contain the same contents as $myarray, minus the rows that have more than one position set to 1. Of course there are several other ways to potentially tackle this - I try to stick to the cleanest to read later if possible... :-)

How to get values in an array with many arrays in php?

I am confused with this list of array. I have a text file that contains the values that I will need to insert to the database. I have exploded it so that I can get those separated values.
REP 1020001,3,140822,140822;0111,260.00,23,34,3,54,1,2,4,5,12,23,46;0214,22.00,32,4,11,25,4,12,23,5,2,2,44;0313,25.00,5,52,12,45,12,5,6,7,12,3,33;
My code is just like this:
$read = FileRead($tmp);
$perline = explode(";",$read);
for($j=0; $j<count($perline); $j++)
{
$percomma = explode(",", $perline[$j]);
print_r ($percomma);
}
And the result is this:
Array ( [0] => 1020001 [1] => 3 [2] => 140822 [3] => 140822 ) Array ( [0] => 0111 [1] => 260.00 [2] => 23 [3] => 34 [4] => 3 [5] => 54 [6] => 1 [7] => 2 [8] => 4 [9] => 5 [10] => 12 [11] => 23 [12] => 46 ) Array ( [0] => 0214 [1] => 22.00 [2] => 32 [3] => 4 [4] => 11 [5] => 25 [6] => 4 [7] => 12 [8] => 23 [9] => 5 [10] => 2 [11] => 2 [12] => 44 ) Array ( [0] => 0313 [1] => 25.00 [2] => 5 [3] => 52 [4] => 12 [5] => 45 [6] => 12 [7] => 5 [8] => 6 [9] => 7 [10] => 12 [11] => 3 [12] => 33 ) Array ( [0] => )
What should I do to get the value from the 2nd array to the 4th array? Should I put it inside another array to make it multidimensional? Or there are other ways to solve this?
EDIT
My question is how will I be able to get the values from the print_r($percomma) when there are a lot of arrays in the result. The result array is up there that says And the result is this
EDIT 2
As making the array to be multidimensional I get this as a result:
Array ( [0] => Array ( [0] => 0111 [1] => 260.00 [2] => 23 [3] => 34 [4] => 3 [5] => 54 [6] => 1 [7] => 2 [8] => 4 [9] => 5 [10] => 12 [11] => 23 [12] => 46 ) ) Array ( [0] => Array ( [0] => 0214 [1] => 22.00 [2] => 32 [3] => 4 [4] => 11 [5] => 25 [6] => 4 [7] => 12 [8] => 23 [9] => 5 [10] => 2 [11] => 2 [12] => 44 ) ) Array ( [0] => Array ( [0] => 0313 [1] => 25.00 [2] => 5 [3] => 52 [4] => 12 [5] => 45 [6] => 12 [7] => 5 [8] => 6 [9] => 7 [10] => 12 [11] => 3 [12] => 33 ) ) Array ( [0] => Array ( [0] => ) )
It shows that they're all individual arrays. My code is this:
$read = FileRead($tmp);
$perline = explode(";",$read);
for($j=0; $j<count($perline); $j++)
{
$percomma = explode(",", $perline[$j]);
$entries = array($percomma);
print_r ($entries);
}
EDIT 3
From subas_poudel's answer I get this result:
Array
(
)
Array
(
[0] => Array
(
[0] => 0111
[1] => 260.00
[2] => 23
[3] => 34
[4] => 3
[5] => 54
[6] => 1
[7] => 2
[8] => 4
[9] => 5
[10] => 12
[11] => 23
[12] => 46
)
)
Array
(
[0] => Array
(
[0] => 0111
[1] => 260.00
[2] => 23
[3] => 34
[4] => 3
[5] => 54
[6] => 1
[7] => 2
[8] => 4
[9] => 5
[10] => 12
[11] => 23
[12] => 46
)
[1] => Array
(
[0] => 0214
[1] => 22.00
[2] => 32
[3] => 4
[4] => 11
[5] => 25
[6] => 4
[7] => 12
[8] => 23
[9] => 5
[10] => 2
[11] => 2
[12] => 44
)
)
Array
(
[0] => Array
(
[0] => 0111
[1] => 260.00
[2] => 23
[3] => 34
[4] => 3
[5] => 54
[6] => 1
[7] => 2
[8] => 4
[9] => 5
[10] => 12
[11] => 23
[12] => 46
)
[1] => Array
(
[0] => 0214
[1] => 22.00
[2] => 32
[3] => 4
[4] => 11
[5] => 25
[6] => 4
[7] => 12
[8] => 23
[9] => 5
[10] => 2
[11] => 2
[12] => 44
)
[2] => Array
(
[0] => 0313
[1] => 25.00
[2] => 5
[3] => 52
[4] => 12
[5] => 45
[6] => 12
[7] => 5
[8] => 6
[9] => 7
[10] => 12
[11] => 3
[12] => 33
)
)
Array
(
[0] => Array
(
[0] => 0111
[1] => 260.00
[2] => 23
[3] => 34
[4] => 3
[5] => 54
[6] => 1
[7] => 2
[8] => 4
[9] => 5
[10] => 12
[11] => 23
[12] => 46
)
[1] => Array
(
[0] => 0214
[1] => 22.00
[2] => 32
[3] => 4
[4] => 11
[5] => 25
[6] => 4
[7] => 12
[8] => 23
[9] => 5
[10] => 2
[11] => 2
[12] => 44
)
[2] => Array
(
[0] => 0313
[1] => 25.00
[2] => 5
[3] => 52
[4] => 12
[5] => 45
[6] => 12
[7] => 5
[8] => 6
[9] => 7
[10] => 12
[11] => 3
[12] => 33
)
)
The last set of array is what I needed but how can I get those values?
EDIT 4
With my simple mistake I put the array_slice inside my for loop that's why I get too many arrays. Now this is the result of #subas_poudel's answer.
Array
(
[0] => Array
(
[0] => 0111
[1] => 260.00
[2] => 23
[3] => 34
[4] => 3
[5] => 54
[6] => 1
[7] => 2
[8] => 4
[9] => 5
[10] => 12
[11] => 23
[12] => 46
)
[1] => Array
(
[0] => 0214
[1] => 22.00
[2] => 32
[3] => 4
[4] => 11
[5] => 25
[6] => 4
[7] => 12
[8] => 23
[9] => 5
[10] => 2
[11] => 2
[12] => 44
)
[2] => Array
(
[0] => 0313
[1] => 25.00
[2] => 5
[3] => 52
[4] => 12
[5] => 45
[6] => 12
[7] => 5
[8] => 6
[9] => 7
[10] => 12
[11] => 3
[12] => 33
)
)
you can use multidimensional array to hold all the value. then you can get all the array you want by either $percomma[index you want] or using array_slice.
$read = FileRead($tmp);
$perline = explode(";",$read);
for($j=0; $j<count($perline); $j++)
{
$percomma[] = explode(",", $perline[$j]);
}
echo '<pre>'.print_r (array_slice($percomma,1,3),true).'</pre>';
$array = array
(
array("bla",22,18),
array("blaa",15,13),
array("blaaa",5,2),
array("blaaaa",17,15)
);
for ($row = 0; $row < 4; $row++) {
echo "<p><b>Row number $row</b></p>";
echo "<ul>";
for ($col = 0; $col < 3; $col++) {
echo "<li>".$array[$row][$col]."</li>";
}
echo "</ul>";
}
I'm going to copy/edit your original code posting, to show what I was talking about in my comment.
$read = FileRead($tmp);
$perline = explode(";",$read);
$percomma = array(); //new array-declaration line
for($j=0; $j<count($perline); $j++)
{ $percomma[j] = explode(",", $perline[$j]);
print_r ($percomma[j]);
}
Your first "And the result is" line is a printed sequence of arrays, and would be unchanged by implementing the above tweaks to your code. However, because the values in the $percomma variable are no longer overwritten in each loop that obtains an array from the explode() function, you may now, at any point after the above code, do something like:
print_r($percomma[1]); //re-print 2nd array, or
$ary_el = $percomma[2][1]; //get 2nd element of 3rd array (22.00) into a variable.

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