Hi I'm trying to create a multi dimensional array but having problems. I have a multi dimensional array that I'm trying to push other arrays onto. The arrays are created and pushed within an array.
$initialChild = $selectorDetailsArray[0];
$selectorDetailsMultiDimArray = array();
$multiDimHoldArray = array();
for($r=0;$r<count($selectorDetailsArray);$r+=3){
echo "Test vars are ".$selectorDetailsArray[$r]." : ".$initialChild."<br> ";
if(intval($selectorDetailsArray[$r]) == intval($initialChild)){
echo"<br> r is ".$r."<br>";
array_push($multiDimHoldArray,$selectorDetailsArray[$r+1],$selectorDetailsArray[$r+2]);
echo"<br> values are ".$selectorDetailsArray[$r+1]." ".$selectorDetailsArray[$r+2]."<br>";
print "<pre>";
print_r($multiDimHoldArray);
print "</pre>";
}else{
array_push($selectorDetailsMultiDimArray,$multiDimHoldArray);
$multiDimHoldArray = array();
echo "initial child is ".$initialChild."<br>";
$initialChild = $selectorDetailsArray[$r];
echo "initial child after change is ".$initialChild."<br>";
}
}
print "<pre>";
print_r($selectorDetailsMultiDimArray);
print "</pre>";
exit;
output is like this
Array
(
[0] => 65
[1] => 1
[2] => 0
[3] => 65
[4] => 29
[5] => 64
[6] => 66
[7] => 1
[8] => 69
[9] => 66
[10] => 29
[11] => 65
)
Test vars are 65 : 65
r is 0
values are 1 0
Array
(
[0] => 1
[1] => 0
)
Test vars are 65 : 65
r is 3
values are 29 64
Array
(
[0] => 1
[1] => 0
[2] => 29
[3] => 64
)
Test vars are 66 : 65
initial child is 65
initial child after change is 66
Test vars are 66 : 66
r is 9
values are 29 65
Array
(
[0] => 29
[1] => 65
)
Array
(
[0] => Array
(
[0] => 1
[1] => 0
[2] => 29
[3] => 64
)
)
I can't get it to push the second array - I must be missing something?
I've tried everything I can think of but can't figure out why the second array (that's been created) isn't pushed onto the multi dimensional array.
Any help would be great
Not sure what the overall purpose of the code is for "exactly" but I do understand the results you were trying to get, and I also understand why it was not working. I have commented my code so you should be able to see what it is doing. I created an array in the code to start with, but I presume it would work with whatever array you are getting even though you are not showing how it was generated.
$selectorDetailsArray = Array(65,1,0,65,29,64,66,1,69,66,29,65);
$initialChild = $selectorDetailsArray[0];
$selectorDetailsMultiDimArray = array();
$multiDimHoldArray = array();
$loopstrings = array(" :: 1st loop :: "," :: 2nd loop :: "," :: 3rd loop :: "," :: 4th loop :: ");
$loop = 0;
$dim = 0;
for($r=0;$r<count($selectorDetailsArray);$r+=3){
echo "<br>START".$loopstrings[$loop]."<br>";
echo "Test vars are ".$selectorDetailsArray[$r]." : ".$initialChild."<br> ";
if(intval($selectorDetailsArray[$r]) == intval($initialChild)){
echo"<br> r is ".$r."<br>";
array_push($multiDimHoldArray,$selectorDetailsArray[$r+1],$selectorDetailsArray[$r+2]);
echo"<br> values are ".$selectorDetailsArray[$r+1]." ".$selectorDetailsArray[$r+2]."<br>";
}else{
array_push($selectorDetailsMultiDimArray,$multiDimHoldArray);
$multiDimHoldArray = array();
// This needs to be here because the "if" is not run during this loop...
array_push($multiDimHoldArray,$selectorDetailsArray[$r+1],$selectorDetailsArray[$r+2]);
echo "initial child is ".$initialChild."<br>";
$initialChild = $selectorDetailsArray[$r];
echo "initial child after change is ".$initialChild." -- ($ r = $r)<br>";
}
echo "<br>END".$loopstrings[$loop++]."<br>";
}
// We push one last time since it successfully pushed twice more in the "if" but never ran the "else" a final time...
array_push($selectorDetailsMultiDimArray,$multiDimHoldArray);
echo "<br>";
echo "<br>BEGIN OUTPUT<br>";
print "<pre>";
print_r($selectorDetailsArray);
print_r($multiDimHoldArray);
print_r($selectorDetailsMultiDimArray);
print "</pre>";
This is the output that my code generates. I have added a few lines to make it readable, and also output all of the arrays at the end:
START :: 1st loop ::
Test vars are 65 : 65
r is 0
values are 1 0
END :: 1st loop ::
START :: 2nd loop ::
Test vars are 65 : 65
r is 3
values are 29 64
END :: 2nd loop ::
START :: 3rd loop ::
Test vars are 66 : 65
initial child is 65
initial child after change is 66 -- ($ r = 6)
END :: 3rd loop ::
START :: 4th loop ::
Test vars are 66 : 66
r is 9
values are 29 65
END :: 4th loop ::
BEGIN OUTPUT
Array
(
[0] => 65
[1] => 1
[2] => 0
[3] => 65
[4] => 29
[5] => 64
[6] => 66
[7] => 1
[8] => 69
[9] => 66
[10] => 29
[11] => 65
)
Array
(
[0] => 1
[1] => 69
[2] => 29
[3] => 65
)
Array
(
[0] => Array
(
[0] => 1
[1] => 0
[2] => 29
[3] => 64
)
[1] => Array
(
[0] => 1
[1] => 69
[2] => 29
[3] => 65
)
)
I would like to convert this formatted string to multidimensional array but I can't manage to do it.
The goal is to extract titles header (Dreg Time, FN, SS_ID, MAC, Sector ID, Type, Reason, CMPNT, Basic, Cid) and associating the corresponding values.
Because in the finality I would need to process the data in the "Reason" column.
String:
$log = ": mss get dereg_log 2 500
---------- MODEM 2 MAC ---------
Dreg Time FN SS_ID MAC Sector ID Type Reason CMPNT Basic Cid
2017/08/29 08:20:39:627 0x4da9f0 8277 00:24:a0:xx:xx:xx 1 2 25 0 86
2017/08/29 07:17:33:478 0x421c02 8238 00:23:a2:xx:xx:xx 1 3 59 0 47
2017/08/29 06:00:43:232 0x340a41 8508 00:23:a2:xx:xx:xx 1 1 6 13 317
Total Deregistrations since sector active in sector 1: 9576
Derigistrations since dreg log last reset in sector 1: 9576
Deregistration Types:
DEREG_MSS_IMMEDIATE = 0
DEREG_MSS_DREG_REQ_FROM_AP = 1
DEREG_MSS_DREG_REQ_FROM_MS = 2
DEREG_MSS_RNG_RSP_ABORT_FROM_AP = 3
DEREG_MSS_CONTACT_LOST = 4
---------------------------------
2017-Aug-29 08:31:53.860";
Desired Array:
Array
(
[0] => Array
(
[Dreg Time] => '2017/08/29 08:20:39:627'
[FN] => '0x4da9f0'
[SS_ID] => '8277'
[MAC] => '00:24:a0:xx:xx:xx'
[Sector ID] => '1'
[Type] => '2'
[Reason] => '25'
[CMPNT] => '0'
[Basic Cid] => '86'
)
[1] => Array
(
[Dreg Time] => '2017/08/29 07:17:33:478'
[FN] => '0x421c02'
[SS_ID] => '8238'
[MAC] => '00:23:a2:xx:xx:xx'
[Sector ID] => '1'
[Type] => '3'
[Reason] => '59'
[CMPNT] => '0'
[Basic Cid] => '47'
)
[2] => Array
(
[Dreg Time] => '2017/08/29 06:00:43:232'
[FN] => '0x340a41'
[SS_ID] => '8508'
[MAC] => '00:23:a2:xx:xx:xx'
[Sector ID] => '1'
[Type] => '1'
[Reason] => '6'
[CMPNT] => '13'
[Basic Cid] => '317'
)
)
Code tried:
echo '<pre>';
// remove blank lines
$log = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $log);
// remove new lines
$log = preg_replace('/^.+\n.+\n/', '', $log);
$lines = explode( "\n\n", $log );
$return = array();
foreach ($lines as $line) {
$items = explode("\n", $line);
$return[array_shift($items)] = $items;
}
print_r($return);
Result:
Array
(
[Dreg Time FN SS_ID MAC Sector ID Type Reason CMPNT Basic Cid] => Array
(
[0] => 2017/08/29 08:20:39:627 0x4da9f0 8277 00:24:a0:xx:xx:xx 1 2 25 0 86
[1] => 2017/08/29 07:17:33:478 0x421c02 8238 00:23:a2:xx:xx:xx 1 3 59 0 47
[2] => 2017/08/29 06:00:43:232 0x340a41 8508 00:23:a2:xx:xx:xx 1 1 6 13 317
[3] => Total Deregistrations since sector active in sector 1: 9576
[4] => Derigistrations since dreg log last reset in sector 1: 9576
[5] => Deregistration Types:
[6] => DEREG_MSS_IMMEDIATE = 0
[7] => DEREG_MSS_DREG_REQ_FROM_AP = 1
[8] => DEREG_MSS_DREG_REQ_FROM_MS = 2
[9] => DEREG_MSS_RNG_RSP_ABORT_FROM_AP = 3
[10] => DEREG_MSS_CONTACT_LOST = 4
[11] => ---------------------------------
[12] => 2017-Aug-29 08:31:53.860
)
)
Your code was an incomplete attempt, and contained a few errors - e.g. you removed newlines from the log, and then tried split the string based on newlines! Then you tried to split a single line by newline, when clearly a single line cannot, by implication, contain a newline. Also the code just naively ran through each line and added it directly to the array rather than creating an associative array within each index, as shown in the desired output. And lastly it did nothing to try and identify which lines actually contained the desired output.
This will do it:
$log = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $log);
$lines = explode( "\n", $log );
$return = array();
foreach ($lines as $line) {
$line = preg_replace("/\s+/", " ", $line); //condense and standardise the whitespace between each item, so that explode can work.
$items = explode(" ", $line);
if (count($items) == 10 && preg_match("/^[0-9]{4}/", $items[0])) //restrict to lines starting with the year, and with the correct number of columns
$return[] = array(
"Dreg Time" => $items[0]." ".$items[1],
"FN" => $items[2],
"SS_ID" => $items[3],
"MAC" => $items[4],
"Sector_ID" => $items[5],
"Type" => $items[6],
"Reason" => $items[7],
"CMPNT" => $items[8],
"Basic_Cid" => $items[9]
);
}
echo "<pre>".var_export($return, true)."</pre>";
Actually my date is $Date= '03/02/2015';
I want to increment this day with array of days .My array is
Array ( [0] => 1 [1] => 42 [2] => 70 [3] => 98 [4] => 186 [5] => 279 [6] => 372 [7] => 465 [8] => 558 [9] => 730 [10] => 1460 [11] => 4380 [12] => 1825 ).
I stored this array in a variable called $data.I want to increment my date with each of this days and print all result dates.How can i do it???
Try this with this code you can print dates
<?php
$Date = "2015-02-03";
$dataArray = Array ( 1 ,42 , 70 , 98 , 186 , 279 ,372);
foreach($dataArray as $val){
echo date('Y-m-d', strtotime($Date. " + $val days"))."</br>";
}
?>
Use this code
<?php
$Date= '03/02/2015';
$stamp= strtotime($Date);
$days=array(42,70,98,186,279,372,465,558,730,1460,4380,1825); //change these values
$values=array();
foreach($days as $day){
$newstamp=$stamp+($day*24*60*60);
$values[]=date("m/d/Y",$newstamp);
}
//now add to database
mysql_connect('localhost','user','pass'); //change user pass
mysql_select_db('yourdatabase'); //change here
foreach($values as $value) {
$sql="insert into yourtable values('".$value."',xxx,xxxx..))"; //change here
mysql_query($sql);
}
mysql_close();
?>
If I have a bunch of strings like XXX-WYC-5b, where XXX is any value between 1 and 999, how do I determine the length of XXX?
So I might have:
1: 6-WYC-5b
2: 32-WYC-5b
3: 932-W-5b
4: 22-XYQ-5b
5: 914-WYC-5b
And I want it to tell me the length of XXX, so:
1: 1 character
2: 2 characters
3: 3 characters
4: 2 characters
5: 3 characters
I would also like to know the value itself, so:
1: 6
2: 32
3: 932
4: 22
5: 914
I keep thinking there's a way to do this using substr_count() and explode(), but I can't seem to figure it out. Thanks very much for your help.
Use PHP's built-in string position function. Because it starts counting at 0, you don't even have to adjust the output:
$pos = strpos($string, "-");
For the second part, use PHP's substring function:
return substr($string, 0, $pos);
a different approach you may want to try:
<?php
$str[] = '6-WYC-5b';
$str[] = '32-WYC-5b';
$str[] = '932-W-5b';
$str[] = '22-XYQ-5b';
$str[] = '914-WYC-5b';
foreach($str as $v){
$result[] = getval($v);
}
function getval($value){
$seg = explode('-',$value);
return array('int'=>$seg[0],'intlen'=>strlen($seg[0]),'char'=>$seg[1],'charlen'=>strlen($seg[1]),'last'=>$seg[2],'lastlen'=>strlen($seg[2]));
}
print_r($result);
?>
outputs:
Array
(
[0] => Array
(
[int] => 6
[intlen] => 1
[char] => WYC
[charlen] => 3
[last] => 5b
[lastlen] => 2
)
[1] => Array
(
[int] => 32
[intlen] => 2
[char] => WYC
[charlen] => 3
[last] => 5b
[lastlen] => 2
)
[2] => Array
(
[int] => 932
[intlen] => 3
[char] => W
[charlen] => 1
[last] => 5b
[lastlen] => 2
)
[3] => Array
(
[int] => 22
[intlen] => 2
[char] => XYQ
[charlen] => 3
[last] => 5b
[lastlen] => 2
)
[4] => Array
(
[int] => 914
[intlen] => 3
[char] => WYC
[charlen] => 3
[last] => 5b
[lastlen] => 2
)
)
I'm trying to find each missing number in an array like the following.
Array (
[0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8
[8] => 9 [9] => 10 [10] => 11 [11] => 12 [12] => 13 [13] => 14 [14] => 15
[15] => 16 [16] => 17 [17] => 18 [18] => 19 [19] => 20 [20] => 21 [21] => 22
[22] => 23 [23] => 24 [24] => 25 [25] => 26 [26] => 27 [27] => 28 [28] => 29
[29] => 30 [30] => 31 [31] => 32 [32] => 33 [33] => 34 [34] => 35 [35] => 36
[36] => 37 [37] => 38 [38] => 39 [39] => 40 [40] => 41 [41] => 42 [42] => 43
[43] => 44 [44] => 45 [45] => 46 [46] => 47 [47] => 48 [48] => 49 [49] => 50
[50] => 51 [51] => 52 [52] => 53 [53] => 54 [54] => 55 [55] => 56 [56] => 57
[57] => 58 [58] => 59 [59] => 60 [60] => 61 [61] => 62 [62] => 63 [63] => 64
[64] => 67 [65] => 68 [66] => 69
)
The numbers 65,66 are missing in this particular array.
My question how do I figure out which numbers are missing with the help of PHP. Specifically what I need to find out is the lowest missing number.
Why: Because then I can assign that number to a member as an id.
You can make use of array_diff and range functions as:
// given array. 3 and 6 are missing.
$arr1 = array(1,2,4,5,7);
// construct a new array:1,2....max(given array).
$arr2 = range(1,max($arr1));
// use array_diff to get the missing elements
$missing = array_diff($arr2,$arr1); // (3,6)
I'm assuming the number is the element, not the key, of the array. I'm also assuming that the numbers start from 1, not 0.
$Expected = 1;
foreach ($InputArray as $Key => $Number)
{
if ($Expected != $Number)
{
break;
}
$Expected++;
}
echo $Number;
For big sorted arrays of unique numbers, you can binary search the array for either the lowest or highest unused number. Cost=Log2N. Example: 65536 items can be searched in 16 loops since
if ( arr[hi] - arr[lo] > hi - lo )
... there are unused numbers in that range ...
So (I don't know PHP, but it can be translated...):
lo = first entry index
hi = last entry index
if ( arr[hi] - arr[lo] == hi - lo )
return arr[hi]+1; // no gaps so return highest + 1
do
{
mid = (lo + hi) / 2;
if ( arr[mid] - arr[lo] > mid - lo ) // there is a gap in the bottom half somewhere
hi = mid; // search the bottom half
else
lo = mid; // search the top half
} while ( hi > lo + 1 ); // search until 2 left
return arr[lo]+1;
If given input is not in sorted order and size of input is very large then we can use following logic in any programming language:
Algorithm
bring smaller chunk into memory from large input
initialize three variables say min = 0, max = 0 and missingIds = []
scan smaller chunked input from left to right
if scannedValue found in missingIds
then,
pop scannedValue from missingIds
go to next value;
If scanned value is near to min
then,
find all the missing numbers between scannedValue and min, push into missingIds
min = scannedValue;
Else if scanned value is near to max
then,
find all the missing numbers between scannedValue and max, push into missingIds
max = scannedValue;
repeat above steps until large input scanned from left to right
Example in PHP
<?php
$largeInput = [40,41,42,43,44,45,1,2,3,4,5,6,7,8,9,10,11,12,13,14,35,36,37,38,39,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,67,68,69,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34];
$missingIds = [];
$min = 0;
$max = 0;
$chunkSize = 10;
$chunkNo = 0;
$currentInput = array_slice($largeInput, $chunkNo, $chunkSize);
while(count($currentInput) > 0) {
foreach($currentInput as $id) {
if(in_array($id,$missingIds)) {
$missingIds = array_diff($missingIds,[$id]);
continue;
}
if($id <= $min) {
$distMin = $min - $id;
if($distMin > 2) {
$tempArr = range($id+1,$min-1);
$missingIds = array_merge($missingIds, $tempArr);
$tempArr = [];
} else if ($distMin > 1) {
$tempArr = [$id+1];
$missingIds = array_merge($missingIds, $tempArr);
$tempArr = [];
}
$min = $id;
} else if ($id >= $max){
$distMax = $id - $max;
if($distMax > 2) {
$tempArr = range($max+1,$id-1);
$missingIds = array_merge($missingIds, $tempArr);
$tempArr = [];
} else if ($distMax > 1) {
$tempArr = [$max+1];
$missingIds = array_merge($missingIds, $tempArr);
$tempArr = [];
}
$max = $id;
}
}
$chunkNo++;
$currentInput = array_slice($largeInput, $chunkNo, $chunkSize);
}
print_r($missingIds);
//$idArrayMissing = array([0] => 1, [1] => 2, [2] => 4, [3] => 5, [4] => 6, [5] => 7);
$idArrayMissing = array(1, 2, 4, 5, 6, 7);
//$idArrayFull = array([0] => 1, [1] => 2, [2] => 3, [3] => 4, [4] => 5, [5] => 6);
$idArrayFull = array(1, 2, 3, 4, 5, 6);
function gap($arr)
{
while (list($k, $v) = each($arr))
if ($k != ($v-1))
return $k;
return -1;
}
print "ok:" . gap($idArrayMissing) . "<br/>\n";
print "full:" . gap($idArrayFull) . "<br/>\n";
The return of the gap function can be 2 values:
-1 could indicate that the array has been traversed and there are no free slots or
$k+1 which could indicate that the first free slot is on the end of the array.
It can also be done easily by using in_array() function like this:
// lets say $InputArray has all the data
// lets declare a variable which we will search inside the $InputArray array and lets initialize it with either 0 or 1 or with the minimum value found inside $InputArray
$start_counting = 1;
$max_value = count($InputArray);
if (!(in_array($start_counting, $InputArray)))
{
echo "Value: ".$start_counting." is missing!"."<br>" ;
}
else{
if($start_counting <= $max_value -1)
{$start_counting++;}
}
else if($start_counting > $max_value -1)
{
echo "All missing numbers printed!"
}
}