PHP preg-match-all regex - php

I would like to capture each of these in their own group with preg_match_all in PHP.
The chapter, section, or page
The number (or letter if it has one) of the specified chapter, section, or page. If there is a single space between them it should be taken into account
The words "and", "or"
Keeping in mind that the number of items in the string may be dynamic, the regex should work on all the examples below:
Ch1 and Sect2b
Ch 4 x blahunwantedtext and Sect 5y and Sect6 z and Ch7 or Ch8
This is what I managed to come up with so far:
<?php
$str = 'Ch 1 a and Sect 2b and Pg3';
preg_match_all ('/([a-z]+)([\s]?[0-9]+)([\s]?[a-z]*)([\s]?and*[\s]?)/is', $str, $matches);
Array
(
[0] => Array
(
[0] => Ch 1 a and
[1] => Sect 2b and
)
[1] => Array
(
[0] => Ch
[1] => Sect
)
[2] => Array
(
[0] => 1
[1] => 2
)
[3] => Array
(
[0] => a
[1] => b
)
[4] => Array
(
[0] => and
[1] => and
)
)
I'm unable to match the last portions of the string (Pg3) in my array.
The expected result should be:
Array
(
[0] => Array
(
[0] => Ch 1 a and
[1] => Sect 2b and
[2] => Pg3
)
[1] => Array
(
[0] => Ch
[1] => Sect
[2] => Pg
)
[2] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[3] => Array
(
[0] => a
[1] => b
[2] =>
)
[4] => Array
(
[0] => and
[1] => and
[2] =>
)
)

This regex should work /(ch|sect|pg)\s*(\d)\s*([a-z]?\b)\s*(and|or)?/i:
$str = 'Ch 1 a and Sect 2b and Pg3';
preg_match_all('/(ch|sect|pg)\s*(\d)\s*([a-z]?\b)\s*(and|or)?/i', $str, $matches);
array (size=5)
0 =>
array (size=3)
0 => string 'Ch 1 a and' (length=10)
1 => string 'Sect 2b and' (length=11)
2 => string 'Pg3' (length=3)
1 =>
array (size=3)
0 => string 'Ch' (length=2)
1 => string 'Sect' (length=4)
2 => string 'Pg' (length=2)
2 =>
array (size=3)
0 => string '1' (length=1)
1 => string '2' (length=1)
2 => string '3' (length=1)
3 =>
array (size=3)
0 => string 'a' (length=1)
1 => string 'b' (length=1)
2 => string '' (length=0)
4 =>
array (size=3)
0 => string 'and' (length=3)
1 => string 'and' (length=3)
2 => string '' (length=0)

Related

Merging two array with same values in php

I have two arrays.
Array 1
Array
(
[1] => 111,
[id1] => 1,
[2] => 11231,
[id2] => 2,
[3] => 12311,
[id3] => 3,
[4] => 11981,
[id4] => 4,
[5] => 11761,
[id5] => 5,
[6] => 11561,
[id6] => 6
)
Array 2
Array
(
[1] => 2,
[id1] => 1,
[2] => 2,
[id2] => 2,
[3] => 3,
[id3] => 3,
[4] => 4,
[id4] => 4,
[5] => 4,
[id5] => 5,
[6] => 6,
[id6] => 6
)
Id key is user id in both arrays
And numerical key in second array is manager id
I want to merge these two array in below format.
Merge array
Array
(
[2] => Array
(
[0] => 111
[1] => 1
),
Array
(
[0] => 11231
[1] => 2
),
[3] => Array
(
[0] => 12311
[1] => 3
),
[4] => Array
(
[0] => 11981
[1] => 4
),
Array
(
[0] => 11761
[1] => 5
),
[6] => Array
(
[0] => 11561
[1] => 6
)
)
Array inside array is the value of first array.
2,3,4,6 key is values from second array.
Those users who have same manager id will be merge in single array.
Array Walk Apply a user supplied function to every member of an array
array_walk($array2, function($value,$key) use($array1,&$result){
if(is_integer($key))
$result[$value][]=[$array1[$key],$array1['id'.$key]];
});
output
array (size=4)
2 =>
array (size=2)
0 =>
array (size=2)
0 => int 111
1 => int 1
1 =>
array (size=2)
0 => int 11231
1 => int 2
3 =>
array (size=1)
0 =>
array (size=2)
0 => int 12311
1 => int 3
4 =>
array (size=2)
0 =>
array (size=2)
0 => int 11981
1 => int 4
1 =>
array (size=2)
0 => int 11761
1 => int 5
6 =>
array (size=1)
0 =>
array (size=2)
0 => int 11561
1 => int 6

