Making variableArray to work

I have the code1 below which works fine.

$numberArray = array(1, 2, 3) ;
echo $numberArray[0] . "<br>";
echo $numberArray[1] . "<br>";
echo $numberArray[2] . "<br>";

The code above produces the result1 below.

And I have the code2 below which does NOT work as I expected.

$myVariable = "1, 2, 3" ;
$variableArray = array($myVariable) ;
echo $variableArray[0].  "<br>" ;
echo $variableArray[1].  "<br>" ;
echo $variableArray[2].  "<br>" ;

The code above produces the result2 below with waring.

How can I make the code2 above works fine like the below?

You need to convert the string β€œ1, 2, 3” to an array. If you are sure the string is always well formatted you can use the explode function

$myString = "1, 2, 3";
$myArray = explode(", ", $myString);

A little bit more Safe method would be to explode with comma only and trim the results

$myArray = explode(",", $myString);
$myArray = array_map(function($entry) { return trim($entry); }, $myArray);
1 Like

To explain this, first you create a single text string variable:-
$myVariable = "1, 2, 3" ;

Next you add that single variable (containing a text string) to an undefined array, thus making the text string the first and only element within the array:-
$variableArray = array($myVariable) ;

Then you echo the that first and only array element, which is a text string:-
echo $variableArray[0]. "<br>" ;
That results in the string you defined being printed:-
1, 2, 3

You then attempt to echo further elements of the array which have not been defined:-

echo $variableArray[1].  "<br>" ;
echo $variableArray[2].  "<br>" ;

Result:-

Undefined array key 1
Undefined array key 2

So why can’t it work like you expected?
Imagine you had an array containing text strings that were paragrphs of an article. You want to add a paragraph to the array. The text has punctuation. Eg:-

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque nec maximus dui, sit amet vulputate neque. Quisque condimentum faucibus hendrerit. Vivamus volutpat porttitor imperdiet. Pellentesque a egestas ex. Nullam ornare eget nisl eget egestas. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Aliquam posuere nulla nulla, ut volutpat nulla finibus non. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Duis ac lobortis erat. Maecenas dignissim, purus ac pellentesque dignissim, enim nunc molestie augue, eget sollicitudin urna nunc ut nisi. Integer tincidunt est sit amet venenatis lacinia. Morbi pellentesque quam nec tortor egestas, quis interdum lacus volutpat. Maecenas luctus tortor magna, id imperdiet dolor tincidunt volutpat. Nulla non laoreet libero, in posuere diam. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Vestibulum pharetra, est sed mollis condimentum, lorem risus fringilla sem, non finibus arcu justo quis nibh.

If this text was assigned as a string to a variable, and that variable added to an array:-

$article = array($paragraph);

Would you expect (want) the whole paragraph to be added to the array as a single element?
Or should the text be broken into separate elements on every comma it contains?

foreach($article as $para){ echo "<p>$para</p>" ;}

Lorem ipsum dolor sit amet,

consectetur adipiscing elit. Quisque nec maximus dui,

sit amet vulputate neque. Quisque condimentum faucibus hendrerit. Vivamus volutpat porttitor imperdiet. Pellentesque a egestas ex. Nullam ornare eget nisl eget egestas. Class aptent taciti sociosqu ad litora torquent per conubia nostra,

per inceptos himenaeos. Aliquam posuere nulla nulla,

ut volutpat nulla finibus non. Orci varius natoque penatibus et magnis dis parturient montes,

nascetur ridiculus mus. Duis ac lobortis erat. Maecenas dignissim,

purus ac pellentesque dignissim,

enim nunc molestie augue,

eget sollicitudin urna nunc ut nisi. Integer tincidunt est sit amet venenatis lacinia. Morbi pellentesque quam nec tortor egestas,

quis interdum lacus volutpat. Maecenas luctus tortor magna,

id imperdiet dolor tincidunt volutpat. Nulla non laoreet libero,

in posuere diam. Class aptent taciti sociosqu ad litora torquent per conubia nostra,

per inceptos himenaeos. Vestibulum pharetra,

est sed mollis condimentum,

lorem risus fringilla sem,

non finibus arcu justo quis nibh.

Using explode() to convert a string to an array will let you define which caracter is used to divide the elements, it can be any.

Thank you very much, Thallius and SamA74