creating a multidimensional array from queries - php

I'm having difficulty constructing a multidimensional array based on queries fetched from a mysql database. The purpose is to prepare the result for json encoding. Having problems here.
Structure I’m aiming for:
Array (
[68] => Array (
[0] => Array (
[id] => 64
[description] => yada, yada, yada... )
[1] => Array (
[id] => 65
[description] => yada, yada, yada...)
[2] => Array (
[id] => 66
[description] => yada, yada, yada... ) )
[69] => Array (
[0] => Array (
[id] => 64
[description] => yada, yada, yada... )
[1] => Array (
[id] => 65
[description] => yada, yada, yada...)
[2] => Array (
[id] => 66
[description] => yada, yada, yada... ) )
...etc
)
From:
//this is a list of unknown length
$array_ids = (68, 69, 70, etc... ); // or:
Array (
[0] => 68
[1] => 69
[2] => 70
[3] => 71
etc..
)
//this is a known length
$array_contents = ( array ( array ( [id], [description]) ); // or:
Array (
[0] => Array (
[id] => 64
[description] =>yada, yada, yada... )
[1] => Array (
[id] => 65
[description] => yada, yada, yada... )
[2] => Array (
[id] => 66
[description] => yada, yada, yada... ) )
My attempt:
foreach($array_ids as $row){
$result = array($row=>array());
foreach($array_contents as $key => $value){
$result [$row][$key] = $value;
}
}
Result:
Array (
[68] => Array (
[0] => Array (
[id] => 64
[description] => yada, yada, yada... )
[1] => Array (
[id] => 65
[description] => yada, yada, yada...)
[2] => Array (
[id] => 66
[description] => yada, yada, yada... ) )
)
...and that’s where it stops. It doesn’t go on to the next row id of 69, 70, etc...
What am I doing wrong?

This line seems to be the problem:
$result = array($row=>array());
Every pass through $array_ids it's completely wiping out $result and repopulating it with one single item from $array_contents, rather than appending it to the end.
Replacing it with this should work:
$result[$row] = array();
Here's a full working version:
$array_ids = array(68,69,70);
$array_contents = array(
array(
'id' => 64,
'description' => 'yada, yada, yada...'
),
array(
'id' => 65,
'description' => 'yada, yada, yada2...'
),
array(
'id' => 66,
'description' => 'yada, yada, yada3...'
),
);
foreach($array_ids as $row){
$result[$row] = array();
foreach($array_contents as $key => $value){
$result [$row][$key] = $value;
}
}
var_dump($result);
?>
Here's the output:
array(3) {
[68]=>
array(3) {
[0]=>
array(2) {
["id"]=>
int(64)
["description"]=>
string(19) "yada, yada, yada..."
}
[1]=>
array(2) {
["id"]=>
int(65)
["description"]=>
string(20) "yada, yada, yada2..."
}
[2]=>
array(2) {
["id"]=>
int(66)
["description"]=>
string(20) "yada, yada, yada3..."
}
}
[69]=>
array(3) {
[0]=>
array(2) {
["id"]=>
int(64)
["description"]=>
string(19) "yada, yada, yada..."
}
[1]=>
array(2) {
["id"]=>
int(65)
["description"]=>
string(20) "yada, yada, yada2..."
}
[2]=>
array(2) {
["id"]=>
int(66)
["description"]=>
string(20) "yada, yada, yada3..."
}
}
[70]=>
array(3) {
[0]=>
array(2) {
["id"]=>
int(64)
["description"]=>
string(19) "yada, yada, yada..."
}
[1]=>
array(2) {
["id"]=>
int(65)
["description"]=>
string(20) "yada, yada, yada2..."
}
[2]=>
array(2) {
["id"]=>
int(66)
["description"]=>
string(20) "yada, yada, yada3..."
}
}
}

I tried with following array structure and logic its working , My logic is bit different
$array = array(68,69,70);
$nextArray = array(
array(
'id'=>64,
'desc'=>'test'
),
array(
'id'=>65,
'desc'=>'test'
),
array(
'id'=>66,
'desc'=>'test'
),
array(
'id'=>67,
'desc'=>'test'
),
);
$tempArray = array();
foreach($array as $value){
foreach($nextArray as $key => $value1){
$tempArray[$value][$key] = $value1;
}
}
var_dump($tempArray);
output :
array
68 =>
array
0 =>
array
'id' => int 64
'desc' => string 'test' (length=4)
1 =>
array
'id' => int 65
'desc' => string 'test' (length=4)
2 =>
array
'id' => int 66
'desc' => string 'test' (length=4)
3 =>
array
'id' => int 67
'desc' => string 'test' (length=4)
69 =>
array
0 =>
array
'id' => int 64
'desc' => string 'test' (length=4)
1 =>
array
'id' => int 65
'desc' => string 'test' (length=4)
2 =>
array
'id' => int 66
'desc' => string 'test' (length=4)
3 =>
array
'id' => int 67
'desc' => string 'test' (length=4)
70 =>
array
0 =>
array
'id' => int 64
'desc' => string 'test' (length=4)
1 =>
array
'id' => int 65
'desc' => string 'test' (length=4)
2 =>
array
'id' => int 66
'desc' => string 'test' (length=4)
3 =>
array
'id' => int 67
'desc' => string 'test' (length=4)

Try to change your code following way & shorter
$result = array();
foreach($array_ids as $row){
foreach($array_contents as $key => $value){
$result [$row][$key] = $value;
}
}

OK, having re-read the question, try the following:
$outputArray = array();
foreach( $array_ids as $id ){
$outputArray[$id] = $array_contents;
}

Related

How to merge array on behalf of a key value

I have an array
{
Array
(
[0] => Array
(
[filterType] => checkbox
[sfid] => a1d1I000000jrmwQAA
[name] => Publication Year
[specValues] => Array
(
[count] => 3
[value] => 1953
)
)
[1] => Array
(
[filterType] => checkbox
[sfid] => a1d1I000000jrmwQAA
[name] => Publication Year
[specValues] => Array
(
[count] => 1
[value] => 1954
)
)
)
}
But I want to merge array if sfid is same i.e i want result
{
Array
(
[0] => Array
(
[filterType] => checkbox
[sfid] => a1d1I000000jrmwQAA
[name] => Publication Year
[specValues] => Array(
[0] =>array(
[count] => 3
[value] => 1953
)
[1] =>array(
[count] => 1
[value] => 1954
)
)
)
)
}
You just need to iterate your array and add the row to the output if it is not in there already (you can use sfid as keys) and add specValues to the correct row.
<?php
$input = [
[
'filterType' => 'checkbox',
'sfid' => 'a1d1I000000jrmwQAA',
'name' => 'Publication Year',
'specValues' => [
'count' => 3,
'value' => 1953,
],
],
[
'filterType' => 'checkbox',
'sfid' => 'a1d1I000000jrmwQAA',
'name' => 'Publication Year',
'specValues' => [
'count' => 1,
'value' => 1954,
],
],
];
$data = [];
foreach ($input as $row) {
if (!isset($data[$row['sfid']])) {
$data[$row['sfid']] = [
'filterType' => $row['filterType'],
'sfid' => $row['sfid'],
'name' => $row['name'],
'specValues' => [],
];
}
$data[$row['sfid']]['specValues'][] = $row['specValues'];
}
var_dump(array_values($data));
Output:
array(1) {
[0]=>
array(4) {
["filterType"]=>
string(8) "checkbox"
["sfid"]=>
string(18) "a1d1I000000jrmwQAA"
["name"]=>
string(16) "Publication Year"
["specValues"]=>
array(2) {
[0]=>
array(2) {
["count"]=>
int(3)
["value"]=>
int(1953)
}
[1]=>
array(2) {
["count"]=>
int(1)
["value"]=>
int(1954)
}
}
}
}
I assume that if sfid is the same, then also filterType and name is the same.

PHP array_multisort not sorting with double multidimensional array as expected

Here's my code.
<?php
$data['test1'][0] = array('total' => 67, 'edition' => 2, 'pkg_version' => "2.5.0" );
$data['test1'][1] = array('total' => 67, 'edition' => 2, 'pkg_version' => "0.1.0" );
$data['test1'][2] = array('total' => 67, 'edition' => 2, 'pkg_version' => "0.3.0" );
$data['test2'][0] = array('total' => 86, 'edition' => 1, 'pkg_version' => "1.5.0");
$data['test2'][1] = array('total' => 85, 'edition' => 6, 'pkg_version' => "0.53.0");
$data['test2'][2] = array('total' => 98, 'edition' => 2, 'pkg_version' => "0.3");
$data['test2'][3] = array('total' => 98, 'edition' => 2, 'pkg_version' => "0.2");
$data['test3'][0] = array('total' => 60, 'edition' => 6, 'pkg_version' => "0.3");
$data['test3'][1] = array('total' => 60, 'edition' => 7, 'pkg_version' => "0.1.1");
$data['test3'][2] = array('total' => 60, 'edition' => 7, 'pkg_version' => "0.25");
foreach ($data as $row) {
foreach ($row as $k){
foreach ($k as $key => $value){
${$key}[] = $value;
}
}
}
array_multisort($pkg_version, SORT_DESC, $data);
echo "<pre>";
print_r($data);
echo "</pre>";
?>
I am trying to sort a multidimensional array using the array_multisort function
I would like to sort the pkg_version of each element to be ordered
The returned order is not as expected. Not sure, I have misunderstood how
array_multisort works? or my code is wrong. could you guys help me? I try to
solve this problem for a long time. it's quite a complex dimension array.
Here's the result after running the code above.
Array
(
[test1] => Array
(
[0] => Array
(
[total] => 67
[edition] => 2
[pkg_version] => 2.5.0
)
[1] => Array
(
[toal] => 67
[edition] => 2
[pkg_version] => 0.1.0
)
[2] => Array
(
[total] => 67
[edition] => 2
[pkg_version] => 0.3.0
)
)
[test2] => Array
(
[0] => Array
(
[total] => 86
[edition] => 1
[pkg_version] => 1.5.0
)
[1] => Array
(
[total] => 85
[type] => 2
[pkg_version] => 0.53.0
)
[2] => Array
(
[total] => 98
[type] => 2
[pkg_version] => 0.3
)
[3] => Array
(
[total] => 98
[edition] => 2
[pkg_version] => 0.2
)
)
[test3] => Array
(
[0] => Array
(
[total] => 60
[edition] => 6
[pkg_version] => 0.3
)
[1] => Array
(
[total] => 60
[edition] => 7
[pkg_version] => 0.1.1
)
[2] => Array
(
[total] => 60
[edition] => 7
[pkg_version] => 0.25
)
)
)
This is what I expected.
Array
(
[test1] => Array
(
[0] => Array
(
[total] => 67
[edition] => 2
[pkg_version] => 2.5.0
)
[1] => Array
(
[total] => 67
[edition] => 2
[pkg_version] => 0.3.0
)
[2] => Array
(
[toal] => 67
[edition] => 2
[pkg_version] => 0.1.0
)
)
[test2] => Array
(
[0] => Array
(
[total] => 86
[edition] => 1
[pkg_version] => 1.5.0
)
[1] => Array
(
[total] => 85
[type] => 2
[pkg_version] => 0.53.0
)
[2] => Array
(
[total] => 98
[type] => 2
[pkg_version] => 0.3
)
[3] => Array
(
[total] => 98
[edition] => 2
[pkg_version] => 0.2
)
)
[test3] => Array
(
[0] => Array
(
[total] => 60
[edition] => 6
[pkg_version] => 0.3
)
[1] => Array
(
[total] => 60
[edition] => 7
[pkg_version] => 0.25
)
[2] => Array
(
[total] => 60
[edition] => 7
[pkg_version] => 0.1.1
)
)
)
You can loop over the array and use usort()
<?php
$data['test1'][0] = array('total' => 67, 'edition' => 2, 'pkg_version' => "2.5.0" );
$data['test1'][1] = array('total' => 67, 'edition' => 2, 'pkg_version' => "0.1.0" );
$data['test1'][2] = array('total' => 67, 'edition' => 2, 'pkg_version' => "0.3.0" );
$data['test2'][0] = array('total' => 86, 'edition' => 1, 'pkg_version' => "1.5.0");
$data['test2'][1] = array('total' => 85, 'edition' => 6, 'pkg_version' => "0.53.0");
$data['test2'][2] = array('total' => 98, 'edition' => 2, 'pkg_version' => "0.3");
$data['test2'][3] = array('total' => 98, 'edition' => 2, 'pkg_version' => "0.2");
$data['test3'][0] = array('total' => 60, 'edition' => 6, 'pkg_version' => "0.3");
$data['test3'][1] = array('total' => 60, 'edition' => 7, 'pkg_version' => "0.1.1");
$data['test3'][2] = array('total' => 60, 'edition' => 7, 'pkg_version' => "0.25");
// use a reference to the sub array
// |
// |
// v
foreach ($data as &$row)
{
usort($row, function($a, $b)
{
// make a desc sort by comparing $b against $a instead of $a against $b
return strcmp($b['pkg_version'], $a['pkg_version']);
});
}
var_dump($data);
This outputs :
array(3) {
["test1"]=>
array(3) {
[0]=>
array(3) {
["total"]=>
int(67)
["edition"]=>
int(2)
["pkg_version"]=>
string(5) "2.5.0"
}
[1]=>
array(3) {
["total"]=>
int(67)
["edition"]=>
int(2)
["pkg_version"]=>
string(5) "0.3.0"
}
[2]=>
array(3) {
["total"]=>
int(67)
["edition"]=>
int(2)
["pkg_version"]=>
string(5) "0.1.0"
}
}
["test2"]=>
array(4) {
[0]=>
array(3) {
["total"]=>
int(86)
["edition"]=>
int(1)
["pkg_version"]=>
string(5) "1.5.0"
}
[1]=>
array(3) {
["total"]=>
int(85)
["edition"]=>
int(6)
["pkg_version"]=>
string(6) "0.53.0"
}
[2]=>
array(3) {
["total"]=>
int(98)
["edition"]=>
int(2)
["pkg_version"]=>
string(3) "0.3"
}
[3]=>
array(3) {
["total"]=>
int(98)
["edition"]=>
int(2)
["pkg_version"]=>
string(3) "0.2"
}
}
["test3"]=>
&array(3) {
[0]=>
array(3) {
["total"]=>
int(60)
["edition"]=>
int(6)
["pkg_version"]=>
string(3) "0.3"
}
[1]=>
array(3) {
["total"]=>
int(60)
["edition"]=>
int(7)
["pkg_version"]=>
string(4) "0.25"
}
[2]=>
array(3) {
["total"]=>
int(60)
["edition"]=>
int(7)
["pkg_version"]=>
string(5) "0.1.1"
}
}
}
Note that in the last array, the version 0.3 is higher than the version 0.25. Since it's the order in your expected output, I left it as is, but if not, you can use instead of strcmp(), version_compare(), this will provide this output for $data['test3'] :
["test3"]=>
&array(3) {
[0]=>
array(3) {
["total"]=>
int(60)
["edition"]=>
int(7)
["pkg_version"]=>
string(4) "0.25"
}
[1]=>
array(3) {
["total"]=>
int(60)
["edition"]=>
int(6)
["pkg_version"]=>
string(3) "0.3"
}
[2]=>
array(3) {
["total"]=>
int(60)
["edition"]=>
int(7)
["pkg_version"]=>
string(5) "0.1.1"
}
}
}

