I need to insert valus into a array.How i do that. my code is below:
foreach($number_array as $number)
{
if(strlen($number)==10)
{
//How to insert the number values into an array ??
}
}
I need to insert valus into a array.How i do that. my code is below:
foreach($number_array as $number)
{
if(strlen($number)==10)
{
//How to insert the number values into an array ??
}
}
Though both answers are correct; it seems to me that the foreach is useless, you can achieve this just as well with array_filter, which is faster and easier to use (from my point of view, anyway):
<?php
$newArray = array_filter( $number_array, function( $element ) {
return strlen( $element ) === 10;
});
Append them onto $array with the [] notation, or use array_push().
// Start with empty array.
$array = array();
foreach($number_array as $number)
{
if(strlen($number)==10)
{
// Append $number to $array
$array[] = $number;
// Alternatively, use array_push()
array_push($array, $number);
}
}