PHP making array with multiple values from a string - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Sunday, June 6, 2021

PHP making array with multiple values from a string

I have a string that have a specific format. The format is, text:number,text:number... Lenght of this string is variable. Something like this:

$input = "hello:20,test:40,abc:25"

As I said, the lenght, text and numbers are variable and passed from another page.

I want to make this string into an array which should look like this.

$array[]  => {$array["title"],$array["length"]}
$array[0] => {"hello","20"}
$array[1] => {"test","40"}
$array[2] => {"abc","25"}

And I want to echo the array items with for loop. So,

<?php for($i=0;$i<count($array);$i++) { ?>
   <th><?=$array[$i]["length"]?></th>
   <th><?=$array[$i]["title"]?></th>
<?php } ?>

should result with

enter image description here

I have tried to use explode() function, but I could only seperate the string into array by the "," and couldn't figure out how to do it.



source https://stackoverflow.com/questions/67852942/php-making-array-with-multiple-values-from-a-string

No comments:

Post a Comment