Array Restructuring

I have the following query result:
Array
(
[0] => stdClass Object
(
[TreatmentLog_ID] => 131
[DateAdministered] => 2016-07-15
[Notes] =>
[Treatment_ID] => 144
[AmountAdministered] => 1.5
[Injectable_ID] => 2
[InjectableName] => Baytril
)
[1] => stdClass Object
(
[TreatmentLog_ID] => 133
[DateAdministered] => 2016-07-12
[Notes] =>
[Treatment_ID] => 146
[AmountAdministered] => 1.2
[Injectable_ID] => 20
[InjectableName] => Vitamin C
)
[2] => stdClass Object
(
[TreatmentLog_ID] => 133
[DateAdministered] => 2016-07-12
[Notes] =>
[Treatment_ID] => 147
[AmountAdministered] => 1.3
[Injectable_ID] => 21
[InjectableName] => Vitamin E
)
)
I'd like to be able to restructure the array into something like this:
Array
(
[0] => stdClass Object
(
[TreatmentLog_ID] => 131
[DateAdministered] => 2016-07-15
[Notes] =>
[Treatments] => Array
(
[0] => stdClass Object
(
[Treatment_ID] => 144
[AmountAdministered] => 1.5
[Injectable_ID] => 2
[InjectableName] => Baytril
)
)
)
[1] => stdClass Object
(
[TreatmentLog_ID] => 133
[DateAdministered] => 2016-07-12
[Notes] =>
[Treatments] => Array
(
[0] => stdClass Object
(
[Treatment_ID] => 146
[AmountAdministered] => 1.2
[Injectable_ID] => 20
[InjectableName] => Vitamin C
)
[1] => stdClass Object
(
[Treatment_ID] => 147
[AmountAdministered] => 1.3
[Injectable_ID] => 21
[InjectableName] => Vitamin E
)
)
)
)
Notice how the second array looks merges the InjectableName, AmountAdministered, Injectable_ID, and Treatment_ID into the array Treatments if the TreatmentLog_ID is a match. Typically working with arrays isn't a problem, but this one has me stumped. Also I cannot change the query.
How could I pull this off in PHP?
The solution using isset and array_values functions:
// $arr is your initial array
$result = [];
foreach ($arr as $obj) {
$innerObj = (object)[ 'Treatment_ID' => $obj->Treatment_ID, 'AmountAdministered' => $obj->AmountAdministered,
'Injectable_ID' => $obj->Injectable_ID, 'InjectableName' => $obj->InjectableName ];
if (!isset($result[$obj->TreatmentLog_ID])) {
$result[$obj->TreatmentLog_ID] = (object)[
'TreatmentLog_ID' => $obj->TreatmentLog_ID,
'DateAdministered' => $obj->DateAdministered,
'Notes' => $obj->Notes,
'Treatments' => [$innerObj]
];
} else {
$result[$obj->TreatmentLog_ID]->Treatments[] = $innerObj;
}
}
$result = array_values($result);
print_r($result); // will output the expected result
Try this.
We use array_filter() to fetch all of the elements from the $inputArray with the same TreatmentLog_ID. Then we transform those filtered elements with array_map(). We have to create a copy of each element with clone, since they're objects and objects are passed by reference. Then we unset() the keys we don't need in the copy.
<?php
$inputArray = [
0 => (object) [
'TreatmentLog_ID' => 131,
'DateAdministered' => '2016-07-15',
'Notes' => '',
'Treatment_ID' => 144,
'AmountAdministered' => 1.5,
'Injectable_ID' => 2,
'InjectableName' => 'Baytril'
],
1 => (object) [
'TreatmentLog_ID' => 133,
'DateAdministered' => '2016-07-12',
'Notes' => '',
'Treatment_ID' => 146,
'AmountAdministered' => 1.2,
'Injectable_ID' => 20,
'InjectableName' => 'Vitamin C'
],
2 => (object) [
'TreatmentLog_ID' => 133,
'DateAdministered' => '2016-07-12',
'Notes' => '',
'Treatment_ID' => 147,
'AmountAdministered' => 1.3,
'Injectable_ID' => 21,
'InjectableName' => 'Vitamin E'
],
];
$transformedArray = [];
foreach ($inputArray as $key => $value)
{
$transformedArray[$key] = [
'TreatmentLog_ID' => $value->TreatmentLog_ID,
'DateAdministered' => $value->DateAdministered,
'Notes' => $value->Notes,
'Treatments' => array_map(
function ($v) {
$copy = clone $v;
unset($copy->Notes);
unset($copy->DateAdministered);
unset($copy->TreatmentLog_ID);
return $copy;
},
array_filter($inputArray, function ($v) use ($value) {
return $v->TreatmentLog_ID == $value->TreatmentLog_ID;
})
)
];
}
var_dump($transformedArray);
This gives me:
array(3) {
[0]=>
array(4) {
["TreatmentLog_ID"]=>
int(131)
["DateAdministered"]=>
string(10) "2016-07-15"
["Notes"]=>
string(0) ""
["Treatments"]=>
array(1) {
[0]=>
object(stdClass)#5 (4) {
["Treatment_ID"]=>
int(144)
["AmountAdministered"]=>
float(1.5)
["Injectable_ID"]=>
int(2)
["InjectableName"]=>
string(7) "Baytril"
}
}
}
[1]=>
array(4) {
["TreatmentLog_ID"]=>
int(133)
["DateAdministered"]=>
string(10) "2016-07-12"
["Notes"]=>
string(0) ""
["Treatments"]=>
array(2) {
[1]=>
object(stdClass)#6 (4) {
["Treatment_ID"]=>
int(146)
["AmountAdministered"]=>
float(1.2)
["Injectable_ID"]=>
int(20)
["InjectableName"]=>
string(9) "Vitamin C"
}
[2]=>
object(stdClass)#7 (4) {
["Treatment_ID"]=>
int(147)
["AmountAdministered"]=>
float(1.3)
["Injectable_ID"]=>
int(21)
["InjectableName"]=>
string(9) "Vitamin E"
}
}
}
[2]=>
array(4) {
["TreatmentLog_ID"]=>
int(133)
["DateAdministered"]=>
string(10) "2016-07-12"
["Notes"]=>
string(0) ""
["Treatments"]=>
array(2) {
[1]=>
object(stdClass)#8 (4) {
["Treatment_ID"]=>
int(146)
["AmountAdministered"]=>
float(1.2)
["Injectable_ID"]=>
int(20)
["InjectableName"]=>
string(9) "Vitamin C"
}
[2]=>
object(stdClass)#9 (4) {
["Treatment_ID"]=>
int(147)
["AmountAdministered"]=>
float(1.3)
["Injectable_ID"]=>
int(21)
["InjectableName"]=>
string(9) "Vitamin E"
}
}
}
}
Let's break down how we build Treatments:
'Treatments' => array_map(
function ($v) {
$copy = clone $v;
unset($copy->Notes);
unset($copy->DateAdministered);
unset($copy->TreatmentLog_ID);
return $copy;
},
array_filter($inputArray, function ($v) use ($value) {
return $v->TreatmentLog_ID == $value->TreatmentLog_ID;
})
)

