I have the following structure array:
array:6 [▼
"593a4331b25f428814000035" => array:8 [▶]
"593a4331b25f428814000036" => array:8 [▶]
"593a4331b25f428814000037" => array:8 [▶]
"593a4331b25f428814000038" => array:8 [▼
"_id" => MongoId {#238 ▶}
"object_id" => "593a4331b25f428814000034"
"parameter_id" => "59398f5ab25f424016000029"
"value" => "1"
"children" => []
"parent_id" => "593a4331b25f428814000037"
"type" => "2"
"prefix" => "object"
]
"593a4331b25f428814000039" => array:8 [▶]
"593a4331b25f42881400003a" => array:8 [▶]
]
As you can see 3-th element of array has parent 593a4331b25f428814000037, where identificator is element in the same array.
How to put this element 593a4331b25f428814000038 inside parent in children?
In result I need to get:
"593a4331b25f428814000037" => array:8 [▼
"_id" => MongoId {#238 ▶}
"object_id" => "593a4331b25f428814000034"
"parameter_id" => "59398f5ab25f424016000029"
"value" => "1"
"children" => [ 0 => array("_id" => MongoId {#238 ▶}
"object_id" => "593a4331b25f428814000034"
"parameter_id" => "59398f5ab25f424016000029"
"value" => "1"
"children" => []
"parent_id" => "593a4331b25f428814000037"
"type" => "2"
"prefix" => "object")]
"parent_id" => "593a4331b25f428814000037"
"type" => "2"
"prefix" => "object"
]
I tried this way:
public function recursion($data){
foreach ($data as $k => $value) {
if (is_array($value['children']) && count($value['children']) > 0) {
$list[$k] = $value;
$list[$k]["children"] = $this->getChildren($all, $value['children']);
} else {
$list[$k] = $value;
}
}
return $list;
}
private function getChildren($all, $childs)
{
$list = [];
foreach ($childs as $k => $child) {
if (is_array($all[$child]['children'])) {
$tmpArray = $all[$child];
$tmpArray['children'] = $this->getChildren($all, $all[$child]['children']);
} else {
$tmpArray = $all[$child];
}
$list[] = $tmpArray;
}
return $list;
}
But it works incorrect
You can use array_reduce like:
$array = array_reduce($myArray, function ($carry, $item) {
if (empty($item['parent_id'])) {
$carry[$item['object_id']] = $item;
} else {
$carry[$item['parent_id']]['children'][] = $item;
}
return $carry;
}, []);
var_dump($array);
In this example I supposed the parent_id is empty for those items which doesn't belong to a another item. You can change that with isset in case there is no parent_id key
Related
I have a problem that has been stressing me out for weeks now and i cannot find a clean solution to it that does not involve recursion.
This is the problem:
Take a flat array of nested associative arrays and group this into one deeply nested object. The top level of this object will have its parent property as null.
This is my solution but i admit it is far from perfect. I am fairly certain this can be done in a single loop without any recursion, but for the life of me i cannot work it out!
//Example single fork
$data = array(
//Top of Tree
0 => array(
"name" => "A",
"parent" => null,
"id" => 1,
),
//B Branch
1 => array(
"name" => "B",
"parent" => "1",
"id" => 2,
),
2 => array(
"name" => "B1",
"parent" => "2",
"id" => 3,
),
3 => array(
"name" => "B2",
"parent" => "3",
"id" => 4,
),
4 => array(
"name" => "B3",
"parent" => "4",
"id" => 5,
),
//C Branch
5 => array(
"name" => "C",
"parent" => "1",
"id" => 6,
),
6 => array(
"name" => "C1",
"parent" => "6",
"id" => 7,
),
7 => array(
"name" => "C2",
"parent" => "7",
"id" => 8,
),
8 => array(
"name" => "C3",
"parent" => "8",
"id" => 9,
),
);
Actual anonymised example
array:7214 [▼
0 => array:3 [▼
"name" => ""
"parent" => null
"id" =>
]
1 => array:3 [▼
"name" => ""
"parent" =>
"id" =>
]
2 => array:3 [▼
"name" => ""
"parent" =>
"id" =>
]
3 => array:3 [▼
"name" => ""
"parent" =>
"id" =>
]
4 => array:3 [▼
"name" => ""
"parent" =>
"id" =>
]
5 => array:3 [▼
"name" => ""
"parent" =>
"id" =>
]
6 => array:3 [▼
"name" => ""
"parent" =>
"id" =>
]
7 => array:3 [▼
"name" => ""
"parent" =>
"id" =>
]
8 => array:3 [▼
"name" => ""
"parent" =>
"id" =>
]
9 => array:3 [▼
"name" => ""
"parent" =>
"id" =>
]
10 => array:3 [▼
"name" => ""
"parent" =>
"id" =>
]
Another example deeper nesting
{
"name":"top",
"id":xxx,
"children":{
"second":{
"name":"second",
"id":xxx,
"children":{
"Third":{
"name":"third",
"id":xxx,
"children":{
"fourth":{
"name":"fourth",
"id":xxx
}
}
}
}
}
}
}
$originalLength = count($data);
$obj = [];
while ($originalLength > 0) {
foreach ($data as $item) {
$name = $item['name'];
$parent = $item['parent'];
$a = isset($obj[$name]) ? $obj[$name] : array('name' => $name, 'id'=>$item['id']);
if (($parent)) {
$path = get_nested_path($parent, $obj, array(['']));
try {
insertItem($obj, $path, $a);
} catch (Exception $e) {
continue;
//echo 'Caught exception: ', $e->getMessage(), "\n";
}
}
$obj[$name] = isset($obj[$name]) ? $obj[$name] : $a;
$originalLength--;
}
}
echo json_encode($obj['A']);
function get_nested_path($parent, $array, $id_path)
{
if (is_array($array) && count($array) > 0) {
foreach ($array as $key => $value) {
$temp_path = $id_path;
array_push($temp_path, $key);
if ($key == "id" && $value == $parent) {
array_shift($temp_path);
array_pop($temp_path);
return $temp_path;
}
if (is_array($value) && count($value) > 0) {
$res_path = get_nested_path(
$parent, $value, $temp_path);
if ($res_path != null) {
return $res_path;
}
}
}
}
return null;
}
function insertItem(&$array, $path, $toInsert)
{
$target = &$array;
foreach ($path as $key) {
if (array_key_exists($key, $target))
$target = &$target[$key];
else throw new Exception('Undefined path: ["' . implode('","', $path) . '"]');
}
$target['children'] = isset($target['children']) ? $target['children'] : [];
$target['children'][$toInsert['name']] = $toInsert;
return $target;
}
Here's my take on what I believe is the desired output:
function buildTree(array $items): ?array {
// Get a mapping of each item by ID, and pre-prepare the "children" property.
$idMap = [];
foreach ($items as $item) {
$idMap[$item['id']] = $item;
$idMap[$item['id']]['children'] = [];
}
// Store a reference to the treetop if we come across it.
$treeTop = null;
// Map items to their parents' children array.
foreach ($idMap as $id => $item) {
if ($item['parent'] && isset($idMap[intval($item['parent'])])) {
$parent = &$idMap[intval($item['parent'])];
$parent['children'][] = &$idMap[$id];
} else if ($item['parent'] === null) {
$treeTop = &$idMap[$id];
}
}
return $treeTop;
}
This does two array cycles, one to map up the data by ID, then one to assign children to parents. Some key elements to note:
The build of $idMap in the first loop also effectively copies the items here so we won't be affecting the original input array (Unless it already contained references).
Within the second loop, there's usage of & to use references to other items, otherwise by default PHP would effectively create a copy upon assignment since these are arrays (And PHP copies arrays on assignment unlike Objects in PHP or arrays in some other languages such as JavaScript). This allows us to effectively share the same array "item" across the structure.
This does not protect against bad input. It's possible that invalid mapping or circular references within the input data could cause problems, although our function should always just be performing two loops, so should at least not get caught in an infinite/exhaustive loop.
category:
array:3 [▼
"78f895684c" => "blue"
"f71db561ba" => "green"
"3e231651de" => "blue"
]
numbersGroup:
array:3 [▼
0 => array:4 [▼
"uuid" => "78f895684c"
"price" => "10"
"discount" => "0"
"total" => "10.00"
]
1 => array:4 [▼
"uuid" => "f71db561ba"
"price" => "2"
"discount" => "0"
"total" => "2.00"
]
2 => array:4 [▼
"uuid" => "3e231651de"
"price" => "50"
"discount" => "10"
"total" => "40.00"
]
]
I try to create an array that sorts my items into categories, but also sums all items in a category together. This is what I have:
foreach ($numbersGroup as $group) {
$cat = $category[$group['uuid']];
unset($group['uuid']);
$numbersArray[$cat][] = $group;
}
With the result:
array:2 [▼
"blue" => array:2 [▼
0 => array:3 [▼
"price" => "10"
"discount" => "0"
"total" => "10.00"
]
1 => array:3 [▼
"price" => "50"
"discount" => "10"
"total" => "40.00"
]
]
"green" => array:1 [▼
0 => array:3 [▼
"price" => "2"
"discount" => "0"
"total" => "2.00"
]
]
]
This is what I am trying to achieve:
array:2 [▼
"blue" => array:2 [▼
0 => array:3 [▼
"price" => "60"
"discount" => "10"
"total" => "50.00"
]
]
"green" => array:1 [▼
0 => array:3 [▼
"price" => "2"
"discount" => "0"
"total" => "2.00"
]
]
]
This is my approach:
foreach ($numbersGroup as $group) {
$cat = $category[$group['uuid']];
unset($group['uuid']);
foreach ($group as $key => $value) {
$sum += $value;
}
$numbersArray[$cat][] = $sum;
}
But I get the error:
Unsupported operand types
Check if $numbersArray[$cat] is already set - if so, don’t add a new array element, but add the individual values to the existing array keys.
foreach ($numbersGroup as $group) {
$cat = $category[$group['uuid']];
unset($group['uuid']);
if(isset($numbersArray[$cat])) {
$numbersArray[$cat]['price'] += $group['price'];
$numbersArray[$cat]['discount'] += $group['discount'];
$numbersArray[$cat]['total'] += $group['total'];
}
else {
$numbersArray[$cat] = $group; // no [] here, because we want only a single element
}
}
Or, without an explicit if, using the ?? operator instead:
foreach ($numbersGroup as $group) {
$cat = $category[$group['uuid']];
$numbersArray[$cat]['price'] = ($numbersArray[$cat]['price'] ?? 0) + $group['price'];
$numbersArray[$cat]['discount'] = ($numbersArray[$cat]['discount'] ?? 0) + $group['discount'];
$numbersArray[$cat]['total'] = ($numbersArray[$cat]['total'] ?? 0) + $group['total'];
}
--
Edit:
I unfortunately I cannot write into my code "price", "discount" etc, because these values are always different, this cannot be hardcoded
Then loop over the keys you get from group, as you tried to with your initial code already:
foreach ($numbersGroup as $group) {
$cat = $category[$group['uuid']];
unset($group['uuid']);
foreach($group as $key => $value) {
$numbersArray[$cat][$key] = ($numbersArray[$cat][$key] ?? 0) + $value;
}
}
Can you try the below code
foreach ($numbersGroup as $group) {
$cat = $category[$group['uuid']];
if(!isset( $numbersArray[$cat][0]) )
$numbersArray[$cat][] = $group;
else{
// If index is known, you can use below code
/*$numbersArray[$cat][0]["price"]+= $group['price'];
$numbersArray[$cat][0]["discount"]+= $group['discount'];
$numbersArray[$cat][0]["total"]+= $group['total'];*/
// If index is not known
foreach($group as $key => $value){
if (is_numeric($value)) {
$numbersArray[$cat][0][$key]+= $group[$key];
}
}
}
}
Now I have added the case if array index is not known. We need a numeric checking before adding the values.
I hope this helps you :)
i need to convert a associative array to a 2 dimensional array in php, the origin array is as followed
array:7 [▼
"data" => "data"
"id_1553539135251" => "<p>nsmn</p>"
"about" => "about"
"id_1553539141598" => "<p>uiu</p>"
my code
$data = $request->all();
$json = array();
foreach($data as $key => $value){
if(strpos($key, 'id') !== false){
$json[$key]['content'] = $value;
}
}
i need the output of the following for each to be
array:3 [▼
"id_1553539135251" => array:1 [▼
"content" => "<p>nsmn</p>"
"data" => "data"
]
"id_1553539141598" => array:1 [▼
"content" => "<p>uiu</p>"
"about" => "about"
]
]
but my code outputs
array:3 [▼
"id_1553539135251" => array:1 [▼
"content" => "<p>nsmn</p>"
]
"id_1553539138029" => array:1 [▼
"content" => "<p>jjkjk</p>"
]
"id_1553539141598" => array:1 [▼
"content" => "<p>uiu</p>"
]
]
guidance on how to achieve the desired output is appreciated.
<?php
$test=array(
array(
"data" => "data",
"id_1553539135251" => "<p>nsmn</p>",
"about" => "about",
"id_1553539141598" => "<p>uiu</p>"
),
);
$output=array();
foreach($test as $item){
$i=0;
$tt='';
foreach($item as $k=>$v){
if(strpos($k, 'id') !== false){
$output[$k]=array(
'content'=>$item[$k],
'header'=>$tt,
);
}else{
$tt=$v;
}
}
}
print_r($output);
This shouldn't be confusing me as much as it is but I am looking to turn this:
array:3 [▼
"subject" => array:2 [▼
0 => "math"
1 => "english"
]
"grade" => array:2 [▼
0 => "a"
1 => "b"
]
"received" => array:2 [▼
0 => "2017"
1 => "2016"
]
]
into this:
array:2 [▼
"0" => array:3 [▼
"subject" => "math"
"grade" => "a"
"received" => "2017"
]
"1" => array:3 [▼
"subject" => "english"
"grade" => "b"
"received" => "2016"
]
]
Tried looping through in a couple different ways but never seem to get the result I am looking for, any help would be much appreciated!
$keys = array_keys($array);
$result = array_map(
function (...$values) use ($keys) { return array_combine($keys, $values); },
...array_values($array)
);
Which is essentially this, but less repetitive:
array_map(
function ($subject, $grade, $received) {
return [
'subject' => $subject,
'grade' => $grade,
'received' => $received
];
},
$array['subject'],
$array['grade'],
$array['received']
)
See the manual for array_map and ... for more explanation.
simple Version:
$arr1 = array(...);
$arr2 = array();
foreach ($arr1 as $k => $v) {
foreach ($v as $x => $y) {
$arr2[$x][$k] = $y;
}
}
But you should add conditions, if the array element not exists, create it, or you may get Errors, depending on your PHP configuration.
I have an array that looks like this:
RecursiveArrayIterator {#605 ▼
+"xs:schema": array:2 [▼
"value" => array:1 [▼
"xs:element" => array:2 [▼
"value" => array:1 [▼
"xs:complexType" => array:2 [▼
"value" => array:2 [▼
"xs:sequence" => array:2 [▼
"value" => array:1 [▼
"xs:element" => array:3 [▼
0 => array:2 [▼
"value" => array:1 [▼
"xs:simpleType" => array:2 [▼
"value" => array:1 [▼
"xs:restriction" => array:2 [▼
"value" => array:1 [▼
"xs:maxLength" => array:1 [▼
"attributes" => array:1 [▼
"value" => "40"
]
]
]
"attributes" => array:1 [▶]
]
]
"attributes" => []
]
]
"attributes" => array:1 [▼
"name" => "title"
]
]
1 => array:2 [▶]
2 => array:2 [▶]
]
]
"attributes" => []
]
"xs:attribute" => array:2 [▶]
]
"attributes" => []
]
]
"attributes" => array:1 [▼
"name" => "book"
]
]
]
"attributes" => []
]
}
I need to access the xs:maxLength attribute, so in order to that that, I'm using the following method:
private function findRestrictions(array $haystack, $needle)
{
$iterator = new \RecursiveArrayIterator($haystack);
$recursive = new \RecursiveIteratorIterator(
$iterator,
\RecursiveIteratorIterator::SELF_FIRST
);
foreach ($recursive as $key => $value)
{
if ($key === $needle)
{
return (int)$value['attributes']['value'];
}
}
}
$maxLength = findRestrictions($array, 'xs:maxLength');
So that gives me back 40, just like expected. Anyway, my issue is that I need to know to which element this limit belongs to, which is mentioned in xs:element[0]['attributes']['name'] and I'm uncertain on how to reach there to grab the information I need, based on the match for xs:maxLength.
Well I have programmed a pretty good solution I think, this time it is tested.
My sample array:
$array = [
"we" =>
["are" => [
"lorem" => [
"gone" => "away",
"my" => "friend"
],
"never" => "abcd",
"any" => [
"btc" => "abc",
"help" => [
"mqf" => "bmx"
]
]
]
],
"fancy" => [
"lorem" => [
"gone" => "away",
"my" => "friend"
],
"never" => "abcd",
"any" => [
"btc" => "abc",
"help" => [
"mqf" => "bmx",
"abc" => 13
]
]
],
"beer" => "bar",
"helpful" => [
"meta" => "column",
"gamma" => [
"lorem" => [
"gone" => "mad",
"my" => "drink"
],
"never" => "abcd",
"any" => [
"btc" => "abc",
"help" => [
"mqf" => "bmx",
"abc" => "alot"
]
]
]
],
"elements" => [
0 => 88,
1 => 99
]
];
My solution:
function array_find_value_return_parent($array,$needle,$parentkey) {
$iterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator($array),
RecursiveIteratorIterator::SELF_FIRST);
foreach($iterator as $key => $value) {
if($value === $needle) {
for ($i = $iterator->getDepth() - 1; $i >= 0; $i--) {
if($iterator->getSubIterator($i)->key() === $parentkey) {
return $iterator->getSubIterator($i)->current();
}
}
}
}
}
function array_find_key_return_value($array,$findkey) {
$iterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator($array),
RecursiveIteratorIterator::SELF_FIRST);
foreach($iterator as $key => $value) {
if($findkey === $key) {
return $iterator->current();
}
}
}
My test:
$findvalue = "alot";
$findparentkey = "gamma";
$findreturnkey = "gone";
echo array_find_key_return_value(array_find_value_return_parent($array,$findvalue,$findparentkey),$findreturnkey);
Output: mad
For your case it means that you might do the following:
$findvalue = "40";
$findparentkey = "xs:element";
$findreturnkey = "name";
echo array_find_key_return_value(array_find_value_return_parent($array,$findvalue,$findparentkey),$findreturnkey);
Expected output: title
Right?
I do not know your original data structure, so I just converted your data to a PHP array. You can use $aks = new ArrayKeySearcher($data, 'xs:maxLength'); to find the key you want. And you can make the search more complex to satisfy your requirement.
However, if you are using something like XML, it is highly recommended to use XML-based solutions, like XPath query (eg: http://php.net/manual/en/domxpath.query.php, http://php.net/manual/en/simplexmlelement.xpath.php). These methods are easier to use, faster and more accurate.
<?php
$data = [
"xs:schema"=> [
"value" => [
"xs:element" => [
"value" => [
"xs:complexType" => [
"value" => [
"xs:sequence" => [
"value" => [
"xs:element" => [
0 => [
"value" => [
"xs:simpleType" => [
"value" => [
"xs:restriction" => [
"value" => [
"xs:maxLength" => [
"attributes" => [
"value" => "40"
]
]
],
"attributes" => []
]
],
"attributes" => []
]
],
"attributes" => [
"name" => "title"
]
],
1 => [],
2 => [],
]
],
"attributes" => []
],
"xs:attribute" => []
],
"attributes" => []
]
],
"attributes" => [
"name" => "book"
]
]
],
"attributes" => []
]
];
class ArrayKeySearcher
{
public $data;
public $path;
public $value;
public function __construct($data, $key)
{
$this->data = $data;
$this->findKeyPath($data, $key);
}
private function findKeyPath($data, $key)
{
foreach ($data as $k => $v) {
$this->path[] = $k;
if ($key === $k) {
$this->value = $v;
return;
}
$this->findKeyPath($v, $key);
if (!is_null($this->value))
return;
array_pop($this->path);
}
}
public function arrayReverseSearch($a, $k, $pos = null)
{
$count = count($a);
$i = ($pos === null) ? ($count - 1) : $pos;
for(; $i >= 0; $i--) {
if($a[$i] === $k)
return $i;
}
return $i;
}
public function getValueByPath($path)
{
$v = $this->data;
foreach($path as $k) {
if(isset($v[$k]))
$v = $v[$k];
}
return $v;
}
}
$aks = new ArrayKeySearcher($data, 'xs:maxLength');
echo 'path: ' . json_encode($aks->path) . PHP_EOL;
echo 'value: ' . json_encode($aks->value) . PHP_EOL;
$p = $aks->path;
$pos = $aks->arrayReverseSearch($p, 'xs:simpleType');
$pos = $aks->arrayReverseSearch($p, 'value', $pos);
$p = array_slice($p, 0, $pos);
$parent = $aks->getValueByPath($p);
echo 'parent path: ' . json_encode($p) . PHP_EOL;
echo 'parent attributes: ' . json_encode($parent['attributes']) . PHP_EOL;
Output:
path: ["xs:schema","value","xs:element","value","xs:complexType","value","xs:sequence","value","xs:element",0,"value","xs:simpleType","value","xs:restriction","value","xs:maxLength"]
value: {"attributes":{"value":"40"}}
parent path: ["xs:schema","value","xs:element","value","xs:complexType","value","xs:sequence","value","xs:element",0]
parent attributes: {"name":"title"}