Asked  7 Months ago    Answers:  5   Viewed   49 times

The following code:

$string = "1,2,3"
$ids = explode(',', $string);
var_dump($ids);

Returns the array:

array(3) {
  [0]=>
  string(1) "1"
  [1]=>
  string(1) "2"
  [2]=>
  string(1) "3"
}

I need for the values to be of type int instead of type string. Is there a better way of doing this than looping through the array with foreach and converting each string to int?

 Answers

53

You can achieve this by following code,

$integerIDs = array_map('intval', explode(',', $string));
Wednesday, March 31, 2021
 
Powering
answered 7 Months ago
77

Use PHP's explode.

$str = "1,2,3,4,5,6";
$arr = explode("," $str); // array( '1', '2', '3', '4', '5', '6' );

foreach ($arr AS $index => $value)
    $arr[$index] = (int)$value; 

// casts each value to integer type -- array( 1, 2, 3, 4, 5, 6 );

As suggested by Tim Cooper, using array_walk is simpler than the above loop:

array_walk($arr, 'intval');
Wednesday, March 31, 2021
 
Magnanimity
answered 7 Months ago
89

Try preg_split.

$exploded = preg_split('@/@', '1/2//3/', NULL, PREG_SPLIT_NO_EMPTY);

Wednesday, June 2, 2021
 
Raef
answered 5 Months ago
81

You're looking for Integer/parseInt.

user=> (map #(Integer/parseInt %) ["1" "2" "3" "4"])
(1 2 3 4)

You have to wrap Integer/parseInt in an anonymous function because Java methods aren't functions.

read-string would also work in this case:

user=> (map read-string ["1" "2" "3" "4"])
(1 2 3 4)

read-string reads any object from a string, not just integers. So, if you did (read-string "1.0") you'd get back a double. When reading from outside sources, it's usually better to limit what can be read to precisely what you need, which is an integer in this case. Therefore, I recommend using my first example.

Tuesday, August 3, 2021
 
redrom
answered 3 Months ago
26

There is no built-in method as far as I know. You have to iterate over the returned pointer array, converting C strings to Swift Strings, until a nil pointer is found:

if var ptr = f() {
    var strings: [String] = []
    while let s = ptr.pointee {
        strings.append(String(cString: s))
        ptr += 1
    }
    // Now p.pointee == nil.

    print(strings)
}

Remark: Swift 3 uses optional pointers for pointers that can be nil. In your case, f() returns an implicitly unwrapped optional because the header file is not "audited": The compiler does not know whether the function can return NULL or not.

Using the "nullability annotations" you can provide that information to the Swift compiler:

const char * _Nullable * _Nullable f(void);
// Imported to Swift  as
public func f() -> UnsafeMutablePointer<UnsafePointer<Int8>?>?

if the function can return NULL, and

const char * _Nullable * _Nonnull f(void);
// Imported to Swift  as
public func f() -> UnsafeMutablePointer<UnsafePointer<Int8>?>

if f() is guaranteed to return a non-NULL result.

For more information about the nullability annotations, see for example Nullability and Objective-C in the Swift blog.

Thursday, October 14, 2021
 
Serge Ballesta
answered 3 Days ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :