I have a recursion function that parses an object/array with a global variable. If I comment out the global variable I get nothing but if I leave it in it keeps adding to the array other values that should be in it own result set. Do I need to change something here?
UPDATE #2:
How can I get the return I want, I thought I was pushing all unique values to the array?
function getResp($objectPassed) {
foreach($objectPassed as $element) {
if(is_object($element)) {
// recursive call
$in_arr = getResp($element);
}elseif(is_array($element)) {
$in_arr = getResp($element);
} else {
// XML is being passed, need to strip it
$element = strip_tags($element);
// Trim whitespace
$element = trim($element);
// Push to array
if($element != '') {
if (!preg_match("/^[0-9]$/", $element)) {
if (!in_array($element,$in_arr)) {
$in_arr[] = $element;
}
}
}
}
}
return $in_arr;
}
INPUT:
stdClass Object
(
[done] => 1
[queryLocator] =>
[records] => Array
(
[0] => stdClass Object
(
[type] => typeName
[Id] => Array
(
[0] => a0E50000002jxhmEAA
[1] => a0E50000002jxhmEAA
)
)
[1] => stdClass Object
(
[type] => typeName
[Id] => Array
(
[0] => a0E50000002jxYkEAI
[1] => a0E50000002jxYkEAI
)
)
)
[size] => 2
)
RETURN:
Array
(
[0] => a0E50000002jxYkEAI
)
WANTED RETURN:
Array
(
[0] => a0E50000002jxYkEAI
[1] => a0E50000002jxhmEAA
)
Is a global variable necessary? Otherwise you could simplify it this way:
function getResp($objectPassed, &$in_arr = array()) { // <-- note the reference '&'
foreach($objectPassed as $element) {
if(is_object($element) || is_array($element)) { // <-- else if statement simplified
getResp($element,$in_arr);
} else {
// XML is being passed, need to strip it
$element = strip_tags($element);
// Trim whitespace
$element = trim($element);
// Push to array
if($element != '' && // <-- everything in one test
!preg_match("/^[0-9]$/", $element) &&
!in_array($element,$in_arr))
{
$in_arr[] = $element;
}
}
}
return $in_arr;
}
Then you do:
$result = getResp($data);
If a recursive function has to access the same resource over and over again (in this case the initial array), I would always pass this as a reference.
I don't know if is measurable but I would guess that this is much more efficient than copying values.
Related
I have a multi-dimensional array of objects (see sample data below). Now I want to search a value (or property) in the data. If the value is found, the function should return the right object and stop searching.
I found three solutions to do this with a recursive function. But nothing works like described above.
The first is my own solution:
public static function getPathForUrl($folderContentDetails, string $url, $result = NULL)
{
foreach($folderContentDetails as $key => $item)
{
if($item->url === $url)
{
$result = $item;
}
elseif($item->elementType === "folder")
{
$result = self::getPathForUrl($item->folderContent, $url, $result);
}
}
return $result;
}
If you call the function like this:
print_r(self::getPathForUrl($data, 'order/abc/alpha');
then it returns the right object. The downside is, that the function searches the whole data and finally returns the result. I did not find a way to stop the function, if a result is found, so its wasting resources.
The second (standard-)solution that you will find in the web, looks like this:
public static function getPathForUrl($folderContentDetails, string $url)
{
foreach($folderContentDetails as $key => $item)
{
if($url === $item->url OR ($item->elementType == "folder" && Folder::getPathForUrl($item->folderContent, $url) !== false))
{
print_r('inner: <br/>'.$item->url);
// prints more then one value, depending on your data, in my case :
// /order/abc/alpha
// /order/abc
// /order
return $item;
}
}
return false;
}
This function stops, if it finds the right value. But for some reason it returns more than one object, and the final object is wrong (see code-comments).
The last solution looks like this:
public static function getPathForUrl($folderContentDetails, string $url)
{
foreach($folderContentDetails as $key => $item)
{
if($item->elementType == "folder" && $item->url != $url)
{
return self::getPathForUrl($item->folderContent, $url);
// iterates only the first sub-folder, then stops
}
elseif($item->url == $url)
{
print_r($item); //nothing, if not found in first sub-folder
return $item; // nothing, if not found in first sub-folder
}
}
return false;
}
If you return the result of the recursive function, then the function goes down to the first nested element and stops there, so it does not go up again to search the other elements.
If you do not return the result, the function searches the whole data but, of course, does not return the right object.
I probably do not understand the concept of recursion properly. Any help is highly welcome.
These are some sample data:
Array
(
[0] => stdClass Object
(
[elementType] => folder
[path] =>
[url] => /getting-started
[folderContent] => Array
(
[0] => stdClass Object
(
[elementType] => file
[path] => \0_getting_started\01-installation.md
[url] => /getting-started/installation
)
[1] => stdClass Object
(
[elementType] => file
[path] => \0_getting_started\02-system-settings.md
[url] => /getting-started/system-settings
)
[2] => stdClass Object
(
[elementType] => file
[path] => \0_getting_started\index.md
[url] => /getting-started/index
)
)
)
[1] => stdClass Object
(
[elementType] => folder
[path] =>
[url] => /order
[folderContent] => Array
(
[0] => stdClass Object
(
[elementType] => folder
[path] => \2_order
[url] => /order/abc
[folderContent] => Array
(
[0] => stdClass Object
(
[elementType] => file
[path] => \2_order\abc\alpha.md
[url] => /order/abc/alpha
)
)
)
)
)
[3] => stdClass Object
(
[elementType] => file
[path] => \index.md
[url] => /index
)
)
Return exits the function so if you want the first result just return it in your condition
public static function getPathForUrl($folderContentDetails, string $url, $result = NULL)
{
foreach($folderContentDetails as $key => $item)
{
if($item->url === $url)
{
return $item;
}
elseif($item->elementType === "folder")
{
$result = self::getPathForUrl($item->folderContent, $url, $result);
}
}
return $result;
}
If you want to stop after a match of value is made; just return inside the condition;
public static function getPathForUrl($folderContentDetails, string $url, $result = NULL)
{
foreach($folderContentDetails as $key => $item)
{
if($url === $item->url)
{
return $item;
}
if("folder" === $item->elementType)
{
return self::getPathForUrl($item->folderContent, $url, $result);
}
}
}
PS: Keeping in mind Best practices for if/return :)
I have array structure where i must get leaf.
Example
First type of array
[name] => long_desc
[values] => Array
(
[0] => Array
(
[values] => xxx
)
)
)
or
(
[name] => long_desc
[values] => Array
(
[0] => Array
(
[name] => span
[values] => Array
(
[0] => Array
(
[values] => xxx
)
)
)
How to get value what name xxx? My array have longer depth and using foreach many times not work fine. I was try recursivearrayiterator but not help.
Try array_walk_recursive() function:
function testArrayItem($item, $key)
{
if ( $item == "xxx" ) {
echo "Found xxx on key {$key}";
}
}
array_walk_recursive($array, 'testArrayItem');
EDIT:
If you want to get entire branch, which leads to the leaf you can recursively iterate through it:
function getPathToLeafRecursive(array $input, array &$branch)
{
foreach ( $input as $key => $item ) {
if ( 'xxx' == $item ) {
$branch[] = $key;
return true;
}
if ( is_array($item) ) {
$res = getPathToLeafRecursive($item, $branch);
if ( $res ) {
$branch[] = $key;
return true;
}
}
}
return false;
}
$found_branch = array();
getPathToLeafRecursive($array, $found_branch);
$found_branch = array_reverse($found_branch);
var_export($found_branch);
Here's how to find the leaf node, without depending on the name of the key. It's rather primitive and could use some OOP, but it demonstrates the basic algo:
$array = array(
array('name' => 'span',
'values' => array('values' => 'xxx', array('values' => 'yyy')),
'stuff' => '123'
)
);
$deepest_depth = 0;
$deepest_node = null;
find_leaf($array, 0);
function find_leaf($array, $current_depth) {
global $deepest_depth, $deepest_node;
do {
$current_node = current($array);
if (is_array($current_node)) {
find_leaf($current_node, $current_depth+1);
} else {
if ($deepest_node === null || $current_depth > $deepest_depth) {
$deepest_depth = $current_depth;
$deepest_node = $current_node;
}
}
next($array);
} while ($current_node !== FALSE);
}
echo $deepest_node;
What is this xxx value? Do you know the content and you just want to know that it is in the Array?
In that case you can use the RecursiveArrayIterator with the RecursiveFilterIterator.
If you want to get all "values" keys that are leafs, then you can use the RecursiveFilterIterator too, but checking for "values" that are scalar for example.
I'm trying to build a function that will apply html_entity_decode to objects and arrays. Since I don't know the structure beforehand, and the child properties can also be objects or arrays, a simple recursive function seemed to be the way to go. I'm unable to figure out why the following does not work:
function decode($data){
if(is_object($data) || is_array($data)){
foreach($data as &$value)
$value = $this->decode($value);
}
else $data = html_entity_decode($data);
return $data;
}
I have also tried the following, which doesn't work either:
function decode($data){
if(is_object($data))
$data = get_object_vars($data);
$data = is_array($data) ? array_map(array('MyClassName', 'decode'), $data) : html_entity_decode($data);
return $data;
}
Neither function has any affect on the data. What am I doing wrong?
The main issue is that you are trying to work with object like array is_object($data) || is_array($data) would not really work except you convert the object to array
Other Issues are base on incorrect variable name and not returning the proper variable You can try
$std = new stdClass();
$std->title = array("x" => "<a>XXX</a>","y" => "<b>YYY</b>");
$std->body = "<p> THis is the Body </p>";
$var = array(
"a" => "I'll \"walk\" the <b>dog</b> now",
"b" => array("<b>Hello World</b>",array(array("Yes am <strong> baba </strong>"))),
"c" => $std
);
$class = new MyClassName();
$encode = $class->encode($var); // encode
$decode = $class->decode($encode); // decode it back
print_r($encode);
print_r($decode);
Encoded Array
Array
(
[a] => I'll "walk" the <b>dog</b> now
[b] => Array
(
[0] => <b>Hello World</b>
[1] => Array
(
[0] => Array
(
[0] => Yes am <strong> baba </strong>
)
)
)
[c] => stdClass Object
(
[title] => Array
(
[x] => <a>XXX</a>
[y] => <b>YYY</b>
)
[body] => <p> THis is the Body </p>
)
)
Decoded Array
Array
(
[a] => I'll "walk" the <b>dog</b> now
[b] => Array
(
[0] => <b>Hello World</b>
[1] => Array
(
[0] => Array
(
[0] => Yes am <strong> baba </strong>
)
)
)
[c] => stdClass Object
(
[title] => Array
(
[x] => <a>XXX</a>
[y] => <b>YYY</b>
)
[body] => <p> THis is the Body </p>
)
)
See Live Demo
class MyClassName {
function encode($data) {
if (is_array($data)) {
return array_map(array($this,'encode'), $data);
}
if (is_object($data)) {
$tmp = clone $data; // avoid modifing original object
foreach ( $data as $k => $var )
$tmp->{$k} = $this->encode($var);
return $tmp;
}
return htmlentities($data);
}
function decode($data) {
if (is_array($data)) {
return array_map(array($this,'decode'), $data);
}
if (is_object($data)) {
$tmp = clone $data; // avoid modifing original object
foreach ( $data as $k => $var )
$tmp->{$k} = $this->decode($var);
return $tmp;
}
return html_entity_decode($data);
}
}
What exactly do you want to do: replace existing values with html entity decoded values or make a copy of the whole data structure with encoded values?
If you want to replace then you should pass function argument by reference:
function decode( &$data ){
If you want to make a copy -- it should work the way it is (or please explain what exactly do you mean by "the following does not work").
What about array-walk-recursive:
http://php.net/manual/en/function.array-walk-recursive.php
perhaps something like this:
function decode($data){
$data = html_entity_decode($data);
}
function decode_data($data){
if(is_object($data) || is_array($data)){
array_walk_recursive($data, 'decode');
}else{
$data = html_entity_decode($data);
}
return $data;
}
I have a multidimensional array in PHP and want to be able to search through it and find all values that are objects.
The reason I want to do this is so that when an object is found I can replace it with an array by calling an output() method on it. The output() method uses get_object_vars() to turn itself into an array, which it then returns.
Here's an example which achieves what I want manually (but only with 2 levels of depth):
// First level search...
foreach($array as $k => $v) {
// Check if it's an array.
if (is_array($v)) {
// Second level search...
foreach($v as $k2 => $v2) {
// If it's an object - convert it!
if (is_object($v2)) {
$array[$k][$k2] = $array[$k][$k2]->output();
}
}
}
// If it's an object - convert it!
if (is_object($v)) {
$array[$k] = $array[$k]->output();
}
}
Tim Cooper's answer is wrong because the function must have a parameter that is passed by reference and not by value.
php > class Foo { public function output() { return "this was an object"; } }
php > $a = array( 1 => array( 2 => array( 'foo', 'red', 1, new Foo() ) ) );
php > array_walk_recursive( $a, function( $item, $key ) {
if ( is_object( $item ) ) {
$item = $item->output();
}
} );
php > print_r( $a );
Array
(
[1] => Array
(
[2] => Array
(
[0] => foo
[1] => red
[2] => 1
[3] => Foo Object
(
)
)
)
)
Versus passing by reference:
php > array_walk_recursive( $a, function( &$item, $key ) {
if ( is_object( $item ) ) {
$item = $item->output();
}
} );
php > print_r( $a );
Array
(
[1] => Array
(
[2] => Array
(
[0] => foo
[1] => red
[2] => 1
[3] => this was an object
)
)
)
You just need a recursive function:
function objects_to_arrays_recursive (&$array) {
foreach ($array as &$member) {
if (is_object($member)) {
$member = $member->output();
}
if (is_array($member)) {
objects_to_arrays_recursive($member);
}
}
}
This will call the output() method of every object and store the result in the key that originally held the object.
Caveats:
This will loop the objects once they have been converted and also convert child objects. You may not want to do this, particularly if you have circular references as this would result in an infinte loop. It can be avoided just by changing the 2 ifs into if / elseif.
This does not check whether a given object has an output() method to call. You should probably add an is_a()/instanceof check.
This function takes its argument by reference, which means the input array will be modified. If you need to keep the original array intact, you will need to make a copy of it first.
I have this array of objects. However I want to only pull out the object which meets a certain criteria. If you look at the "post_title" key, I want to only pull out the object if it does not contain a comma. Is this possible?
So in my example below, I want to pull out the array in index 2 as that is the only one that has no comma in the "post_title" key.
Array
(
[0] => stdClass Object
(
[ID] => 504
[post_title] => Playroom (Black, Custom 2)
)
[1] => stdClass Object
(
[ID] => 503
[post_title] => Playroom (Black, Custom 1)
)
[2] => stdClass Object
(
[ID] => 252
[post_title] => Play (Black)
)
)
1
Thank you for looking at this.
Yes you can but you will have to code it yourself, like this:
$results = array();
foreach ($source as $obj) {
if (strpos($obj->post_title, ',') === false) {
$results[] = $obj;
}
}
// $results will now have the elements filtered
check array_filter function - http://php.net/manual/en/function.array-filter.php
$myArray = /*snip*/;
function titleDoesNotContainComma($object) {
return strpos($object->post_title, ',') === false;
}
$filteredArray = array_filter($myArray, 'titleDoesNotContainComma');
What have you tried? You could just foreach over the array and build a new array with the relevant results. Alternatively you could use array_filter, something like this:
function hasComma( $object ) {
if( strpos( $object->post_title, ',' ) ) {
return false;
} else {
return true;
}
}
var_dump( array_filter( $array, 'hasComma' ) );