Jump to content

Archived

This topic is now archived and is closed to further replies.

Cep

Multi Arrays problem

Recommended Posts

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?

Share this post


Link to post
Share on other sites
[!--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]
<?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>';
?>
[/code]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
[!--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).

Share this post


Link to post
Share on other sites
Guest footballkid4
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]<?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 />";
}
?>[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.