简体   繁体   中英

Joining two arrays together

I'll start with the code:

$checkIpaddresses = $wpdb->get_results( $wpdb->prepare("SELECT affiliateID, source FROM am_ipaddress WHERE affiliateID = $affiliateID"));

foreach ($checkIpaddresses as $ipaddress) { 
    $ipSource = explode(",", $ipaddress->source);
}

$newIP = ",".$ipAddress;
array_push($newIP, $ipSource);

print_r($ipSource);

I have a column in a database with a list of IP addresses seperated by , (comma). I want to be able to get that list from the database, add in a new IP address and update the column with the new list of IP addresses.

The above code is giving this error. array_push() expects parameter 1 to be array .

The value of $ipAddress is $_SERVER['REMOTE_ADDR']; .

First off, if this is returning only one row, you don't need a foreach .

$ipSource = explode(",", $checkIpaddresses[0]->source);  // Get the 1st row as an array

Second, you are over-complicating adding the new IP address to the array.

$ipSource[] = $ipAddress;

That's it. Now the $ipSource array contains the new IP address.

(PS Use implode(',', $ipSource) to turn the array into a comma-separated string)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM