Forum Moderators: coopster

Message Too Old, No Replies

Function, array and array_push

Problem with return

         

tomda

2:42 pm on Jul 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I spend the last three hours on this one.
I am sure it is a simple one!


// THE FUNCTION ITSELF
// *******************
function mod_array ($var) {
echo "<br>".$var[0]."<br>";
// ADD "CHOCO" IF CAKE
if ($var[0]=="cake") {$var[]="choco";}
return $var;}

// ARRAY
// *****
$text_array=array("cake", "sweet");
$nb=count($text_array);

// LOOP & FUNCTION CALL
// ********************
for($i=0; $i<$nb; $i++){
// MAKE A NEW ARRAY FOR EACH ELEMENT OF THE ARRAY
$new_array=array($text_array[$i]);

// PRINT NEW ARRAY AFTER THE FUNCTION IS CALL
// THIS METHOD DOESN'T WORK
mod_array ($new_array); print_r ($new_array);

// THIS METHOD WORKS
// print_r (mod_array($new_array));
}

Any idea why?
Any help would really appreciated.
Thank you

gliff

3:08 pm on Jul 4, 2005 (gmt 0)

10+ Year Member



It helps immensely if you state

1. What you expected to happen

2. What actually happened

From what you've provided, here's my stab in the dark.

mod_array ($new_array); will not alter the contents of $new_array. mod_array only returns a new array, it doesn't modify the variable you pass to it. So, you need to say

$new_array = mod_array($new_array);

If you want to make your mod_array function modify the variable that you pass to it (like the built in array sort functions), you must pass the value by reference. You can do that by using the & symbol in your function declaration.

function mod_array (& $var) {...}

This can often have unintended consequences, and I wouldn't use it until you understand how PHP works a little better.

bennymack

3:14 pm on Jul 4, 2005 (gmt 0)

10+ Year Member




I am sure it is a simple one!

You are correct. This is a simple one. There's a couple different ways to fix it.

Change:


function mod_array ($var) {
echo "<br>".$var[0]."<br>";
// ADD "CHOCO" IF CAKE
if ($var[0]=="cake") {
$var[]="choco";
}
return $var;
}

To:


function mod_array (&$var) {
echo "<br>".$var[0]."<br>";
// ADD "CHOCO" IF CAKE
if ($var[0]=="cake") {
$var[]="choco";
}
}

Note the &$var in the function declaration. So now it's taking a reference to the array so any changes to that array are reflected in the array after the function call.

Or leave the function and change:


mod_array ($new_array);

To:


$new_array = mod_array ($new_array);

Hopefully this saves you three more hours. Enjoy!

mcibor

3:37 pm on Jul 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This didn't work because of two tiny mistakes. In loop
// MAKE A NEW ARRAY FOR EACH ELEMENT OF THE ARRAY
you have overwritten the variable
moreover you have a double array, and your mod function doesn't recognise that.

As I suppose this is the corrected version: (function is correct)

<?php
...
$text_array=array("cake", "sweet");
$nb=count($text_array);

// LOOP & FUNCTION CALL
// ********************
for($i=0; $i<$nb; $i++){
// MAKE A NEW ARRAY FOR EACH ELEMENT OF THE ARRAY
$new_array[]] = mod_array(array($text_array[$i]));//don't rewrite the new_array and mod_array needs argument that is single array, not double.
}
// PRINT NEW ARRAY AFTER THE FUNCTION IS CALL
print_r ($new_array);
?>


Hope this works (I didn't test it:) ).
Michal Cibor

tomda

5:56 am on Jul 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Gliff, Bennymack & Mcibor, it is fixed now thanks to your help...
Gliff - I promise that next week I will say what happened and what should happen.

tomda

6:41 am on Jul 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oups, sorry!
I meant "next time" not "next weeek"...
Anyway, I 'll be on leave next week :)