Jump to content

[SOLVED] Empty array: Array ( )


Thomisback

Recommended Posts

Hi,

 

I have made a small PHP script, the problem is that after I run it the array is still empty.

But if I print_r the array inside the function it does work and prints one value.

 

<?php

function test_function($parameter){
$thiz[] = "testvalue"; 
    return(1);

}

$thiz = array();
$this_array = array();
$this_array[] = "value1";
$this_array[] = "value2";

foreach ($this_array as $parameter){
test_function($parameter);
}

    print_r($thiz);
?>

 

Kind regards

Link to comment
Share on other sites

Try the below code:

<?php
$thiz = array();
$this_array = array();
$this_array[] = "value1";
$this_array[] = "value2";

foreach ($this_array as $compare){
$thiz[] = "testvalue";
}

print_r($thiz);

?>

The reason why you couldn't retrieve the values from what I can see is that functions cannot call variable/array values from outside the function. So if you have a variable and you want to pass it to a function, you can 1, use the function brackets or 2, use sessions. But the easiest thing to do in your case was just to remove the entire function since it did not contain much.

Hope that helps.

Link to comment
Share on other sites

<?php
function test_function($parameter)
{
    global $thiz;

    $thiz[] = $parameter;

    return;
}

$this_array = array();
$this_array[] = "value1";
$this_array[] = "value2";

foreach ($this_array as $parameter){
    test_function($parameter);
}

print_r($thiz);
?>

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.