Regex date formatting for templates not working - php

I'm trying to replace variables like {{{month}}} in a template to the current month and {{{month+1}}} to current month + 1.
That's not the hardest part of my code, except that the regex I wrote doesn't yield expected results.
$string = '{{{year}}}{{{month+1}}}';
preg_match_all('/{{{(?:([yY])ear|([mM])onth|([dD])ay)(?:(?<operation>[-|+])(?<amount>[1-9]+))?}}}/m', $string, $matches);
var_dump($matches);
Why do I have so much empty array entries?
I was expecting
[0] => array('{{{year}}}', '{{{month+1}}}')
[1] => array('y', 'm')
[2] => array('', '+')
[3] => array('', '1')
What am I doing wrong?
The respond of the above code is:
array(8) {
[0]=>
array(2) {
[0]=>
string(10) "{{{year}}}"
[1]=>
string(13) "{{{month+1}}}"
}
[1]=>
array(2) {
[0]=>
string(1) "y"
[1]=>
string(0) ""
}
[2]=>
array(2) {
[0]=>
string(0) ""
[1]=>
string(1) "m"
}
[3]=>
array(2) {
[0]=>
string(0) ""
[1]=>
string(0) ""
}
["operation"]=>
array(2) {
[0]=>
string(0) ""
[1]=>
string(1) "+"
}
[4]=>
array(2) {
[0]=>
string(0) ""
[1]=>
string(1) "+"
}
["amount"]=>
array(2) {
[0]=>
string(0) ""
[1]=>
string(1) "1"
}
[5]=>
array(2) {
[0]=>
string(0) ""
[1]=>
string(1) "1"
}
}

You may use a "generic" character class to match the first letters of month, year and day, and then use an alternation with positive look-behinds to make sure we match what we need.
preg_match_all('/{{{([yYmMdD])(?:(?<=[Yy])ear|(?<=[Mm])onth|(?<=[Dd])ay)(?:([-‌​+])([1-9]+))?}}}/m', $string, $matches);
See IDEONE demo
And this is the print_r view:
Array
(
[0] => Array
(
[0] => {{{year}}}
[1] => {{{month+1}}}
)
[1] => Array
(
[0] => y
[1] => m
)
[2] => Array
(
[0] =>
[1] => +
)
[3] => Array
(
[0] =>
[1] => 1
)
)

Related

PHP how to unset a sub-array by key from a sub-array within a multi dimensional parent array?

