-2

So basically I have a column in the database which has stored multiple values like this

0 , 33, 0, 0, 138, 1231 

What i would like to do is build an array from the output obtained from the database:

$values = $fetch['values'];
Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57
Gongas
  • 87
  • 7
  • 2
    `explode()`? And please do not tag unrelated products – B001ᛦ Sep 13 '18 at 13:49
  • MorganFreeFarm answered you question with an example, but you should really use a relational database model so that you wouldn't need to store values like you're doing right now. – David Sep 13 '18 at 13:53
  • There is some cases when you need to store data like this, but use json string. then you can easy get array from this string and string from the array. – MorganFreeFarm Sep 13 '18 at 13:59

1 Answers1

2
<?php

$values = '0,33,0,0,138,1231';

$array = explode(',', $values);

var_dump($array);

?>

The output:

array(6) { [0]=> string(2) ""0" [1]=> string(2) "33" [2]=> string(1) "0" [3]=> string(1) "0" [4]=> string(3) "138" [5]=> string(4) "1231" }

More info here

MorganFreeFarm
  • 3,811
  • 8
  • 23
  • 46
  • This did not work , it added all items to only 1 array column, array[0] only – Gongas Sep 13 '18 at 14:07
  • @Gongas - it works for me. Are you getting an error? – Fergal Andrews Sep 13 '18 at 14:14
  • Not an error it simply adds all the list to only 1 array column , array(1) { [0]=> string(240) "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,33,0" } – Gongas Sep 13 '18 at 14:19
  • @B001ᛦ not a typo that was just an example for what i wanted, im getting the values from a database column where i inserted them – Gongas Sep 13 '18 at 14:21
  • It's not working , was due to fact the explode had an extra space after the " , " which i did not have in my list so i removed the space and all was ok ! – Gongas Sep 13 '18 at 15:42