0

Is there any way to pass string variable to array() function:

$ints = '112233,112234';
$arr = array($ints);

//instead of

$arr = array(112233,112234);

I know that there are plenty other ways, but passing the string is my only option:

$stArr = array();
foreach($dataTeacherAssWr[0]->getStudents() as $student) {
    $stArr[] = $student->getIdUser();
}
MyCacheManager::clearAssignmentCacheOnHomepageForUsers($_SESSION['id_user'], $stArr);

and the method:

public static function clearAssignmentCacheOnHomepageForUsers($teacherId, $studentId) {
    return self::clearAssignmentCacheOnHomepage(
        Array(
            'teacherIds' => array($teacherId),
            'studentIds' => array($studentId)
        )
    );
}

as you can see I can pass $studentId as "flat" ids in string

4
  • 2
    array() is a language construct not a function, and you might misinterpret it's usage if you look at it as a function. Commented Nov 16, 2012 at 9:34
  • 1
    You need to be clearer as to why you think passing a string into array() is your only option, and what you hope to achieve. Commented Nov 16, 2012 at 10:17
  • $stArr = array(); foreach($dataTeacherAssWr[0]->getStudents() as $student) { $stArr[] = $student->getIdUser(); } MyCacheManager::clearAssignmentCacheOnHomepageForUsers($_SESSION['id_user'], $stArr); public static function clearAssignmentCacheOnHomepageForUsers($teacherId, $studentId) { return self::clearAssignmentCacheOnHomepage( Array( 'teacherIds' => array($teacherId), 'studentIds' => array($studentId) ) ); } Commented Nov 16, 2012 at 13:29
  • I guess $teacherId and $studentId are strings formatted like "112233,112234" ? Literally all you need to do is replace 'teacherIds' => array($teacherId); with 'teacherIds' => explode(',',$teacherId); and the same for 'studentIds' => ... ... so you don't have to use array(); Commented Nov 16, 2012 at 16:39

4 Answers 4

3

Try using the explode function to split the numbers separated by commas into an array...

explode(',', $ints);

With your example...

$ints = '112233,112234';
$arr = explode(',', $ints);

Produces the following output:

Array ( [0] => 112233 [1] => 112234 )

If you are looking to pass a string to the language construct array(), it simply cannot be done. You can give it a comma separated list for example array(112233, 112234); but you cannot give it array("112233,112234"); The array() documentation will explain what syntax it will accept.

It will not know that the comma separates a list of integers and that you want it split. All it cares is that you've given it a string, so it will create an array with a string in it. It's not a mind-reader, so you need to separate the values yourself.

2
  • as I mentioned - I need to pass the string to language construct Commented Nov 16, 2012 at 10:09
  • 1
    Why do you need to? You can't pass a string to the language construct. Check the docs for the syntax it will accept: uk1.php.net/manual/en/function.array.php . What exactly is stopping you from using explode instead? It produces exactly the same response. Commented Nov 16, 2012 at 10:12
2

You could use explode()

$array = explode(",", $string);

Input:

$string = '112233,112234';
$array = explode(",", $string);
var_dump($array);

Output:

array(2) { [0]=> string(6) "112233" [1]=> string(6) "112234" }
2
  • please, read the question! language construct array() is my only option Commented Nov 16, 2012 at 10:10
  • 1
    @Wojciechk That isn't possible. Array will take a comma separated list, won't take a string and separate it out for you. Commented Nov 16, 2012 at 10:12
1

If you must have your data in a comma-separated string, you can simply explode it on the comma to get the array:

$ints = '112233,112234';
$arr = explode(',', $ints);
2
  • please, read the question! language construct array() is my only option Commented Nov 16, 2012 at 10:10
  • 1
    @Wojciechk language construct array() has a specific syntax. You cannot require to use one construct with a syntax from another. If you cannot use explode but do require a CSV string, then you simply cannot achieve what you're after. Commented Nov 16, 2012 at 10:13
1

This is another way:

    $separator=",";
    $str="112233,112234,112235,112236,112237";
    $arr = explode($separator, $str);
    //print_r($arr);
1
  • Please consider upvoting existing, correct answers instead of posting your own, similar one that doesn't seem to bring anything new to the table. =) Commented Nov 16, 2012 at 13:40

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.