I have a php array with strings and I'd like to delete the keys that have a string containing only numbers.
How can I do that?
Filtering the array would be the most elegant way:
$array = array_filter($array, 'is_numeric');
This returns an array with only those values for whom is_numeric() is true.
foreach ($array as $key => $val)
if (is_numeric($key)) // only numbers, a point and an `e` like in 1.1e10
unset($array[$key]);
This unsets all the entries where there are only numbers.
Use this code
foreach($array as $key=>$value)
if(is_numeric($value))
unset($array($key));
Related
I have an array with string value in PHP for example : arr['apple'], arr['banana'], and many more -about 20-30 data (get it from some process). Now I want to get its value and return it to one variable.
For example, I have Original array is like this:
$arr['Apple']
$arr['Banana']
and more..
and result that I want is like this:
$arr[0] = "Apple"
$arr[1] = "Banana"
and more..
Any idea how to do that?
Why not using array_keys()?
$new_array = array_keys($array);
Use array_flip()
$new_arr = array_flip($old_arr);
Demonstration
use foreach loop
foreach($arr as $key => $val){
$new_var[] = $key;
}
use array_keys function:
$keys = array_keys($arr);
It returns an array of all the keys in array.
I have a foreach loop and I would like to completely remove the array elements that satisfy the criteria, and change the keys to stay sequential 1,2,3,4.
I have:
$thearray = array(20,1,15,12,3,6,93);
foreach($thearray as $key => $value){
if($value < 10){
unset($thearray[$key]);
}
}
print_r($thearray);
But this keeps the keys as they were before. I want to make them 1,2,3,4, how can this be achieved?
Reset the array indices with array_values():
$thearray = array_values( $thearray);
print_r($thearray);
You can just use array_filter to remove the array elements that satisfy the criteria
$thisarray = array_filter($thearray,function($v){ return $v > 10 ;});
Then use array_values change the keys to stay 0, 1,2,3,4 .... as required
$thisarray = array_values($thisarray);
Build up a new array and then assign that to your original array after:
$thearray=array(20,1,15,12,3,6,93);
$newarray=array();
foreach($thearray as $key=>$value){
if($value>=10){
$newarray[]=$value
}
}
$thearray=$newarray;
print_r($thearray);
I have this kind of an array containing single-element arrays:
$array = [[88868], [88867], [88869], [88870]];
I need to convert this to one dimensional array.
Desired output:
[88868, 88867, 88869, 88870]
Is there any built-in/native PHP functionality for this array conversion?
For your limited use case, this'll do it:
$oneDimensionalArray = array_map('current', $twoDimensionalArray);
This can be more generalized for when the subarrays have many entries to this:
$oneDimensionalArray = call_user_func_array('array_merge', $twoDimensionalArray);
The PHP array_mergeĀDocs function can flatten your array:
$flat = call_user_func_array('array_merge', $array);
In case the original array has a higher depth than 2 levels, the SPL in PHP has a RecursiveArrayIterator you can use to flatten it:
$flat = iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($array)), 0);
See as well: Turning multidimensional array into one-dimensional array
try:
$new_array = array();
foreach($big_array as $array)
{
foreach($array as $val)
{
array_push($new_array, $val);
}
}
print_r($new_array);
$oneDim = array();
foreach($twoDim as $i) {
$oneDim[] = $i[0];
}
Yup.
$values = array(array(88868), array(88867), array(88869), array(88870));
foreach ($values as &$value) $value = $value[0];
http://codepad.org/f9KjbCCb
foreach($array as $key => $value){
//check that $value is not empty and an array
if (!empty($value) && is_array($value)) {
foreach ($value as $k => $v) {
//pushing data to new array
$newArray[] = $v;
}
}
}
For a two dimensional array this works as well:
array_merge(...$twoDimensionalArray)
While some of the answers on the page that was previously used to close this page did have answers that suited this question (like array_merge(...$array)). There are techniques for this specific question that do not belong on the other page because of the input data structure.
The sample data structure here is an array of single-element, indexed arrays.
var_export(array_column($array, 0));
Is all that this question requires.
If you ever have a daft job interview that asks you to do it without any function calls, you can use a language construct (foreach()) and use "array destructuring" syntax to push values into a result variable without even writing a body for the loop. (Demo)
$result = [];
foreach ($array as [$result[]]);
var_export($result);
Laravel also has a flattening helper method: Arr::flatten()
I have a foreach loop in php to iterate an associative array. Within the loop, instead of increamenting a variable I want to get numeric index of current element. Is it possible.
$arr = array('name'=>'My name','creditcard'=>'234343435355','ssn'=>1450);
foreach($arr as $person){
// want index here
}
I usually do this to get index:
$arr = array('name'=>'My name','creditcard'=>'234343435355','ssn'=>1450);
$counter =0;
foreach($arr as $person){
// do a stuff
$counter++;
}
Use this syntax to foreach to access the key (as $index) and the value (as $person)
foreach ($arr as $index => $person) {
echo "$index = $person";
}
This is explained in the PHP foreach documentation.
Why would you need a numeric index inside associative array? Associative array maps arbitrary values to arbitrary values, like in your example, strings to strings and numbers:
$assoc = [
'name'=>'My name',
'creditcard'=>'234343435355',
'ssn'=>1450
];
Numeric arrays map consecutive numbers to arbitrary values. In your example if we remove all string keys, numbering will be like this:
$numb = [
0=>'My name',
1=>'234343435355',
2=>1450
];
In PHP you don't have to specify keys in this case, it generates them itself.
Now, to get keys in foreach statement, you use the following form, like #MichaelBerkowski already shown you:
foreach ($arr as $index => $value)
If you iterate over numbered array, $index will have number values. If you iterate over assoc array, it'll have values of keys you specified.
Seriously, why I am even describing all that?! It's common knowledge straight from the manual!
Now, if you have an associative array with some arbitrary keys and you must know numbered position of the values and you don't care about keys, you can iterate over result of array_values on your array:
foreach (array_values($assoc) as $value) // etc
But if you do care about keys, you have to use additional counter, like you shown yourself:
$counter = 0;
foreach ($assoc as $key => $value)
{
// do stuff with $key and $value
++$counter;
}
Or some screwed functional-style stuff with array_reduce, doesn't matter.
In the screenshot, you can see i have an array of arrays. I need to find an array that contains say, 'Russia', and unset it completely. That is, for Russia, remove the element [303].
I've played around with array search but I'm sure theres a funkier way of doing this.
Chris.
$array = your array;
foreach ($array as $key => $value) {
if ($value['countryName'] == 'Russia') {
unset($array[$key]);
}
}
and if you want reorder the key , you could use:
$new_array = array_values($array);