pobri19 Posted July 5, 2010 Share Posted July 5, 2010 Hi guys, I need to write an algorithm for manipulating arrays. Basically the algorithm needs to unset arrays of arrays of arrays if all their members have no values, and so fourth. The reason for this is json encoded request I'll be sending to a server will contain invalid data if the values are empty (using json_decode()), here is an example: array: array1 => array() => testmember, testvalue testmember2, testmember3, array2 => array() => testmember, testvalue1 testmember2, testvalue2 array3 => array() => array() => testmember1, testvalue1 testmember2, testvalue2 (sorry for the bad formatting) Any who, basically the loop needs to loop through every member of that array (whether it be an array of an array of an array and so fourth) and unset the top array if none of the proceeding members have values, an example of that would be: array1() => array2() => array3() => member1, member1, member2, member3, None of these have values, so obviously it would need to figure out that it doesn't need any of the sub arrays and unset array2(), however if it was like this: array1() => array2() => array3() => member1, member1, value1 member2, member3, array1=>member1 has a value therefore it would need to unset: All of array2 and its subarrays/members But not unset array1=>member1 It's hard to explain what I'm after, but anyway I can't think of a solution to this... Surely there has to be some simple recursive solution for this... Thoughts? Thanks for your time. Link to comment https://forums.phpfreaks.com/topic/206731-how-to-write-this-algorithm/ Share on other sites More sharing options...
kenrbnsn Posted July 5, 2010 Share Posted July 5, 2010 We don't write code here, we help you with code you've already written. Have you tried to write this? Ken Link to comment https://forums.phpfreaks.com/topic/206731-how-to-write-this-algorithm/#findComment-1081164 Share on other sites More sharing options...
pobri19 Posted July 5, 2010 Author Share Posted July 5, 2010 Hi Ken, I was just after pointers, not code And yes, I've written it, appears to be working: public function RemoveEmptyValues($array) { for ($i = 0; $i < count($array); $i++) { if (is_array($array[$i])) { if (count($array[$i]) == 0) { unset($array[$i]); } else { $tmpArray = $array[$i]; $array[$i] = $this->RemoveEmptyValues(array_values($array[$i])); } } else { if ($array[$i] == "") { unset($array[$i]); } } } return $array; } It does what I was after: However there's a problem, I had to convert the array to a number indexed array opposed to an associative array to do looping etc, but the problem is the associative array names are the field names that are needed in the JSON request, therefore I need them back whilst maintaining the new array structure. It doesn't seem plausible that there is a way to convert an array BACK to an associative array from an array_values call, so can anyone think of a way in which I can keep the new structure whilst keeping the associative array index names (and preferably replacing the numbered indexes with their original associative array index counterparts)? A better solution would be being able to index the array as both an indexed array and an associative array, that way I don't need to convert it with array_values, and I can do the looping whilst keeping the associative index names. Is this possible? Cheers Link to comment https://forums.phpfreaks.com/topic/206731-how-to-write-this-algorithm/#findComment-1081171 Share on other sites More sharing options...
kenrbnsn Posted July 5, 2010 Share Posted July 5, 2010 You can loop though an associative array using the foreach loop. Ken Link to comment https://forums.phpfreaks.com/topic/206731-how-to-write-this-algorithm/#findComment-1081174 Share on other sites More sharing options...
pobri19 Posted July 5, 2010 Author Share Posted July 5, 2010 Why didn't I think of that... Thanks Ken! Link to comment https://forums.phpfreaks.com/topic/206731-how-to-write-this-algorithm/#findComment-1081178 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.