Regex causes duplicates, why?

Im trying to catch all urls inside a #font-face but $matches creates duplicates, which it shouldn't. I really can't figure out at all why this happens. I created the regex with https://regex101.com/ and it works well but it does not work when I put it in a php document myself. Any help is really appreciated!
My php version is 7.1 if that helps!
<?php
$re = '/url\s*\(\s*[\'"]?(?!(((?:https?:)?\/\/)|(?:data\:?:)))([^\'"\)]+)[\'"]?\s*\)/';
$str = '#font-face{font-family:Poiret One;font-style:normal;font-weight:400;src:local(\'PoiretOne-Regular\'),url(/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff2) format(\'woff2\'),url(/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff) format(\'woff\'),url(/wp-content/themes/Divi-Child/fonts/PoiretOne-Regular.ttf) format(\'truetype\')}#font-face{font-family:Open sans;font-style:normal;font-weight:400;src:local(\'OpenSans-Regular\'),url(/wp-content/themes/Divi-Child/fonts/opensans-regular-webfont_webfont.woff2) format(\'woff2\'),url(/wp-content/themes/Divi-Child/fonts/opensans-regular-webfont_webfont.woff) format(\'woff\'),url(/wp-content/themes/Divi-Child/fonts/OpenSans-Regular.ttf) format(\'truetype\')}#font-face{font-family:Open sans;font-weight:300;src:local(\'OpenSans-Light\'),url(/wp-content/themes/Divi-Child/fonts/opensans-light-webfont_webfont.woff2) format(\'woff2\'),url(/wp-content/themes/Divi-Child/fonts/opensans-light-webfont_webfont.woff) format(\'woff\'),url(/wp-content/themes/Divi-Child/fonts/OpenSans-Light.ttf) format(\'truetype\')}#font-face{font-family:Dancing Script;font-weight:400;font-style:normal;src:local(\'dancing-script_regular\'),url(/wp-content/themes/Divi-Child/fonts/dancingscript-regular_webfont.woff2) format(\'woff2\'),url(/wp-content/themes/Divi-Child/fonts/dancingscript-regular_webfont.woff) format(\'woff\'),url(/wp-content/themes/Divi-Child/fonts/dancing-script_regular_webfont.ttf) format(\'truetype\')}#font-face{font-family:Josefin Slab;font-weight:400;font-style:normal;src:local(\'JosefinSlab-Regular\'),url(/wp-content/themes/Divi-Child/fonts/josefinslab-regular_webfont.woff2) format(\'woff2\'),url(/wp-content/themes/Divi-Child/fonts/josefinslab-regular_webfont.woff) format(\'woff\'),url(/wp-content/themes/Divi-Child/fonts/JosefinSlab-Regular.ttf) format(\'truetype\')}#font-face{font-family:Josefin Slab;font-weight:400;font-style:italic;src:local(\'JosefinSlab-Italic\'),url(/wp-content/themes/Divi-Child/fonts/josefinslab-italic_webfont.woff2) format(\'woff2\'),url(/wp-content/themes/Divi-Child/fonts/josefinslab-italic_webfont.woff) format(\'woff\'),url(/wp-content/themes/Divi-Child/fonts/JosefinSlab-Italic.ttf) format(\'truetype\')}';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
print_r($matches);
If you ctrl+f and write divi-child you can see that it's stated 18 times in the $str but $matches is 36 times:
Array ( [0] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff2) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff2 ) [1] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff ) [2] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/PoiretOne-Regular.ttf) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/PoiretOne-Regular.ttf ) [3] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/opensans-regular-webfont_webfont.woff2) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/opensans-regular-webfont_webfont.woff2 ) [4] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/opensans-regular-webfont_webfont.woff) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/opensans-regular-webfont_webfont.woff ) [5] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/OpenSans-Regular.ttf) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/OpenSans-Regular.ttf ) [6] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/opensans-light-webfont_webfont.woff2) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/opensans-light-webfont_webfont.woff2 ) [7] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/opensans-light-webfont_webfont.woff) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/opensans-light-webfont_webfont.woff ) [8] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/OpenSans-Light.ttf) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/OpenSans-Light.ttf ) [9] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/dancingscript-regular_webfont.woff2) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/dancingscript-regular_webfont.woff2 ) [10] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/dancingscript-regular_webfont.woff) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/dancingscript-regular_webfont.woff ) [11] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/dancing-script_regular_webfont.ttf) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/dancing-script_regular_webfont.ttf ) [12] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/josefinslab-regular_webfont.woff2) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/josefinslab-regular_webfont.woff2 ) [13] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/josefinslab-regular_webfont.woff) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/josefinslab-regular_webfont.woff ) [14] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/JosefinSlab-Regular.ttf) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/JosefinSlab-Regular.ttf ) [15] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/josefinslab-italic_webfont.woff2) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/josefinslab-italic_webfont.woff2 ) [16] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/josefinslab-italic_webfont.woff) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/josefinslab-italic_webfont.woff ) [17] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/JosefinSlab-Italic.ttf) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/JosefinSlab-Italic.ttf ) )
I just ran your code.
I got an array of 18 elements.
The reason your Ctrl+F is returning two instances of "divi-child" is because it is present in the full contents match, and the smaller match you specify with brackets.
array (size=18)
0 =>
array (size=4)
0 => string 'url(/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff2)' (length=72)
1 => string '' (length=0)
2 => string '' (length=0)
3 => string '/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff2' (length=67)
1 =>
array (size=4)
0 => string 'url(/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff)' (length=71)
1 => string '' (length=0)
2 => string '' (length=0)
3 => string '/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff' (length=66)
<snip>
Also, when debugging, var_dump() is a better function - gives you more information about the variable contents.
Suggested Solution for User
(Rather than trying to squeeze this into comments...)
Change the Regular Expression to
$re = '/url\s*\(\s*[\'"]?(?!(?:(?:https?:)?\/\/)|(?:data\:?:))([^\'"\)]+)[\'"]?\s*\)/';
Change the parameters sent to the preg_match_all() function to
preg_match_all($re, $str, $matches);
The returned value will be an array with two elements (each arrays in themselves). The first element will be an array of the complete matches to your regular expression. The second element will the the portion of the match which we are capturing with the brackets.
array (size=2)
0 =>
array (size=18)
0 => string 'url(/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff2)' (length=72)
1 => string 'url(/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff)' (length=71)
2 => string 'url(/wp-content/themes/Divi-Child/fonts/PoiretOne-Regular.ttf)' (length=62)
3 => string 'url(/wp-content/themes/Divi-Child/fonts/opensans-regular-webfont_webfont.woff2)' (length=79)
<snip>
1 =>
array (size=18)
0 => string '/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff2' (length=67)
1 => string '/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff' (length=66)
2 => string '/wp-content/themes/Divi-Child/fonts/PoiretOne-Regular.ttf' (length=57)
3 => string '/wp-content/themes/Divi-Child/fonts/opensans-regular-webfont_webfont.woff2' (length=74)
<snip>

