souce code souce code - 6 months ago 8
PHP Question

how to add a string value into php array

When i add a string value into array through

array_push()
, it give me a numeric value i.e

$array=array("one","two","three");
$array2=array("test","test2");
foreach($array as $value){
if($value==='one'){
$push=array_push($array2, $value);
}
}
print_r($push);


It output is
3
.what i want to is
$array2 = array("test","test2","one")

Answer

The array_push is working as it is designed for.

It will add the value and returns the number of elements in that array.

so it is natural if it is returning 3 your array has 2 elements after array push there are now three elements.

You should print_r($array2) your array and look the elements.

Comments