0

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.

1
  • 1
    That's not a valid PHP array Commented Apr 12, 2014 at 2:18

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.