Update a multidimensional array

I'm trying to update my array but it doesn't work as expected!
I got this array:
Array
(
[0] => Array
(
[0] => property_unit Object
(
[unit] => one bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 3
[size] => 54
[percentage] => 0
[search] => 1
)
)
)
[1] => property_unit Object
(
[unit] => two bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 1
[size] => 74
[percentage] => 0
[search] => 1
)
)
)
[2] => property_unit Object
(
[unit] => three bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 1
[size] => 90
[percentage] => 0
[search] => 1
)
)
)
[3] => property_unit Object
(
[unit] => studio
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 2
[size] => 22.35
[percentage] => 0
[search] => 1
)
)
)
)
[1] => Array
(
[0] => property_unit Object
(
[unit] => one bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 3
[size] => 54
[percentage] => 0
[search] => 1
)
)
)
[1] => property_unit Object
(
[unit] => two bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 1
[size] => 74
[percentage] => 0
[search] => 1
)
)
)
[2] => property_unit Object
(
[unit] => studio
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 2
[size] => 22.35
[percentage] => 0
[search] => 1
)
)
)
[3] => property_unit Object
(
[unit] => three bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 1
[size] => 90
[percentage] => 0
[search] => 1
)
)
)
)
[2] => Array
(
[0] => property_unit Object
(
[unit] => one bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 3
[size] => 54
[percentage] => 0
[search] => 1
)
)
)
[1] => property_unit Object
(
[unit] => three bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 1
[size] => 90
[percentage] => 0
[search] => 1
)
)
)
[2] => property_unit Object
(
[unit] => two bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 1
[size] => 74
[percentage] => 0
[search] => 1
)
)
)
[3] => property_unit Object
(
[unit] => studio
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 2
[size] => 22.35
[percentage] => 0
[search] => 1
)
)
)
)
)
and using var_dump()
array (size=24)
0 =>
array (size=4)
0 =>
object(property_unit)[1]
public 'unit' => string 'one bedroom' (length=11)
public 'details' =>
array (size=1)
...
1 =>
object(property_unit)[3]
public 'unit' => string 'two bedroom' (length=11)
public 'details' =>
array (size=1)
...
2 =>
object(property_unit)[5]
public 'unit' => string 'three bedroom' (length=13)
public 'details' =>
array (size=1)
...
3 =>
object(property_unit)[7]
public 'unit' => string 'studio' (length=6)
public 'details' =>
array (size=1)
...
1 =>
array (size=4)
0 =>
object(property_unit)[1]
public 'unit' => string 'one bedroom' (length=11)
public 'details' =>
array (size=1)
...
1 =>
object(property_unit)[3]
public 'unit' => string 'two bedroom' (length=11)
public 'details' =>
array (size=1)
...
2 =>
object(property_unit)[7]
public 'unit' => string 'studio' (length=6)
public 'details' =>
array (size=1)
...
3 =>
object(property_unit)[5]
public 'unit' => string 'three bedroom' (length=13)
public 'details' =>
array (size=1)
...
2 =>
array (size=4)
0 =>
object(property_unit)[1]
public 'unit' => string 'one bedroom' (length=11)
public 'details' =>
array (size=1)
...
1 =>
object(property_unit)[5]
public 'unit' => string 'three bedroom' (length=13)
public 'details' =>
array (size=1)
...
2 =>
object(property_unit)[3]
public 'unit' => string 'two bedroom' (length=11)
public 'details' =>
array (size=1)
...
3 =>
object(property_unit)[7]
public 'unit' => string 'studio' (length=6)
public 'details' =>
array (size=1)
...
3 =>
I got this array with this:
$array = [new property_unit('one bedroom', [new propertydetails(1, 54, 0, 1)]),
new property_unit('two bedroom', [new propertydetails(1, 74, 0, 1)]),
new property_unit('three bedroom', [new propertydetails(1, 90, 0, 1)]),
new property_unit('studio', [new propertydetails(2, 22.35, 0, 1)])];
Then I perform a computation on it to get all the possible array.
function computePermutations($array) {
$result = [];
$recurse = function($array, $start_i = 0) use (&$result, &$recurse) {
if ($start_i === count($array)-1) {
array_push($result, $array);
}
for ($i = $start_i; $i < count($array); $i++) {
//Swap array value at $i and $start_i
$t = $array[$i];
$array[$i] = $array[$start_i];
$array[$start_i] = $t;
//Recurse
$recurse($array, $start_i + 1);
//Restore old order
$t = $array[$i];
$array[$i] = $array[$start_i];
$array[$start_i] = $t;
}
};
$recurse($array);
return $result;
}
//I assign to my array
$array = computePermutations($array);
I thing something maybe goes wrong here ?
but when I'm trying to update my array like :
$array[0][0]->details[0]->priority = 3;
It update all the first priority field, (see the array before) this is after the update.
Meaning:
$array[0][0]->details[0]->priority
$array[1][0]->details[0]->priority
$array[2][0]->details[0]->priority
etc... becomes 3
Any idea where this can come from, is there any other way to update a multidimensional array? Am I doing something wrong here?

