allinurl Posted March 13, 2008 Share Posted March 13, 2008 Hello, I'm trying re create array 2 with the elements of array1 BUT just for the second column. and then print out the new array3. So far I have done what is below; I would appreciate so much if you guys could give a hand on this. Thanks $array1 = array(10, 200, 290); $array2 = array( array("rose", 10, 795, "test"), array("daisy", 200, 795, ""), array("orchid", 290, 795, "test") ); for ($row = 0; $row < 4; $row++) { for ($col = 0; $col < 4; $col++) { if($array2[$row][3]!=""){ echo $array3[$row][1] = $x[$col]; } } } Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 13, 2008 Share Posted March 13, 2008 I'm not exactly sure what you want. Do you want to grab the values from array2[x][2] that match array1[x] and create a new array3 with them? What do you want your final array3 to look like? Quote Link to comment Share on other sites More sharing options...
allinurl Posted March 13, 2008 Author Share Posted March 13, 2008 basically what Im trying to do is that if $array2 has an empty value then create an array3 with the same values of array2 execpt the element that has the empty value. so array3 will look lihe this: $array3 = array( array("rose", 10, 795, "test"), array("orchid", 200, 795, "test") ); Notice that the there is no more "array("daisy", 200, 795, "")" and the value of "orchid" now is 200 because that's the second value of array1 Thanks again Quote Link to comment Share on other sites More sharing options...
lemmin Posted March 13, 2008 Share Posted March 13, 2008 If I understand what you are trying to do, you should remove all of the rows that you don't want first, then remap the other column. I think you would do something like this: //Remove all of the elements that have a blank value at offset 3 for ($i=0;$i<sizeof($array2);$i++)) { if ($array2[$i][3] == "") $array2 = array_splice($array2, $i, 1); } //Remap the numbers for ($i=0;$i<sizeof($array2);$i++)) $array2[$i][1] = $array1[$i]; Quote Link to comment Share on other sites More sharing options...
allinurl Posted March 13, 2008 Author Share Posted March 13, 2008 yeah good aproach, but im not quite sure why when I print_r is giving me the element that was empty, some ideas? thanks $array1 = array(10, 200, 290); $array2 = array( array("rose", 10, 795, "test"), array("daisy", 200, 795, "test"), array("orchid", 290, 795, "") ); //echo sizeof($array2); //Remove all of the elements that have a blank value at offset 3 for ($i=0; $i < sizeof($array2); $i++){ if ($array2[$i][3] == ""){ $array2 = array_splice($array2, $i, 1); } } print_r($array2); //Remap the numbers for ($i=0; $i < sizeof($array2); $i++){ $array2[$i][1] = $array1[$i]; print_r($array2); } Quote Link to comment Share on other sites More sharing options...
allinurl Posted March 14, 2008 Author Share Posted March 14, 2008 ok so far I got the following working but now the problem is that if I need to bump down more than one item in the list, since it resizes the array each time it will screw up. what should I do? Thanks function bump_down(&$array, $index) { do { $array[$index][0] = $array[$index+1][0]; $array[$index][2] = $array[$index+1][2]; $array[$index][3] = $array[$index+1][3]; } while (isset($array[++$index + 1])); unset($array[$index]); } $array2 = array( array("rose", 10, 795, "test"), array("daisy", 200, 795, ""), array("orchid", 290, 795, "test") ); //Remove all of the elements that have a blank value at offset 3 for ($i=0; $i < sizeof($array2); $i++){ if ($array2[$i][3] == ""){ bump_down($array2, $i); } } print_r($array2); Quote Link to comment Share on other sites More sharing options...
lemmin Posted March 17, 2008 Share Posted March 17, 2008 You would have to run both of the loops in the code that I posted for each element that is removed. Quote Link to comment Share on other sites More sharing options...
tapos Posted March 17, 2008 Share Posted March 17, 2008 Hi What about this $array2 = array( array("rose", 10, 795, "test"), array("daisy", 200, 795, ""), array("orchid", 290, 795, "test") ); $array3 = $array2; $count = count($array3); for($i = 0 ; $i < $count ; $i++){ if(array_search('',$array3[$i]) !== false){ unset($array3[$i]); } } echo '<pre>'; print_r($array2); print_r($array3); echo '</pre>'; Quote Link to comment Share on other sites More sharing options...
allinurl Posted March 18, 2008 Author Share Posted March 18, 2008 Hi What about this Thanks tapos, thats very helpful to getrid of the empty ones, if you see above, Im trying to rearrange the values of the second column. "Notice that the there is no more "array("daisy", 200, 795, "")" and the value of "orchid" now is 200 because that's the second value of array1 Thanks again" 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.