PHP Array not populating

Posted on 2012-08-13
Last Modified: 2012-08-13

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


The array just doesn't get populated and from debugging I can see it is not being populated within the function.
Question by:rvcw
    LVL 34

    Expert Comment

    Try print_r($freeips); instead of your for loop.

    Author Comment


    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.



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

    Likewise my loop outputs:
    LVL 34

    Accepted Solution

    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);
    $freeips = showFreeIps($listIP);

    Author Closing Comment

    Many thanks, that sorted it!

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now