2
I need a function that converts this string into an array as follows
3:2,4:1,5:1 //string
array (
[3] = 2
[4] = 1
[5] = 1
)
2
I need a function that converts this string into an array as follows
3:2,4:1,5:1 //string
array (
[3] = 2
[4] = 1
[5] = 1
)
2
Use a explode() to break the string into array then create a new array where key will be $valor[0]
and the value $valor[1]
<?php
$str = '3:2,4:1,5:1';
$arr = explode(',', $str); // transforma a string em array.
$arrN = array();
foreach($arr as $item){
$valor = explode(':', $item); // quebra o elemento atual em um array com duas posições,
onde o indice zero é a chave e o um o valor em $arrN
$arrN[$valor[0]] = $valor[1];
}
Browser other questions tagged php array string
You are not signed in. Login or sign up in order to post.
what language?
– luigibertaco