Reputation: 79
I'm trying to make an array with the structure like so:
[
{
"ips":
"xxx.xxx.xxx.xx",
"yyy.yyy.yyy.yy",
"zzz.zzz.zzz.zz"
"ai":
"xxx.xxx.xxx.xx",
"yyy.yyy.yyy.yy"
}
]
So I can check if variable with string is inside array. But I'm collecting the IP's from a DB using mysql, and am not sure how to insert the IP's.
I've tried this:
$whitelist = array(
'ips' => array(),
'ai' => array()
);
// sql command
$sql = "SELECT `ip` FROM `testers`";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)) {
array_push($whitelist['ips'],
$row
);
}
Can somebody please tell me how I can do this/ Seriously so lost other then inserting the IP's individually. That would take forever and not easy to track.
Upvotes: 0
Views: 41
Reputation: 2833
It looks like your solution is really close. You just need to access the row that you retrieved from mysql as an associative array using $row['ip']
.
The below code should save the ip's from the database to the $whitelist['ips'] array and then print the results to the screen.
$whitelist = array(
'ips' =>array()
);
$sql = "SELECT `ip` FROM `testers`";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)) {
array_push($whitelist['ips'], $row['ip']);
}
var_export($whitelist);
Upvotes: 1