I have this array here that contains a group of duplicate ids each id is set as as array key and the sub array contains a list of ids that also has another array within the same array?
DUPLICATES 16
array(16) {
[19503804]=>
array(3) {
[0]=>
string(8) "19501594"
[1]=>
string(8) "15539642"
[2]=>
string(8) "19498944"
}
[19501594]=>
array(3) {
[0]=>
string(8) "19503804"
[1]=>
string(8) "15539642"
[2]=>
string(8) "19498944"
}
[19837033]=>
array(6) {
[0]=>
string(8) "19854557"
[1]=>
string(8) "19854558"
[2]=>
string(8) "19854553"
[3]=>
string(8) "19854565"
[4]=>
string(8) "19854554"
[5]=>
string(8) "19854683"
}
[19854553]=>
array(6) {
[0]=>
string(8) "19854557"
[1]=>
string(8) "19854558"
[2]=>
string(8) "19837033"
[3]=>
string(8) "19854565"
[4]=>
string(8) "19854554"
[5]=>
string(8) "19854683"
}
[19544216]=>
array(3) {
[0]=>
string(8) "19524884"
[1]=>
string(8) "19560234"
[2]=>
string(8) "19540264"
}
[19854565]=>
array(6) {
[0]=>
string(8) "19854557"
[1]=>
string(8) "19854558"
[2]=>
string(8) "19837033"
[3]=>
string(8) "19854553"
[4]=>
string(8) "19854554"
[5]=>
string(8) "19854683"
}
[19854554]=>
array(6) {
[0]=>
string(8) "19854557"
[1]=>
string(8) "19854558"
[2]=>
string(8) "19837033"
[3]=>
string(8) "19854553"
[4]=>
string(8) "19854565"
[5]=>
string(8) "19854683"
}
[15539642]=>
array(3) {
[0]=>
string(8) "19503804"
[1]=>
string(8) "19501594"
[2]=>
string(8) "19498944"
}
[19844271]=>
array(1) {
[0]=>
string(8) "19341140"
}
[19498944]=>
array(3) {
[0]=>
string(8) "19503804"
[1]=>
string(8) "19501594"
[2]=>
string(8) "15539642"
}
[16399898]=>
array(1) {
[0]=>
string(8) "15436391"
}
[15436391]=>
array(1) {
[0]=>
string(8) "16399898"
}
[19341140]=>
array(1) {
[0]=>
string(8) "19844271"
}
[19560234]=>
array(3) {
[0]=>
string(8) "19544216"
[1]=>
string(8) "19524884"
[2]=>
string(8) "19540264"
}
[19854683]=>
array(6) {
[0]=>
string(8) "19854557"
[1]=>
string(8) "19854558"
[2]=>
string(8) "19837033"
[3]=>
string(8) "19854553"
[4]=>
string(8) "19854565"
[5]=>
string(8) "19854554"
}
[19540264]=>
array(3) {
[0]=>
string(8) "19544216"
[1]=>
string(8) "19524884"
[2]=>
string(8) "19560234"
}
}
notice first array?
[19503804]=>
array(3) {
[0]=>
string(8) "19501594"
[1]=>
string(8) "15539642"
[2]=>
string(8) "19498944"
}
the "19501594" "15539642" "19498944" also has another array which need to be unset.so at the end in this array only the unique arrays with unique values are given.
and this is the expected output required.
array(5) {
[19503804]=>
array(3) {
[0]=>
string(8) "19501594"
[1]=>
string(8) "15539642"
[2]=>
string(8) "19498944"
}
[19837033]=>
array(6) {
[0]=>
string(8) "19854557"
[1]=>
string(8) "19854558"
[2]=>
string(8) "19854553"
[3]=>
string(8) "19854565"
[4]=>
string(8) "19854554"
[5]=>
string(8) "19854683"
}
[19544216]=>
array(3) {
[0]=>
string(8) "19524884"
[1]=>
string(8) "19560234"
[2]=>
string(8) "19540264"
}
[19844271]=>
array(1) {
[0]=>
string(8) "19341140"
}
[16399898]=>
array(1) {
[0]=>
string(8) "15436391"
}
}
This is what i had initially coded as proposed by #Dale in the answer below.
$theduplicates = array (
19503804 =>
array (
0 => '19501594',
1 => '15539642',
2 => '19498944',
),
19501594 =>
array (
0 => '19503804',
1 => '15539642',
2 => '19498944',
),
19837033 =>
array (
0 => '19854557',
1 => '19854558',
2 => '19854553',
3 => '19854565',
4 => '19854554',
5 => '19854683',
),
19854553 =>
array (
0 => '19854557',
1 => '19854558',
2 => '19837033',
3 => '19854565',
4 => '19854554',
5 => '19854683',
),
19544216 =>
array (
0 => '19524884',
1 => '19560234',
2 => '19540264',
),
19854565 =>
array (
0 => '19854557',
1 => '19854558',
2 => '19837033',
3 => '19854553',
4 => '19854554',
5 => '19854683',
),
19854554 =>
array (
0 => '19854557',
1 => '19854558',
2 => '19837033',
3 => '19854553',
4 => '19854565',
5 => '19854683',
),
15539642 =>
array (
0 => '19503804',
1 => '19501594',
2 => '19498944',
),
19844271 =>
array (
0 => '19341140',
),
19498944 =>
array (
0 => '19503804',
1 => '19501594',
2 => '15539642',
),
16399898 =>
array (
0 => '15436391',
),
15436391 =>
array (
0 => '16399898',
),
19341140 =>
array (
0 => '19844271',
),
19560234 =>
array (
0 => '19544216',
1 => '19524884',
2 => '19540264',
),
19854683 =>
array (
0 => '19854557',
1 => '19854558',
2 => '19837033',
3 => '19854553',
4 => '19854565',
5 => '19854554',
),
19540264 =>
array (
0 => '19544216',
1 => '19524884',
2 => '19560234',
),
)
foreach($theduplicates as $k => $v){
foreach($v as $d){
if(isset($theduplicates[$d])){
unset($theduplicates[$d]);
}
}
// but it totally cleans theduplicates array
array(0) {
}
For every iteration you want to check they keys that have already been iterated. Therefore you want to keep track of these keys. I would do something like this:
$keys = [];
foreach ($main_array as $k => $set) {
if (in_array($k, $keys)) {
unset($main_array[$k]);
continue;
}
foreach ($set as $val) {
$keys[] = $val;
}
}
var_dump($main_array);
This code will also work, but you have to pass by reference:
foreach ($main_array as &$values) {
foreach ($values as $value) {
if(isset($main_array[$value])) {
unset($main_array[$value]);
}
}
}
If I understand your question (of which I am doubtful) this code should get you on your way
$array = [
0 => [5, 6, 7],
1 => [10],
5 => [10], // to remove
6 => [10], // to remove
7 => [10] // to remove
];
print_r($array);
foreach ($array as $key => $values) {
foreach ($values as $value) {
if(isset($array[$value])) {
unset($array[$value]);
}
}
}
print_r($array);
output..
Array
(
[0] => Array
(
[0] => 5
[1] => 6
[2] => 7
)
[1] => Array
(
[0] => 10
)
[5] => Array
(
[0] => 10
)
[6] => Array
(
[0] => 10
)
[7] => Array
(
[0] => 10
)
)
Array
(
[0] => Array
(
[0] => 5
[1] => 6
[2] => 7
)
[1] => Array
(
[0] => 10
)
)

Create array from array values

I have an array like this :
$lessonOptions=array();
$lessonOptions[0]=array("Physics","6.00","2015-01-01","-","4");
$lessonOptions[1]=array("Physics","16.00","2015-01-01","-","2");
$lessonOptions[2]=array("Maths","10.00","2015-07-01","-","10");
$lessonOptions[3]=array("Maths","20.00","2015-07-01","-","10");
I want to create an output with new array called optionarray which contains:
**
physics 6.00 2015-01-04 -
physics 16.00 2015-01-01 -
maths 10.00 2015-07-01 -
maths 20.00 2015-07-07 -
My problem is that I can see every line of maths but only one line of physics is missing .
How can I display all line ?
My actual code is :
$lessonGroup=$lessonOptions[0][0];
$display=FALSE;
$state=0;
$nbLessons=count($lessonsOptions);
for ($i = 0; $i<$nbLessons; $i++)
{
if ($i+1!=$nbLessons) $lessonGroupSuivant=$lessonOptions[$i+1][0];
else $display=TRUE;
switch ($state)
{
case 0 :
$infoLesson[$state]=$lessonOptions[$i];
default :
{
if ($lessonGroup==$lessonGroupSuivant)
{
$infoLesson[$state]=$lessonOptions[$i];
$state=$state+1;
}
else
{
$display=TRUE;
$lessonGroup=$lessonGroupSuivant;
$state=0;
}
}
}
if ($display==TRUE)
{
//var_dump($infoLesson);
$display=FALSE;
}
}
My actual array (wrong)
As you can see, the size of my array is 1 , I need to have 2 because I have two field for the lesson physics.
array(1) { [0]=> array(5) { [0]=> string(31) "physics" [1]=> string(10) "6.00" [2]=> string(10) "2015-01-01" [3]=> string(1) "-" [4]=> float(42) } }
array(2) { [0]=> array(5) { [0]=> string(15) "Maths" [1]=> string(11) "10.00" [2]=> string(10) "2015-07-01" [3]=> string(1) "-" [4]=> float(10) } [1]=> array(5) { [0]=> string(15) "Maths" [1]=> string(11) "10.00" [2]=> string(10) "2015-07-12" [3]=> string(1) "-" [4]=> float(10) } }
Okay i think i found what you need:
<?php
$lessonOptions=array();
$lessonOptions[0]=array("Physics","6.00","2015-01-01","-","4");
$lessonOptions[1]=array("Physics","16.00","2015-01-01","-","2");
$lessonOptions[2]=array("Maths","10.00","2015-07-01","-","10");
$lessonOptions[3]=array("Maths","20.00","2015-07-01","-","10");
$optionarray = array();
$lastLessonKey = '';
$i = 0;
foreach ($lessonOptions as $key => $data) {
if (empty($lastLessonKey)) {
$lastLessonKey = $data[0];
} else if ($lastLessonKey !== $data[0]) {
$i++;
$lastLessonKey = $data[0];
}
$optionarray[$i][] = $data;
}
var_dump($optionarray);
Output:
array(2) { [0]=> array(2) { [0]=> array(5) { [0]=> string(7) "Physics" [1]=> string(4) "6.00" [2]=> string(10) "2015-01-01" [3]=> string(1) "-" [4]=> string(1) "4" } [1]=> array(5) { [0]=> string(7) "Physics" [1]=> string(5) "16.00" [2]=> string(10) "2015-01-01" [3]=> string(1) "-" [4]=> string(1) "2" } } [1]=> array(2) { [0]=> array(5) { [0]=> string(5) "Maths" [1]=> string(5) "10.00" [2]=> string(10) "2015-07-01" [3]=> string(1) "-" [4]=> string(2) "10" } [1]=> array(5) { [0]=> string(5) "Maths" [1]=> string(5) "20.00" [2]=> string(10) "2015-07-01" [3]=> string(1) "-" [4]=> string(2) "10" } } }
So, you are willing to group arrays by some value
$infoLesson = array();
foreach ($lessonOptions as $v) {
$infoLesson[$v[0]][] = $v;
}
You can then retrieve arrays by key.
For example printing $infoLesson['Maths'] will get you
Array
(
[0] => Array
(
[0] => Maths
[1] => 10.00
[2] => 2015-07-01
[3] => -
[4] => 10
)
[1] => Array
(
[0] => Maths
[1] => 20.00
[2] => 2015-07-01
[3] => -
[4] => 10
)
)
And $infoLesson['Physics']
Array
(
[0] => Array
(
[0] => Physics
[1] => 6.00
[2] => 2015-01-01
[3] => -
[4] => 4
)
[1] => Array
(
[0] => Physics
[1] => 16.00
[2] => 2015-01-01
[3] => -
[4] => 2
)
)

How to sort the array by the value of given key in my case?

I need to sort the array by the value of the given key 0 in this case.
So here is my array:
array(11) {
[0]=> array(3) {
[0]=> string(3) "5"
[1]=> string(1) "3"
[2]=> string(2) "21"
}
[1]=> array(3) {
[0]=> string(3) "0.5"
[1]=> string(1) "3"
[2]=> string(3) "may"
}
[2]=> array(3) {
[0]=> string(3) "2.2"
[1]=> string(1) "3"
[2]=> string(16) "sport"
}
}
Result must be sorted by the value of key 0:
array(11) {
[0]=> array(3) {
[0]=> string(3) "0.5"
[1]=> string(1) "3"
[2]=> string(2) "may"
}
[1]=> array(3) {
[0]=> string(3) "2.2"
[1]=> string(1) "3"
[2]=> string(3) "sport"
}
[2]=> array(3) {
[0]=> string(3) "5"
[1]=> string(1) "3"
[2]=> string(16) "21"
}
}
I attempted it with this code:
function sort_by_second($i,$j){return $i[0]-$j[0];};
usort($mas,'sort_by_second');
I don't understand why it doesn't work.
Try below code:
$array = array(
array("5","3","21"),
array("0.5","3","may"),
array("2.2","3","sport"),
);
//print_r($array);
function subval_sort($a,$subkey) {
foreach($a as $k=>$v) {
$b[$k] = strtolower($v[$subkey]);
}
asort($b);
foreach($b as $key=>$val) {
$c[] = $a[$key];
}
return $c;
}
$result = subval_sort($array, 0);
print_r($result);
Result:
Array
(
[0] => Array
(
[0] => 0.5
[1] => 3
[2] => may
)
[1] => Array
(
[0] => 2.2
[1] => 3
[2] => sport
)
[2] => Array
(
[0] => 5
[1] => 3
[2] => 21
)
)
Demo:
http://3v4l.org/q4ZtL#v430
PHP >= 5.5.0:
array_multisort(array_column($array, 0), SORT_ASC, $array);

How to sort 3D arrays in php?

How do I sort 3D arrays ? In this case I want to sort after date. Thx ?
array(2) {
["garsoniere"]=>array(2) {
[0]=> array(5) {
["date"]=> string(19) "2014-02-04"
["sponsored"]=> string(1) "0"
["offer_status"]=> string(6) "active"
["suprafata"]=> string(3) "111"
["confort"]=> string(2) "-1"
["title"]=> string(18) "Jimmy Humununukaua"
}
[1]=> array(5) {
["date"]=> string(19) "2013-03-25"
["sponsored"]=> string(1) "0"
["offer_status"]=> NUL
["suprafata"]=> string(2) "23"
["confort"]=> NULL
["title"]=> string(38) "Garsoniera de vanzare in Marasti, Cluj"
}
}
["apartamente"]=> array(2) {
[0]=> array(5) {
["date"]=> string(19) "2014-02-05"
["sponsored"]=> string(1) "0"
["offer_status"]=> string(6) "active"
["etaj"]=> string(2) "50"
["title"]=> string(15) "Test Apartament"
}
[1]=> array(5) {
["date"]=> string(19) "2014-02-04"
["sponsored"]=> string(1) "0"
["offer_status"]=> string(6) "active"
["etaj"]=> string(2) "50"
["title"]=> string(13) "dfasfsdffasdf"
}
}
}
I know it looks wrong but I want that the result should look like this:
["apartamente"][0][date]=>2014-02-05.....[title]
["apartamente"][1][date]=>2014-02-04.....[title]
["garsoniere"][0][date]=>2014-02-04.....[title]
["garsoniere"][1][date]=>2013-03-25.....[title]
Can anyone help me with this ?
Please use usort function to achieve this. Please have a look at http://badarwaqas.blogspot.com/2013/04/php-sorting-array-on-nth-level-key.html article
Use usort
Inital array
Array
(
[giraffe] => Array
(
[date] => 2014-02-04
)
[zebra] => Array
(
[date] => 2012-02-04
)
[penguin] => Array
(
[date] => 2014-01-04
)
)
The sorting algorithm
usort($arr, function($a, $b) {
return ($a['date'] > $b['date'] ? 1 : ($a['date'] < $b['date'] ? -1 : 0));
});
Which gives - after sorting
Array
(
[0] => Array
(
[date] => 2012-02-04
)
[1] => Array
(
[date] => 2014-01-04
)
[2] => Array
(
[date] => 2014-02-04
)
)
Code grabbed from here
Try
$result = array();
foreach($arr as $key=>$val){
foreach($val as $key1=>$val1){
$result[$key][$key1]= $val1;
}
}
ksort($result,SORT_NATURAL);
I am not sure about the sort you have specified in your question.
See demo here

php multidimensional array: how to remove duplicate entries

in fear of duplicating content, i have looked through so many similar SO questions, but i think i need a bit more than code, to tell me how solve my problem- would be lovely with some explaination too.
How do i turn $list:
array(4) {
[0]=>
array(2) {
["title"]=>
string(8) "Zambezia"
["id"]=>
int(31)
}
[1]=>
array(2) {
["title"]=>
string(6) "Zarafa"
["id"]=>
int(34)
}
[2]=>
array(2) {
["title"]=>
string(8) "Zambezia"
["id"]=>
int(31)
}
[3]=>
array(2) {
["title"]=>
string(8) "Zambezia"
["id"]=>
int(31)
}
}
Into $list:
array(2) {
[0]=>
array(2) {
["title"]=>
string(8) "Zambezia"
["id"]=>
int(31)
}
[1]=>
array(2) {
["title"]=>
string(6) "Zarafa"
["id"]=>
int(34)
}
}
By removing duplicate entries?
Use array_unique() with SORT_REGULAR flag.
$new_array = array_unique($array, SORT_REGULAR);
Output should be:
Array
(
[0] => Array
(
[title] => Zambezia
[id] => 31
)
[1] => Array
(
[title] => Zarafa
[id] => 34
)
)
Demo.

Categories