I'm having a graph like this:
Now, let's say I'm looking for a word CAT. I'm trying to make a nice code to walk this graph and find a word. I'd like it to find all existing positions of a word, not only first one.
The result for $graph->find('cat') should be:
return [
[1, 0, 6],
[1, 2, 3]
];
I have created such code in the past, but it was iterative. This time I'd like to try recursive.
Here's what I have so far:
I call it like this:
// LetterGraph is my own class
$graph = new LetterGraph($nodes);
$graph->find('cat');
And in my LetterGraph class I do the following:
public function find(string $word): array {
$result = [];
$firstLetter = mb_substr($word, 0, 1);
foreach ($this->letters as $node) {
if ($node->letter === $firstLetter) {
$result[] = $this->walk($word, [$node]);
}
}
return $result;
}
protected function walk(string $word, array $nodes): array {
$lastNode = end($nodes);
$letterToFind = mb_substr($word, count($nodes), 1);
foreach ($lastNode->neighbours as $neighbour) {
if ($neighbour->letter === $letterToFind) {
// is return okay here?
return $this->walk($word, array_merge($nodes, $neighbour);
}
}
}
Now, I'm not sure how to deal with recursive returns to make it give me the result I want.
It can be solved using Master theorem.
Assuming $node->id is the number you want to see in the resulting array, the recursion may look like
public function find(string $word, array $nodes = null): array
{
$firstLetter = mb_substr($word, 0, 1);
$rest = mb_substr($word, 1);
if (empty($nodes)) {
//top level call, start to search across all nodes
$nodes = $this->letters;
}
$result = [];
foreach ($nodes as $node) {
if ($node->letter === $firstLetter) {
if (empty($rest)) {
//exit recursion
$result[] = [$node->id];
} else {
//recursively search rest of the string
$branches = $this->find($rest, $node->neighbours);
if (!empty($branches)) {
foreach ($branches as $branch) {
$result[] = array_merge([$node->id], $branch);
}
}
}
}
}
return $result;
}
Related
I have an array which contains bunch of strings, and I would like to find all of the possible combinations no matter how it's being sorted that match with given string/word.
$dictionary = ['flow', 'stack', 'stackover', 'over', 'code'];
input: stackoverflow
output:
#1 -> ['stack', 'over', 'flow']
#2 -> ['stackover', 'flow']
What I've tried is, I need to exclude the array's element which doesn't contain in an input string, then tried to match every single merged element with it but I'm not sure and get stuck with this. Can anyone help me to figure the way out of this? thank you in advance, here are my code so far
<?php
$dict = ['flow', 'stack', 'stackover', 'over', 'code'];
$word = 'stackoverflow';
$dictHas = [];
foreach ($dict as $w) {
if (strpos($word, $w) !== false) {
$dictHas[] = $w;
}
}
$result = [];
foreach ($dictHas as $el) {
foreach ($dictHas as $wo) {
$merge = $el . $wo;
if ($merge == $word) {
} elseif ((strpos($word, $merge) !== false) {
}
}
}
print_r($result);
For problems like this you want to use backtracking
function splitString($string, $dict)
{
$result = [];
//if the string is already empty return empty array
if (empty($string)) {
return $result;
}
foreach ($dict as $idx => $term) {
if (strpos($string, $term) === 0) {
//if the term is at the start of string
//get the rest of string
$substr = substr($string, strlen($term));
//if all of string has been processed return only current term
if (empty($substr)) {
return [[$term]];
}
//get the dictionary without used term
$subDict = $dict;
unset($subDict[$idx]);
//get results of splitting the rest of string
$sub = splitString($substr, $subDict);
//merge them with current term
if (!empty($sub)) {
foreach ($sub as $subResult) {
$result[] = array_merge([$term], $subResult);
}
}
}
}
return $result;
}
$input = "stackoverflow";
$dict = ['flow', 'stack', 'stackover', 'over', 'code'];
$output = splitString($input, $dict);
In Microsoft Dynamics Nav 2013, there is a feature for specifying Item Substitutions for an item (product); However, you can specify more than one substitution for a single product and technically a substitution can itself have one or more substitutions.
I am trying to build a recursive solution in PHP that allows me to take a known product code, and recursively search for item substitutions to generate a one dimensional array of items. If this were a one to one relationship (parent,child) this would be a trivial task for me, but the fact that there can be multiple childs on any given iteration is kind of blowing my mind.
My question is if anyone knows how to write a recursive method for the situation I've described above? Below I will layout the way the data is structured to give a better understanding of the problem:
$lineItems = array(
'XXX-0',
'XXX-1',
'XXX-3'
);
$substitutionsLookup = array(
0 => array('No_' => 'XXX-1', 'Substitute No_' => 'XXX-2'),
1 => array('No_' => 'XXX-3', 'Substitute No_' => 'XXX-4'),
2 => array('No_' => 'XXX-3', 'Substitute No_' => 'XXX-5'),
3 => array('No_' => 'XXX-5', 'Substitute No_' => 'XXX-6')
);
// Resulting product code substitutions for XXX-0
$result1 = array();
// Resulting product code substitutions for XXX-1
$result2 = array('XXX-2');
// Resulting product code substitutions for XXX-3
$result3 = array('XXX-4', 'XXX-6');
Edit (added my attempt to solve using recursive method):
protected function getSubstitutions($haystack, $needle, &$results = array())
{
if (count($haystack) == 0)
{
return false;
}
$matches = array();
foreach ($haystack as $index => $check)
{
if ($check['No_'] === $needle)
{
$newHaystack = $haystack;
unset($newHaystack[$index]);
$moreMatches = $this->getSubstitutions($newHaystack, $check['Substitute No_'], $results);
if ($moreMatches === false)
{
$matches[] = $check['Substitute No_'];
}
}
}
if (count($matches))
{
foreach($matches as $match)
{
$results[] = $match;
}
}
return $results;
}
Edit (Final code used, derived from accepted answer):
class ItemSubstitutionService implements ServiceLocatorAwareInterface
{
public function getSubstitutions($itemNo, $noInventoryFilter = true, $recursive = true)
{
$substitutions = array();
$directSubs = $this->itemSubstitutionTable->getSubstitutionsByNo($itemNo);
if ($recursive)
{
foreach($directSubs as $sub)
{
$this->getSubstitutionsRecursive($sub, $substitutions);
}
} else {
$substitutions = $directSubs;
}
foreach($substitutions as $index => $sub)
{
$inventory = $this->itemLedgerEntryTable->getQuantityOnHand($sub->getSubstituteNo());
$sub->setInventory($inventory);
if ($noInventoryFilter)
{
if ($inventory == 0)
{
unset($substitutions[$index]);
}
}
}
return $substitutions;
}
private function getSubstitutionsRecursive(ItemSubstitution $sub, &$subs)
{
$directSubs = $this->itemSubstitutionTable->getSubstitutionsByNo($sub->getSubstituteNo());
if (empty($directSubs))
{
$subs[$sub->getSubstituteNo()] = $sub;
}
foreach($directSubs as $curSub)
{
$this->getSubstitutionsRecursive($curSub, $subs);
}
}
}
This code could serve as a solution for your example.
I just presume that you fetch the list of 'direct' items substitutes from your database, so you could replace GetDirectSubstitutes with the code that fetches the substitutes list for the given item (I used you example array as a data source).
Just be careful - this simple implementation doesn't check for cyclical references. If your initial data contains loops, this code will stuck.
function GetDirectSubstitutes($itemNo)
{
global $substitutionsLookup;
$items = array();
foreach ($substitutionsLookup as $itemPair) {
if ($itemPair['No'] == $itemNo) {
array_push($items, $itemPair['SubstNo']);
}
}
return $items;
}
function GetSubstitutesTree($itemNo, &$substitutes)
{
$directSubst = GetDirectSubstitutes($itemNo);
if (!empty($directSubst)) {
$substitutes = array_merge($substitutes, $directSubst);
foreach ($directSubst as $item) {
GetSubstitutesTree($item, $substitutes);
}
}
}
The following code uses foreach on an array and if the value is an array it does a for each on the nested array
foreach ($playfull as $a)
{
if (is_array($a))
{
foreach ($a as $b)
{
print($b);
print("<p>");
}
} else {
print($a);
print("<p>");
}
}
This only works if you know that the arrays may only be nested one level deep
If arrays could be nested an unknown number of levels deep how do you achieve the same result? (The desired result being to print the value of every key in every array no matter how deeply nested they are)
You can use array_walk_recursive. Example:
array_walk_recursive($array, function (&$val)
{
print($val);
}
This function is a PHP built in function and it is short.
Use recursive functions (that are functions calling themselves):
function print_array_recursively($a)
{
foreach ($a as $el)
{
if (is_array($el))
{
print_array_recursively($el);
}
else
{
print($el);
}
}
}
This is the way, print_r could do it (see comments).
You want to use recursion, you want to call your printing function in itself, whenever you find an array, click here to see an example
$myArray = array(
"foo",
"bar",
"children" => array(
"biz",
"baz"),
"grandchildren" => array(
"bang" => array(
"pow",
"wow")));
function print_array($playfull)
{
foreach ($playfull as $a)
{
if (is_array($a))
{
print_array($a);
} else {
echo $a;
echo "<p>";
}
}
}
echo "Print Array\n";
print_array($myArray);
You could use a recursive function, but the max depth will be determined by the maximum nesting limit (see this SO question, Increasing nesting functions calls limit, for details about increasing that if you need it)
Here's an example:
$array = array(1,array(2,3,array(4,5)),6,7,8);
function printArray($item)
{
foreach ($item as $a)
{
if (is_array($a))
{
printArray($a);
} else {
print($a);
print("<p>");
}
}
}
printArray($array);
I hope that helps.
Try this -
function array_iterate($arr, $level=0, $maxLevel=0)
{
if (is_array($arr))
{
// unnecessary for this conditional to enclose
// the foreach loop
if ($maxLevel < ++$level)
{ $maxLevel = $level; }
foreach($arr AS $k => $v)
{
// for this to work, the result must be stored
// back into $maxLevel
// FOR TESTING ONLY:
echo("<br>|k=$k|v=$v|level=$level|maxLevel=$maxLevel|");
$maxLevel= array_iterate($v, $level, $maxLevel);
}
$level--;
}
// the conditional that was here caused all kinds
// of problems. so i got rid of it
return($maxLevel);
}
$array[] = 'hi';
$array[] = 'there';
$array[] = 'how';
$array['blobone'][] = 'how';
$array['blobone'][] = 'are';
$array['blobone'][] = 'you';
$array[] = 'this';
$array['this'][] = 'is';
$array['this']['is'][] = 'five';
$array['this']['is']['five'][] = 'levels';
$array['this']['is']['five']['levels'] = 'deep';
$array[] = 'the';
$array[] = 'here';
$var = array_iterate($array);
echo("<br><br><pre>$var");
I am trying to parse some data and just cant seem to figure it out myself.
I have tried using recursion and got the general idea but i cant seem to work out getting the array indexes to line up right. Here is what I have so far:
public function reverse() {
$reversedValues= array();
foreach ( $this->sorted as $key=>$value ) {
array_push( $this->reversedPaths ,$key );
array_push( $reversedValues , $value );
//print_r($this->reversed);
echo "<br />";
}
$this->reversed = $this->stringToArray($this->reversedPaths[0] , $reversedValues);
var_dump($this->reversed);
return json_encode($this->reversed , false);
}
private function stringToArray($path , $values , $count = 0) {
$separator = '/';
$pos = strpos($path, $separator);
if ($pos === false) {
//check for numbers so we know to add those to an json object
if (is_numeric($path)) {
//add it to the parent array of values...
}
$reversed = array(
$path => $values[$count],
);
return $reversed;
}
$key = substr($path, 0, $pos);
$path = substr($path, $pos + 1);
$reversed[$key] = $this->stringToArray($path , $values , $count);
$count++;
//read in next path string
if (array_key_exists( $count ,$this->reversedPaths)) {
$currentpos = strpos($this->reversedPaths[$count], $path.$separator);
$currentPath = substr($this->reversedPaths[$count], $currentpos );
$nextpos = strpos($currentPath, $separator);
if ($nextpos === false) {
} else {
$nextPath = substr($currentPath, $nextpos + 1);
$nextKey = substr($nextPath, 0, $nextpos);
echo $nextKey;
echo $nextPath;
// echo $nextKey;
//if this key equals first value of next path dont return but process recurssion again on it
if ($nextKey !== false ) {
$reversed[$key][$nextKey] = $this->stringToArray($nextPath , $values , $count);
}
}
} else {
}
return $reversed;
}
I was trying to read in the next path data to check if it is within the same array index but i just couldn't get it working. I know i am over complicating it but it doesn't seem like there is any easy way to accomplish this...
I had a crack at this. Based on the details you provided, it looks like you are trying to create a tree, like a directory structure: each / delimited string in the key represents a 'depth'. The solution I found was to create a multidimensional array for each element, parsing the current key into levels and recursively merge/replace the result into a master 'tree' array. This is what I have:
// original JSON string
$json = '{
"one/two": 3,
"one/four/0": 5,
"one/four/1": 6,
"one/four/2": 7,
"eight/nine/ten": 11
}';
// convert to a PHP multidimensional array
$array = json_decode($json, true);
// init an array to hold the final result
$tree = [];
// iterate over the array of values
// explode the key into an array 'path' tokens
// pop each off and build a multidimensional array
// finally 'merge' the result into the result array
foreach ($array as $path => $value) {
$tokens = explode('/', $path);
while (null !== ($key = array_pop($tokens))) {
$current = [$key => $value];
$value = $current;
}
$tree = array_replace_recursive($tree, $value);
}
// show the result!
print_r(json_encode($tree, JSON_PRETTY_PRINT));
Yields:
{
'one': {
'two': 3,
'four': [5, 6, 7]
},
'eight': {
'nine': {
'ten':11
}
}
}
Hope this helps :)
I want to pass an array to a function and iterate through it in this function. But I would like to be able to change the way the single entries are displayed.
Suppose I have an array of complicated objects:
$items = array($one, $two, $three);
Right now I do:
$entries = array();
foreach($items as $item) {
$entries[] = create_li($item["title"], pretty_print($item["date"]));
}
$list = wrap_in_ul($entries);
I would like to do the above in one line:
$list = create_ul($items, $item["title"], pretty_print($item["date"]));
Any chance of doing that as of PHP4? Be creative!
from my understanding, you're looking for an "inject" type iterator with a functional parameter. In php, inject iterator is array_reduce, unfortunately it's broken, so you have to write your own, for example
function array_inject($ary, $func, $acc) {
foreach($ary as $item)
$acc = $func($acc, $item);
return $acc;
}
define a callback function that processes each item and returns accumulator value:
function boldify($list, $item) {
return $list .= "<strong>$item</strong>";
}
the rest is easy:
$items = array('foo', 'bar', 'baz');
$res = array_inject($items, 'boldify', '');
print_r($res);
You could use callbacks:
function item_title($item) {
return $item['title'];
}
function item_date($item) {
return $item['date'];
}
function prettyprint_item_date($item) {
return pretty_print($item['date']);
}
function create_ul($items, $contentf='item_date', $titlef='item_title') {
$entries = array();
foreach($items as $item) {
$title = call_user_func($titlef, $item);
$content = call_user_func($contentf, $item);
$entries[] = create_li($title, $content);
}
return wrap_in_ul($entries);
}
...
$list = create_ul($items, 'prettyprint_item_date');
PHP 5.3 would be a big win here, with its support for anonymous functions.