Combining array of array with another array in PHP using array_combine

I'm playing around with array functions to get a better understanding.
Below I have three arrays:
$cardNumber = array(2, 4, 5, 8, 9);
$playerName = array('Julian', 'Brad', 'Chloe', 'Laura', 'Paul');
$playerWins = array(4, 5, 1, 2, 6);
I am trying to achieve the following array structure:
array (size=5)
2 =>
array (size=2)
0 => string 'Julian' (length=6)
1 => int 4
4 =>
array (size=2)
0 => string 'Brad' (length=4)
1 => int 5
5 =>
array (size=2)
0 => string 'Chloe' (length=5)
1 => int 1
8 =>
array (size=2)
0 => string 'Laura' (length=5)
1 => int 2
9 =>
array (size=2)
0 => string 'Paul' (length=4)
1 => int 6
I decided to combine $playerName and $playerWins with array_combine(), and then combine the resulting array with $cardNumber, which does not yield the output I expect. Am I understanding array_combine() incorrectly?
You can use array_map with null as a callback and the two data arrays to give you the array structure that you want. Then combine that with the array for the keys using array_combine:
$result = array_combine($cardNumber, array_map(null, $playerName, $playerWins));
For illustration:
$result = array_map(null, $playerName, $playerWins);
Yields:
Array
(
[0] => Array
(
[0] => Julian
[1] => 4
)
[1] => Array
(
[0] => Brad
[1] => 5
)
[2] => Array
(
[0] => Chloe
[1] => 1
)
[3] => Array
(
[0] => Laura
[1] => 2
)
[4] => Array
(
[0] => Paul
[1] => 6
)
)
Then combine to get the keys:
$result = array_combine($cardNumber, $result);
Yields:
Array
(
[2] => Array
(
[0] => Julian
[1] => 4
)
[4] => Array
(
[0] => Brad
[1] => 5
)
[5] => Array
(
[0] => Chloe
[1] => 1
)
[8] => Array
(
[0] => Laura
[1] => 2
)
[9] => Array
(
[0] => Paul
[1] => 6
)
)

