Jump to content


Photo

Multi Arrays problem


  • Please log in to reply
5 replies to this topic

#1 Cep

Cep
  • Members
  • PipPipPip
  • Advanced Member
  • 539 posts
  • LocationOn a boat in the middle of nowhere

Posted 30 March 2006 - 08:57 AM

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),

$array_1 = array(1, 2, 3);
$array_2 = array(2, 4, 6);
$array_3 = array(3, 6, 9);

And place these into an array,

$bigarray = array($array_1, $array_2, $array_3);

I then want to pass each one of the "sub" arrays to a function,

function myFunc($string, $subarray) {

$newstring = $string.", ".$subarray[0].", ".$subarray[1].", ".$subarray[2];

return $newstring;
}

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?

$string = "a string";

foreach ($bigarray as $subarray) {
     $my_new_array[] = myFunc($string, $subarray);
}

The values returned should be,

$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";

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?
Thanks Cep

#2 Honoré

Honoré
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationAntwerp - Belgium

Posted 30 March 2006 - 09:12 AM

[!--quoteo(post=359957:date=Mar 30 2006, 10:57 AM:name=Cep)--][div class=\'quotetop\']QUOTE(Cep @ Mar 30 2006, 10:57 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
The values returned should be,
$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";
[/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:
<?php
function 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>';
?>


#3 Cep

Cep
  • Members
  • PipPipPip
  • Advanced Member
  • 539 posts
  • LocationOn a boat in the middle of nowhere

Posted 30 March 2006 - 10:12 AM

Hmm,

thats really weird because thats not what I get as the output I just get things like this,
$my_new_array[0] = "1, 1, 1, 1";
$my_new_array[1] = "2, , 4,";
$my_new_array[2] = ", 3,,";


Thanks Cep

#4 Honoré

Honoré
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationAntwerp - Belgium

Posted 30 March 2006 - 10:25 AM

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?

#5 Cep

Cep
  • Members
  • PipPipPip
  • Advanced Member
  • 539 posts
  • LocationOn a boat in the middle of nowhere

Posted 30 March 2006 - 10:32 AM

[!--quoteo(post=359968:date=Mar 30 2006, 11:25 AM:name=Honoré)--][div class=\'quotetop\']QUOTE(Honoré @ Mar 30 2006, 11:25 AM) View Post[/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).
Thanks Cep

#6 Guest_footballkid4_*

Guest_footballkid4_*
  • Guests

Posted 30 March 2006 - 02:18 PM

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:
<?php
function 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 />";
}
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users