Jump to content

Multi Arrays problem


Cep

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?
Link to comment
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]
Link to comment
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]

Link to comment
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).
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.