PHP Adjacency Data to Nested Array

I am having array as follows:
Array
(
[0] => Array
(
[question_id] => 1
[question_title] => Q1
[child_question_id] => NULL
)
[1] => Array
(
[question_id] => 2
[question_title] => Q2
[child_question_id] => NULL
)
[2] => Array
(
[question_id] => 3
[question_title] => Q3
[child_question_id] => 4
)
[3] => Array
(
[question_id] => 3
[question_title] => Q3
[child_question_id] => 5
)
[4] => Array
(
[question_id] => 4
[question_title] => Q4
[child_question_id] => NULL
)
[5] => Array
(
[question_id] => 5
[question_title] => Q5
[child_question_id] => NULL
)
[6] => Array
(
[question_id] => 6
[question_title] => Q6
[child_question_id] => NULL
)
[7] => Array
(
[question_id] => 7
[question_title] => Q7
[child_question_id] => 6
)
)
And using PHP I am expecting result as:
Array
(
[0] => Array
(
[question_id] => 1
[question_title] => Q1
[child_question_id] => NULL
)
[1] => Array
(
[question_id] => 2
[question_title] => Q2
[child_question_id] => NULL
)
[2] => Array
(
[question_id] => 3
[question_title] => Q3
[children] => array(
[0] => Array
(
[question_id] => 4
[question_title] => Q4
)
[1] => Array
(
[question_id] => 5
[question_title] => Q5
)
)
)
[3] => Array
(
[question_id] => 7
[question_title] => Q7
[children] => array(
[0] => Array
(
[question_id] => 6
[question_title] => Q6
)
)
)
)
I tried to build final array using following logic:
Traversing array to collect child ids and child data
Traversing array again and match items child id with child array
formed earlier, if found then create children array
Could you please help me out to create better solution?
Did you mean something like that?
$i = 0;
$new = array();
$unsetter = array();
foreach($array as $val)
{
$new[$i] = array();
foreach($val as $key => $data)
{
if(($key == 'child_question_id') && (!is_null($data)))
{
$new[$i]['children'] = array();
$new[$i]['children']['question_id'] = $data;
$new[$i]['children']['question_title'] = $array[$data]['question_title'];
$unsetter[] = $data;
} else {
$new[$i][$key] = $data;
}
}
$i++;
}
foreach($unsetter as $uns)
{
unset($new[$uns]);
}
$new = array_values($new);
var_dump($new);
This will output
array (size=5)
0 =>
array (size=3)
'question_id' => int 1
'question_title' => string 'Q1' (length=2)
'child_question_id' => null
1 =>
array (size=3)
'question_id' => int 2
'question_title' => string 'Q2' (length=2)
'child_question_id' => null
2 =>
array (size=3)
'question_id' => int 3
'question_title' => string 'Q3' (length=2)
'children' =>
array (size=2)
'question_id' => int 4
'question_title' => string 'Q4' (length=2)
3 =>
array (size=3)
'question_id' => int 3
'question_title' => string 'Q3' (length=2)
'children' =>
array (size=2)
'question_id' => int 5
'question_title' => string 'Q5' (length=2)
4 =>
array (size=3)
'question_id' => int 7
'question_title' => string 'Q7' (length=2)
'children' =>
array (size=2)
'question_id' => int 6
'question_title' => string 'Q6' (length=2)
Live Demo

Categories