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
https://forums.phpfreaks.com/topic/141430-solved-empty-array-array/
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.

<?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);
?>

Archived

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

×
×
  • 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.