rvcw
asked on
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:
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
is:
Array
The array just doesn't get populated and from debugging I can see it is not being populated within the function.
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];
}
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];
}
is:
Array
The array just doesn't get populated and from debugging I can see it is not being populated within the function.
Try print_r($freeips); instead of your for loop.
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Many thanks, that sorted it!