Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

How to convert array values from string to int?

If I have this array array(1,2,3); How can I convert this integer value to string array("1","2","3")

user-image
Question ajoutée par Khadijah Shtayat , Technical Lead , Opensooq
Date de publication: 2013/10/30
Feras Abualrub
par Feras Abualrub , Web Solutions Manager , Qistas for Information Technology

use int($value) or intval($value)

Muhammad Majid Saleem
par Muhammad Majid Saleem , Senior PHP Developer / Project Manager , SwaamTech

You have2 questions actually.1 in title and other is in description. You don't define in which language you want to do so either in JS, PHP, ASP or any other language so I am a little bit confused to answer :).

 

Let me try to answer your both questions.

You can convert an INT array to String array in PHP using following way:

- There is not built-in function available in PHP to convert it directly so you need to use your own logic to acheive your goal. So my logic is:

<?php

1st Way:

$a = array(1,2,3);

for( $i =0; $i < count( $a ); $i++ ){    $c[$i] = (string) $a[$i];}

var_dump( $a );

var_dump( $c )

 

2nd Way:

$a = array(1,2,3);$b = '"'.implode('","', $a).'"';$c = explode(",", $b);

var_dump( $a );

var_dump( $c );

 

// Reverse this process to convert a String array to INT array.

$a = array("1","2","3");for( $i =0; $i < count( $a ); $i++ ){    $c[$i] = (int) $a[$i];}

var_dump( $a );

var_dump( $c );

?>

Mohammad Irfan
par Mohammad Irfan , Certified Magento Developer , Xcite.com

Please check this:

string[] arInfo = { "1", "2", "3" };

int[] iarInfo = new int[3];          

for (int i =0; i < arInfo.Length; i++)          

{            

 iarInfo[i] = Convert.ToInt32(arInfo[i]);            

 Console.Write(iarInfo[i]);         

 }

i hope it will work for you.

Ashok Yadav
par Ashok Yadav , trainee , S.O Infotech Pvt. Ltd.

simply use toString() method to convert int to string.

public static void main(String[] s)throws Exception

{

    int a[]={1,2,3};   

String a1[]=new String[3]; 

 for (int i =0; i <3; i++)

{

   a1[i]=Integer.toString(a[i]);           

}

    }

 

Zaid Rabab'a
par Zaid Rabab'a , Software Development Team Leader , Al-Safa Co. Ltd.

You can use Convert.ToInt32

for ( int i=0;i< a_array.length ; i++) {

Console.writeline(convert.toInt32(a_array[i].toString())

}

int=arry("1","2","3");

string=sum;

sum=array[0][1]+[0][2]+........}

khaja Mohammed siddique
par khaja Mohammed siddique , Senior SharePoint Developer And Administrator , NAMAA

Have you done casting or try this example:

 

int[] intarray ={1,2,3,4,5};string[] result = intarray.Select(x=>x.ToString()).ToArray();

AMITESH KUMAR
par AMITESH KUMAR , SOFTWARE DEVELOPER , TECHZONE TECHNOLOGY PVT. LTD.

public static void main(String[] args)throws Exception{    int a[]={1,2,3};    String aa[]=new String[a.length];    for (int i =0; i < a.length; i++) {        aa[i]=Integer.toString(a[i]);            }    }

EL Mehdi EL MAIS
par EL Mehdi EL MAIS , Concepteur/Devloppeur , Orbit Interactive

Assalmou Alykom ,

There is3 way to convert value in php

1 - changing variable's data type : settype($var , 'string')

2 - changing type by casting  : (string) $var

3 - changing by this function strval($var)

for apply it easy i make this exmple ,

____________________________________________________________________

<?php

$array = [1,2,3,4] ;

var_dump($array );

$array  = array_map("IntegerToString", $array);

var_dump($array );

function IntegerToString($n)

{

settype($n, 'string');

return $n ;

}

__________________________________________________________________________

and for result 

array (size=4)0 => int 11 => int 22 => int 33 => int 4

 

array (size=4)0 => string '1' (length=1)1 => string '2' (length=1)2 => string '3' (length=1)3 => string '4' (length=1)

 

Assalmou alykom

Mohammad Ateieh
par Mohammad Ateieh , Software Engineering Manager , Bayt.com

try this one 

$string = array("1","2","3");

$integers = array_map('intval', $string);

var_dump($integers);

Tamer Ibrahim
par Tamer Ibrahim , Analyst Programmer , Royal Children's Hospital

int[] intArray = {1,2,3 };

string[] stringArray = intarray.Select(s => s.ToString()).ToArray();

More Questions Like This

Avez-vous besoin d'aide pour créer un CV ayant les mots-clés recherchés par les employeurs?