Regular expression, filtering expected data from an array like ":key{value}"

Example:
$match_check = "(?'txt'(?<=:txt{)([^}]+)(?=}))|(?'reg'(?<=:reg{)([^}]+)(?=}))";
$route_from_value = ':txt{resultxt}:txt{test}:reg{/^[a-zA-Z]*$/}:reg{regexresult}';
preg_match_all('/'.$match_check.'/', $route_from_value, $get_matchers_check);
var_dump($get_matchers_check);
And result for given question are:
array(7) {
[0] =>
array(4) {
[0] =>
string(8) "resultxt"
[1] =>
string(4) "test"
[2] =>
string(13) "/^[a-zA-Z]*$/"
[3] =>
string(11) "regexresult"
}
'txt' =>
array(4) {
[0] =>
string(8) "resultxt"
[1] =>
string(4) "test"
[2] =>
string(0) ""
[3] =>
string(0) ""
}
[1] =>
array(4) {
[0] =>
string(8) "resultxt"
[1] =>
string(4) "test"
[2] =>
string(0) ""
[3] =>
string(0) ""
}
[2] =>
array(4) {
[0] =>
string(8) "resultxt"
[1] =>
string(4) "test"
[2] =>
string(0) ""
[3] =>
string(0) ""
}
'reg' =>
array(4) {
[0] =>
string(0) ""
[1] =>
string(0) ""
[2] =>
string(13) "/^[a-zA-Z]*$/"
[3] =>
string(11) "regexresult"
}
[3] =>
array(4) {
[0] =>
string(0) ""
[1] =>
string(0) ""
[2] =>
string(13) "/^[a-zA-Z]*$/"
[3] =>
string(11) "regexresult"
}
[4] =>
array(4) {
[0] =>
string(0) ""
[1] =>
string(0) ""
[2] =>
string(13) "/^[a-zA-Z]*$/"
[3] =>
string(11) "regexresult"
}
}
But, expected result should be (how make it only with regexp?) or something simply:
'txt' =>
array(4) {
[0] =>
string(8) "resultxt"
},
'txt' =>
array(4) {
[0] =>
string(8) "resultxt"
}
'reg' =>
array(4) {
[0] =>
string(8) "/^[a-zA-Z]*$/"
}
'reg' =>
array(4) {
[0] =>
string(8) "regexresult"
}
The output of preg_match_all and preg_split function follows a format that doesn't match what you want.
What you can do is just do some post-processing on the output of preg_match_all to obtain the result (something like an array of ['key', 'value'] pair). Note that you should set the flag PREG_OFFSET_CAPTURE to obtain the index of the match for comparison. A capturing group that matches nothing will give index -1 or will not return an array that contains 2 elements.
By the way, you can remove some unnecessary capturing group from your regex.
"(?'txt'(?<=:txt{)[^}]+(?=}))|(?'reg'(?<=:reg{)[^}]+(?=}))"
Sample run:
$matches = null;
$returnValue = preg_match_all('/(?\'txt\'(?<=:txt{)[^}]+(?=}))|(?\'reg\'(?<=:reg{)[^}]+(?=}))/', ':txt{resultxt}:txt{test}:reg{/^[a-zA-Z]*$/}:reg{regexresult}', $matches, PREG_OFFSET_CAPTURE);
Output:
array (
0 =>
array (
0 =>
array (
0 => 'resultxt',
1 => 5,
),
1 =>
array (
0 => 'test',
1 => 19,
),
2 =>
array (
0 => '/^[a-zA-Z]*$/',
1 => 29,
),
3 =>
array (
0 => 'regexresult',
1 => 48,
),
),
'txt' =>
array (
0 =>
array (
0 => 'resultxt',
1 => 5,
),
1 =>
array (
0 => 'test',
1 => 19,
),
2 => // No match found, index -1
array (
0 => '',
1 => -1,
),
3 => // No match found, index -1
array (
0 => '',
1 => -1,
),
),
1 =>
array (
0 =>
array (
0 => 'resultxt',
1 => 5,
),
1 =>
array (
0 => 'test',
1 => 19,
),
2 =>
array (
0 => '',
1 => -1,
),
3 =>
array (
0 => '',
1 => -1,
),
),
'reg' =>
array (
0 => '', // No match found, not array of 2 elements [<matched text>, <index>]
1 => '', // No match found, not array of 2 elements [<matched text>, <index>]
2 =>
array (
0 => '/^[a-zA-Z]*$/',
1 => 29,
),
3 =>
array (
0 => 'regexresult',
1 => 48,
),
),
2 =>
array (
0 => '',
1 => '',
2 =>
array (
0 => '/^[a-zA-Z]*$/',
1 => 29,
),
3 =>
array (
0 => 'regexresult',
1 => 48,
),
),
)

