Cep Posted March 30, 2006 Share Posted March 30, 2006 Hello I have asked this before but I have reposted this in a simplified code manner so that people can understand it easier (my original code relys on too many functions etc that you wont see),What I want to do is take say 3 arrays (or more),[code]$array_1 = array(1, 2, 3);$array_2 = array(2, 4, 6);$array_3 = array(3, 6, 9);[/code]And place these into an array,[code]$bigarray = array($array_1, $array_2, $array_3);[/code]I then want to pass each one of the "sub" arrays to a function,[code]function myFunc($string, $subarray) {$newstring = $string.", ".$subarray[0].", ".$subarray[1].", ".$subarray[2];return $newstring;}[/code]And then return the $newstring value to a new array called $my_new_array.And I would do this using a loop, possibly a foreach loop?[code]$string = "a string";foreach ($bigarray as $subarray) { $my_new_array[] = myFunc($string, $subarray);}[/code]The values returned should be,[code]$my_new_array[0] = "a string, 1, 2, 3";$my_new_array[1] = "a string, 2, 4, 6";$my_new_array[2] = "a string, 3, 6, 9";[/code] The trouble is the foreach loop is not working correctly and I dont know how else to go about it.What happens is the loop only goes round for the number of subarrays (3 in this case) but does not pass its subarray values to the function, instead it is like the function reads the array as an integer. Any help? Quote Link to comment https://forums.phpfreaks.com/topic/6164-multi-arrays-problem/ Share on other sites More sharing options...
Honoré Posted March 30, 2006 Share Posted March 30, 2006 [!--quoteo(post=359957:date=Mar 30 2006, 10:57 AM:name=Cep)--][div class=\'quotetop\']QUOTE(Cep @ Mar 30 2006, 10:57 AM) [snapback]359957[/snapback][/div][div class=\'quotemain\'][!--quotec--]The values returned should be,[code]$my_new_array[0] = "a string, 1, 2, 3";$my_new_array[1] = "a string, 2, 4, 6";$my_new_array[2] = "a string, 3, 6, 9";[/code][/quote]I'm confused because I think that your code does exactly what you say it should do.You can check it yourself at [a href=\"http://www.koblix.com/test_2006_03_30.php\" target=\"_blank\"]http://www.koblix.com/test_2006_03_30.php[/a]The code used is:[code]<?phpfunction myFunc($string, $subarray) { $newstring = $string.", ".$subarray[0].", ".$subarray[1].", ".$subarray[2]; return $newstring;}$array_1 = array(1, 2, 3);$array_2 = array(2, 4, 6);$array_3 = array(3, 6, 9);$bigarray = array($array_1, $array_2, $array_3);$string = "a string";foreach ($bigarray as $subarray) { $my_new_array[] = myFunc($string, $subarray);}print_r($my_new_array);echo '<br>';echo $my_new_array[0] . '<br>';echo $my_new_array[1] . '<br>';echo $my_new_array[2] . '<br>';?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/6164-multi-arrays-problem/#findComment-22239 Share on other sites More sharing options...
Cep Posted March 30, 2006 Author Share Posted March 30, 2006 Hmm,thats really weird because thats not what I get as the output I just get things like this,[code]$my_new_array[0] = "1, 1, 1, 1";$my_new_array[1] = "2, , 4,";$my_new_array[2] = ", 3,,";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/6164-multi-arrays-problem/#findComment-22246 Share on other sites More sharing options...
Honoré Posted March 30, 2006 Share Posted March 30, 2006 Weird yes. And it probably is not a problem with PHP version neither as I used PHP 4.4.1 on FreeBSD 5.3 and also PHP 5.1.2 on Windows XP.Both give the expected result.What PHP version do you use? Quote Link to comment https://forums.phpfreaks.com/topic/6164-multi-arrays-problem/#findComment-22248 Share on other sites More sharing options...
Cep Posted March 30, 2006 Author Share Posted March 30, 2006 [!--quoteo(post=359968:date=Mar 30 2006, 11:25 AM:name=Honoré)--][div class=\'quotetop\']QUOTE(Honoré @ Mar 30 2006, 11:25 AM) [snapback]359968[/snapback][/div][div class=\'quotemain\'][!--quotec--]Weird yes. And it probably is not a problem with PHP version neither as I used PHP 4.4.1 on FreeBSD 5.3 and also PHP 5.1.2 on Windows XP.Both give the expected result.What PHP version do you use?[/quote]I use PHP 4.4.1 with MySQL 4.1.13 on a linux (sorry cant pull down the server info from my current location). Quote Link to comment https://forums.phpfreaks.com/topic/6164-multi-arrays-problem/#findComment-22250 Share on other sites More sharing options...
Guest footballkid4 Posted March 30, 2006 Share Posted March 30, 2006 Well, when you use modifiers such as [] on arrays, it will return the array key stored inside the brackets. If you use them on a string, they will return the first amount of characters (inside the brakcets) of the string. That may explain your problem...but now on to the solution:[code]<?phpfunction myFunc( $str , $arr ){ $arr_str = implode( "," , $arr ); return "a string, " . $arr_str;}$subarray[] = array( 1 , 2 , 3 );$subarray[] = array( 2 , 3 , 4 );$subarray[] = array( 3 , 4 , 5 );$bigarray = array();$bigarray = array_merge( $bigarray , $subarray );print_r( $bigarray );foreach ( $bigarray as $subarr ){ echo myFunc( "" , $subarr ) . "<br />";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/6164-multi-arrays-problem/#findComment-22294 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.