help_me_with_php Posted December 27, 2012 Share Posted December 27, 2012 (edited) all, can someone give me a push here (no pun intended)? Once again I can't figure out incredibly elementary issues because PHP is smart and has more than one option for many coding techniques. I'm trying to print all the elements in an array that are filled dynamically, but apparently I'm missing a line of code. the base 0 index is being overwritten instead of being pushed to the next int. here are 2 small scripts I've tried but both output the same thing, which is below the code blocks: <?php $arr = array(1,4,17,-9); test_arrayKeys_function($arr); function test_arrayKeys_function($inArray) { $Indicies = array(); $Indicies[] = array_keys($inArray, $inArray[0]); $Indicies[] = array_keys($inArray, $inArray[1]); $Indicies[] = array_keys($inArray, $inArray[2]); $Indicies[] = array_keys($inArray, $inArray[3]); print_r($Indicies); } ?> <?php $arr = array(1,4,17,-9); test_arrayKeys_function($arr); function test_arrayKeys_function($inArray) { $Indicies = array(); $Indicies[] = array_keys($inArray, $inArray[0]); array_push($Indicies, array_keys($inArray, $inArray[1])); array_push($Indicies, array_keys($inArray, $inArray[2])); array_push($Indicies, array_keys($inArray, $inArray[3])); print_r($Indicies); } ?> the output is always: Array ( [0] => Array ( [0] => 0 ) [1] => Array ( [0] => 1 ) [2] => Array ( [0] => 2 ) [3] => Array ( [0] => 3 ) ) Crutch anyone? thanks. Edited December 27, 2012 by help_me_with_php Quote Link to comment Share on other sites More sharing options...
Andy123 Posted December 27, 2012 Share Posted December 27, 2012 I am not entirely sure what you are trying to accomplish here. I will assume that you want an array containing the keys in $inArray. The array_keys function returns an array where the values are the keys from the input array. In your case, you are getting arrays back containing the keys at which the value is 1, 4, 17 or -9. Again, I am not certain what you are looking for, but if you want an array with all of the keys, then you can just do like this: $Indicies = array_keys($inArray); If this is not what you were after, then please elaborate. Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 27, 2012 Share Posted December 27, 2012 Same here. The results from your functions above are exactly as I would expect based upon your input. And, what you are doing is probably the most inefficient manner in which to do that. But, apparently that is not what you are trying to do anyway. So, provide an example of the input array and what you want for the output. Quote Link to comment Share on other sites More sharing options...
silkfire Posted December 27, 2012 Share Posted December 27, 2012 What exactly are you trying to achieve, i.e. for what purpose are you doing this? Quote Link to comment Share on other sites More sharing options...
help_me_with_php Posted December 27, 2012 Author Share Posted December 27, 2012 (edited) $Indicies = array_keys($inArray); this returns: Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 ) my question on this one would be...why is it necessary to print out redundancies like this?? the ints in [ ] are the indicies, but the output the same as the index ints. I see exactly why but man that's confusing as he$$. excuse the reference there. That output kind of makes it seem pointless to print something like this. But I guess there would never be a need to anyway. Now to what I'm trying to do: Trying to get a function to take an input array, run some code and fill a dynamic array with index values from any given value in the input Array (when looping through values of the input array). The function would return the dynamic array that is full of specific index ints that were captured inside any given iteration of the loop. The kicker => I don't want to reject the function call if there are dups in the input array. But see, if there is, code like this, if in the function, would not be reliable: array_keys($array(), $value) because there is more than one value to be referenced. wouldn't the above code return the first occurance of $value?? If so, what if the loop is processing the second occurrance of $value?? There is nothing to achieve in these tests other than trying to write the most iterative code I can to produce the inevitable function I need out of this whole thing. I don't care at all about efficiency because what I do is not on a large enough scope to catch the attention of the folks obsessed with energy efficiency and the "green movement". So in other words, "I don't count pennies". the conditionals and loops and such inside the function I need I can write myself (I would hope anyway!). Loosely-typed languages are a huge problem for me because I don't like giving any more control to scripts than absolutely necessary. Not to mention the fact that I always want to know what's going on. But that's the way it is here, so it's not a big deal. thanks guys. Edited December 27, 2012 by help_me_with_php Quote Link to comment Share on other sites More sharing options...
Jessica Posted December 27, 2012 Share Posted December 27, 2012 That made. No. Sense. Quote Link to comment Share on other sites More sharing options...
help_me_with_php Posted December 27, 2012 Author Share Posted December 27, 2012 (edited) That made. No. Sense. it may not to many developers who understand IDE's on a very low level. I do, but I also deal with people who can't even turn on the machine. Try bridging that gap. It's tough, at the very least. Edited December 27, 2012 by help_me_with_php Quote Link to comment Share on other sites More sharing options...
help_me_with_php Posted December 27, 2012 Author Share Posted December 27, 2012 here's a question for anyone. This is what the function has to do, ultimately. And it's probably not tough at all: a function that takes a list of X integers and returns a list of indices that add up to Y. Example: input: ([1,4,17,-9], Y=5) output: [[0,1]] function tester($inArray, $outSummation) { //code } make sense? Quote Link to comment Share on other sites More sharing options...
Barand Posted December 27, 2012 Share Posted December 27, 2012 This seems very reminiscent of an earlier thread http://forums.phpfreaks.com/topic/271981-function-for-an-output-of-indicies/ where you were asking me to write the code for you. Quote Link to comment Share on other sites More sharing options...
silkfire Posted December 27, 2012 Share Posted December 27, 2012 Why didn't you say this in the first place. You're trying to solve a classic problem - choose a certain number Y, and then let the algorithm find out which numbers from a list Z add up to and result in the number Y. Try this solution, the first answer: http://stackoverflow.com/questions/2667664/php-find-two-or-more-numbers-from-a-list-of-numbers-that-add-up-towards-a-given Quote Link to comment Share on other sites More sharing options...
help_me_with_php Posted December 28, 2012 Author Share Posted December 28, 2012 This seems very reminiscent of an earlier thread http://forums.phpfre...ut-of-indicies/ where you were asking me to write the code for you. Hi Barand. I'm gathering that you've been at this for a while? No, I'm not asking people to write anything. I never do that. Did I? So the issue here seems to be that there is no function in PHP that returns what I want. There are ways to do this, but it would look like crap, to be honest. the issue also is that I'm so out of touch with writing any code nowadays because code writing is less important now by humans than it was in the past. I'm more systems and hardware now but I was a programmer to start out. If I have the capital, it seems to me to be a better investment to buy code rather than write it yourself. That's all, Barand. I'm sure you're aware of this reality as well. Programmers are still needed of course, but not as much as they were before the latest wave of automation was ushered in. Quote Link to comment Share on other sites More sharing options...
help_me_with_php Posted December 28, 2012 Author Share Posted December 28, 2012 (edited) Why didn't you say this in the first place. You're trying to solve a classic problem - choose a certain number Y, and then let the algorithm find out which numbers from a list Z add up to and result in the number Y. Try this solution, the first answer: http://stackoverflow...towards-a-given Silk, You do realize that I want the associative INDEXES of the array values, don't you? take a look at the response in that thread that starts with "i don't think the answer isn't as easy as nik mentioned. let's ay you have the following numbers:" He's right, I believe. Here's the script I have compiled so far, and it actually does execute properly. But like that guy says, it iterates to find every combination (or permutation, whatever) that's possible. But this code uses bitwise to ask the machine what's up (your responder on stackoverflow calls it "big magic"). Check this out, this is what I have now: function Sum_Of_Indicies($inArray, $indexSum) { $retVal = 0; //bit indicator of success //array count $count = count($inArray); //The total number of possible combinations $total = pow(2, $count); //2^number of elements (factorial) //Loop through each possible combination (base 0) for ($i = 0; $i < $total; $i++) { $Indicies = array(); //stores associative indicies of found values. $ctrIndex = 0; //counter for indicies stored //For each combination for ($j = 0; $j < $count; $j++) { //Is bit $j set in $i? if (pow(2, $j) & $i) { $Indicies[] = array_keys($inArray, $inArray[$j]); // ****(UNSURE)**** $ctrIndex = $ctrIndex + 1; } // ****(UNSURE)**** } //Sum of indicies = $outIndicies? ****(UNSURE)**** if ($indexSum = array_sum($Indicies)) { echo $j.'<br>'; } } } the code with "****(UNSURE)****" is what I'm unsure about. see what I'm asking now? Edited December 28, 2012 by help_me_with_php Quote Link to comment Share on other sites More sharing options...
Barand Posted December 28, 2012 Share Posted December 28, 2012 No, I'm not asking people to write anything. I never do that. Did I? Looks to me like that's what you were asking Barand, Let me just ask you this: what would you personally write in PHP that takes 2 inputs (X, Y): 1) an array of integers (X) and 2) a summation target value (Y). and then produces one output: an array / list of indicies that sum to input #2 (Y). Quote Link to comment Share on other sites More sharing options...
help_me_with_php Posted December 28, 2012 Author Share Posted December 28, 2012 Looks to me like that's what you were asking OK, then you got me. I didn't remember and didn't look. If you don't want to do it, don't do it. I'll get it eventually. If you have FTE in your own life, don't waste the time. Work is stressful enough as it is. I'll take the code if I can get it, but if not I'll pound it out even if I have to write 100 lines that manually spells out recursion to the recipient. As you can see, there's only a couple lines left that have to be modified. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted December 28, 2012 Share Posted December 28, 2012 As I alluded to in your previous thread, your troubles does not stem from a lack of functions (or premade code). This is a solved problem, and a quite basic one at that. Your troubles stems from a lack of understanding the problem itself, and how to properly solve it. Trying to solve a problem at the same time as you're trying to translate said solution into code is a sure-fire way to overly complicate things, and confuse yourself. I recommend sitting down with a piece of paper, a pen, and think logically through how you'd solve it without any computers involved. Also, keep it as simple as possible, with as few steps as possible. Once you've done that, you'll find that the actual coding becomes trivial. Quote Link to comment Share on other sites More sharing options...
help_me_with_php Posted December 28, 2012 Author Share Posted December 28, 2012 Your troubles stems from a lack of understanding the problem itself, and how to properly solve it. Of course it does. I've said a million times I'm not trained in this area. Thanks for the input. Once you've done that, you'll find that the actual coding becomes trivial. of course it is. Code doesn't mean anything really. The only purpose IDE's have ever served was to make it easier for humans to tell machines what to do so they don't have to write out millions of bit indicators. So what? thanks again, but the lecture kinda ticked me off. Re-iterating the obvious isn't cool bud, but I appreciate the fact that you're obviously a competent professional in the field. I'll end the thread here. thanks for all the replies guys. Quote Link to comment Share on other sites More sharing options...
Jessica Posted December 28, 2012 Share Posted December 28, 2012 IDE... Quote Link to comment Share on other sites More sharing options...
silkfire Posted December 28, 2012 Share Posted December 28, 2012 Yeah if you even bothered to look at the solution. Here's some code for you: function array_sum_parts($n,$t,$all=false) { $count_n = count($n); // how much fields are in that array? $count = pow(2,$count_n); // we need to do 2^fields calculations to test all possibilities # now i want to look at every number from 1 to $count, where the number is representing # the array and add up all array-elements which are at positions where my actual number # has a 1-bit # EXAMPLE: # $i = 1 in binary mode 1 = 01 i'll use ony the first array-element # $i = 10 in binary mode 10 = 1010 ill use the secont and the fourth array-element # and so on... the number of 1-bits is the amount of numbers used in that try for($i=1;$i<=$count;$i++){ // start calculating all possibilities $total=0; // sum of this try $anzahl=0; // counter for 1-bits in this try $k = $i; // store $i to another variable which can be changed during the loop for($j=0;$j<$count_n;$j++){ // loop trough array-elemnts $total+=($k%2)*$n[$j]; // add up if the corresponding bit of $i is 1 $anzahl+=($k%2); // add up the number of 1-bits $k=$k>>1; //bit-shift to the left for looking at the next bit in the next loop } if ($total==$t) { $loesung[$i] = $anzahl; // if sum of this try is the sum we are looking for, save this to an array (whith the number of 1-bits for sorting) if (!$all) { break; // if we're not looking for all solutions, make a break because the first one was found } } } asort($loesung); // sort all solutions by the amount of numbers used // formating the solutions to getting back the original array-keys (which shoud be the return-value) foreach($loesung as $val=>$anzahl) { $bit = strrev(decbin($val)); $total = 0; $ret_this = array(); for($j=0; $j < strlen($bit); $j++) { if ($bit[$j] == '1') { $ret_this[] = $j; } } $ret[]=$ret_this; } return $ret; } $x = array(1, 4, 17, -9); $y = 5; var_dump(array_pop(array_sum_parts($x, $y))); Result: array (size=2) 0 => int 0 1 => int 1 Quote Link to comment Share on other sites More sharing options...
help_me_with_php Posted December 28, 2012 Author Share Posted December 28, 2012 IDE... OK I'll bite once more...what does it mean Ms. Jessica?? Quote Link to comment Share on other sites More sharing options...
help_me_with_php Posted December 28, 2012 Author Share Posted December 28, 2012 Silk, Interesting technique. Different thoughts with the modulus usage. Yes the code returns the correct result. Thank you. Didn't have to, but thanks. I'll use it and modify it to be shorter. Hope you had fun with it. Quote Link to comment Share on other sites More sharing options...
kicken Posted December 28, 2012 Share Posted December 28, 2012 $Indicies = array_keys($inArray); this returns: Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 ) my question on this one would be...why is it necessary to print out redundancies like this?? It's not outputting any redundancies, it might just look that way because of what you're asking it to do. Your initial array looks like this: array( 0 => 1 1 => 4 2 => 17 3 => -9 ) Notice your index values are 0-3. Now when you run the function array_keys() you're asking PHP to tell you what the index values of the given array are. PHP returns these indexes in a new simple list array. That new array has indexes 0-n where n is the number of items contained in the source array. The values in the new array are the indexes from the source array. Since the indexes from your source array are just a simple 0-3, you end up with your seemingly redundant array output of array( 0 => 0 1 => 1 2 => 2 3 => 3 ) If you had a source array with varying indexes, you might better see what is going on and why you get what you are getting. Eg, given a source array of: array( 'a' => 1 'b' => 4 0 => 17 1 => -9 ) running array_keys($source) would result in a returned array that looks like: array( 0 => 'a' 1 => 'b' 2 => 0 3 => 1 ) OK I'll bite once more...what does it mean Ms. Jessica?? Itegrated Development Evironment An IDE is all about making it easier for a person to write code and fix bugs in their software. It does things like provide autocompletion suggestions as you write code, allow you to quickly reference any relevant documentation for the function you're trying to use, manage and organize your applications source files and easily search/replace within them, provide integrated debugging tools to let you run through your code and view(maybe even change) the values of variables as it runs, and much more usually. They don't really have anything to do with making it easier to tell a computer what to do (except maybe in a round-about way by helping the human). Programming languages themselves (ie, C, PHP, Java, etc) are what allow humans to tell a computer what to do easier. The IDE's just help you write the code for a given language by consolidating all the tools you might use in one easy to use package. Quote Link to comment Share on other sites More sharing options...
help_me_with_php Posted December 28, 2012 Author Share Posted December 28, 2012 (edited) It's not outputting any redundancies, it might just look that way because of what you're asking it to do. Your initial array looks like this: array( 0 => 1 1 => 4 2 => 17 3 => -9 ) Notice your index values are 0-3. Now when you run the function array_keys() you're asking PHP to tell you what the index values of the given array are. PHP returns these indexes in a new simple list array. That new array has indexes 0-n where n is the number of items contained in the source array. The values in the new array are the indexes from the source array. Since the indexes from your source array are just a simple 0-3, you end up with your seemingly redundant array output of array( 0 => 0 1 => 1 2 => 2 3 => 3 ) If you had a source array with varying indexes, you might better see what is going on and why you get what you are getting. Eg, given a source array of: array( 'a' => 1 'b' => 4 0 => 17 1 => -9 ) running array_keys($source) would result in a returned array that looks like: array( 0 => 'a' 1 => 'b' 2 => 0 3 => 1 ) Itegrated Development Evironment An IDE is all about making it easier for a person to write code and fix bugs in their software. It does things like provide autocompletion suggestions as you write code, allow you to quickly reference any relevant documentation for the function you're trying to use, manage and organize your applications source files and easily search/replace within them, provide integrated debugging tools to let you run through your code and view(maybe even change) the values of variables as it runs, and much more usually. They don't really have anything to do with making it easier to tell a computer what to do (except maybe in a round-about way by helping the human). Programming languages themselves (ie, C, PHP, Java, etc) are what allow humans to tell a computer what to do easier. The IDE's just help you write the code for a given language by consolidating all the tools you might use in one easy to use package. LOL. Thanks. I have a MUCH wider view of all things involved in business scenarios than many others here, but that's my gift I suppose. I always love getting answers like this because it's so exact and to the point. Just like a professional developer should be. And even a reference to a wiki article?? That's hilarious! If I had a hiring need at the moment Kicken, you'd certainly be on my list of prospectives. I know what an IDE is and I understood the outputs. You and Jessica seem to be one in the same. As a matter of fact, both of you could be on a list of prospective hires. Edited December 28, 2012 by help_me_with_php Quote Link to comment Share on other sites More sharing options...
Barand Posted December 28, 2012 Share Posted December 28, 2012 "What would I write?", you asked function getIndices($array, $target, &$results, $indSoFar=array(), $totSoFar=0) { foreach ($array as $k => $v) { $inds = array_merge($indSoFar, array($k)); $t = $totSoFar + $v; if ($t == $target) { sort($inds); if (!in_array($inds, $results)) // ensure only unique combinations $results[] = $inds; return; } $a = $array; unset($a[$k]); getIndices($a, $target, $results, $inds, $t); } } $input = array(2, 4, 17, -11); $target = 12; $results = array(); // call function getIndices($input, $target, $results); // print results echo "Target: $target "; foreach ($results as $res) { echo '(' . join(',', $res) . ') '; } /***** results ******** * Target: 6 (0,1) (2,3) * Target: 12 (0,1,2,3) ***********************/ Quote Link to comment Share on other sites More sharing options...
Christian F. Posted December 28, 2012 Share Posted December 28, 2012 Thanks again, but the lecture kinda ticked me off. Re-iterating the obvious isn't cool bud, but I appreciate the fact that you're obviously a competent professional in the field. My reply was not intended to be a lecture, but the best possible advice I could give on how you could become a more competent programmer yourself. In addition to the best manner, that I could see, on how to proceed to solve the problem on your own. I'd also like to refer you to this post I just made in reply to another thread, which should (hopefully) help to clear up some confusion. Quote Link to comment Share on other sites More sharing options...
Jessica Posted December 28, 2012 Share Posted December 28, 2012 I have a MUCH wider view of all things involved in business scenarios than many others here That's relevant...and probably true. Good for you. /s Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.