array key value pair at a specified point in array

I have an associative array , i would like to add some more key and values
Array
(
[0] => Array
(
[NUMBER] => 67
[TYPE] => Other
[DATE] => 3/31/2011
)
[1] => Array
(
[NUMBER] => 87
[TYPE] => something
[DATE] => 3/28/2011
)
[2] => Array
(
[NUMBER] => 67
[TYPE] => Other
[DATE] => 3/2/2011
)
)
In Above array i want to add another key named STATUS and value before DATE
so that finally iget
Array
(
[0] => Array
(
[NUMBER] => 67
[TYPE] => Other
[STATUS] => waiting
[DATE] => 3/31/2011
)
}
canPlease give me proper direction
$arr = Array(
0 => Array('NUMBER' => 67, 'TYPE' => Other, 'DATE' => '3/32/2011'),
1 => Array('NUMBER' => 87, 'TYPE' => something, 'DATE' => '3/28/2011'),
2 => Array('NUMBER' => 67, 'TYPE' => Other, 'DATE' => '3/2/2011')
);
foreach($arr as $key => $value) {
$arr[$key] = array_slice($value, 0, 2) +
array('Status' => 'waiting') +
array_slice($value, -1);
}
var_dump($arr);
gives the following array:
array(3) {
[0]=>
array(4) {
["NUMBER"]=>
int(67)
["TYPE"]=>
string(5) "Other"
["Status"]=>
string(7) "waiting"
["DATE"]=>
string(9) "3/32/2011"
}
[1]=>
array(4) {
["NUMBER"]=>
int(87)
["TYPE"]=>
string(9) "something"
["Status"]=>
string(7) "waiting"
["DATE"]=>
string(9) "3/28/2011"
}
[2]=>
array(4) {
["NUMBER"]=>
int(67)
["TYPE"]=>
string(5) "Other"
["Status"]=>
string(7) "waiting"
["DATE"]=>
string(8) "3/2/2011"
}
}

Categories