• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 358
  • Last Modified:

PHP Array not populating

Hello,

I have an issue with my code that I'm struggling to figure out.
Essentially, I'm trying to populate an array then return it. The array expands, but with no value.

Here is the code I have:
// input: cpanels JSON response
// output: list of unused IP addresses
function showFreeIps($ipInput) {
    
     $showFreeIps_freeIpArray = array();
    // format cPanels response and convert from object to array
    $showFreeIps_ipList = json_decode($ipInput, true);
    
    //loop through IP addresses
    for($i = 0; $i < sizeof($showFreeIps_ipList['result']); $i++) {
                       
        $showFreeIps_isUsed = $showFreeIps_ipList['result'][$i]['used'];
        $showFreeIps_ipAddr = $showFreeIps_ipList['result'][$i]['ip'];
        
        //check for an unused IP and assign to array
        if ($showFreeIps_isUsed = 0) {
            
            #$showFreeIps_freeIpArray[] = $showFreeIps_ipAddr;       
            array_push($showFreeIps_freeIpArray, $showFreeIps_ipAddr);
        }       
    }
    return $showFreeIps_freeIpArray;    
}



$freeips = array();

$listIP = executeCpanelQuery("listips");
$freeips[] = showFreeIps($listIP);

for ($j=0; $j<sizeof($freeips); $j++) {
    print $freeips[$j];
}

Open in new window


I have tried adding to the array $showFreeIps_freeIpArray using array_push and and the normal way. It doesn't seem to be getting any values.

Debugging the code shows the variable $showFreeIps_ipAddr does have the correct value and does have a value.

The resulting output of the
for ($j=0; $j<sizeof($freeips); $j++) {
    print $freeips[$j];
}

Open in new window


is:
Array

The array just doesn't get populated and from debugging I can see it is not being populated within the function.
0
rvcw
Asked:
rvcw
  • 2
  • 2
1 Solution
 
gr8gonzoConsultantCommented:
Try print_r($freeips); instead of your for loop.
0
 
rvcwAuthor Commented:
Hi,

I don't think that will make a difference. After debugging more, I found it's because my if statement is wrong (was doing if ( x = y) instead of if (x == y)).


However, now I have found my array is 2d and I don't want it to be.

var_dump($freeips);

Outputs:

array (size=1)
  0 =>
    array (size=1)
      0 => string '127.0.0.1' (length=13)

Likewise my loop outputs:
Array
0
 
gr8gonzoConsultantCommented:
print_r and var_dump will work the same for us in this situation - I was just trying to get you to display the full tree of the variable, since your "for" loop wasn't going deep enough.

Try changing:

$freeips[] = showFreeIps($listIP);
to
$freeips = showFreeIps($listIP);
0
 
rvcwAuthor Commented:
Many thanks, that sorted it!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now