Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm trying to get the bottom values of an array with a single condition as below :
Get all numeric values until the next value is a string then stop the instruction, and leave all other values even if they are numeric
<? php $data= [1,2,3,'web',4,5,6,'web',7,8,9]; ?>
The output will be : 7,8,9
We can try first filtering to find the first non numeric entry. Then, subset the array starting with this index. Finally, filter the subset again to remove all non numeric elements.
$data= [1,2,3,'web',4,5,6,'web',7,8,9];
$sub_data = array_filter($data, function($item) { return !is_numeric($item); });
$index = array_search(current($sub_data), $data);
$sub_data = array_slice($data, $index);
$sub_data = array_filter($sub_data, function($item) { return is_numeric($item); });
print_r($sub_data);
This prints:
Array
(
[1] => 4
[2] => 5
[3] => 6
[5] => 7
[6] => 8
[7] => 9
)
i don't know if there a specific function for it but i got the same result with this
$data= [1,2,3,'web',4,5,6,'web',7,8,9];
foreach ($data as $key => $value) {
if($value=="web"){
$x =$key;
}
}
$data = array_slice($data,$x);
print_r($data);
Output:
Array ( [0] => web [1] => 7 [2] => 8 [3] => 9 )
You can use array_reverse to get values from the array with elements in reverse order.
$reversed = array_reverse($data);
and then parse the array to stop where it gets a string
foreach($reversed as $value) {
if(is_string($value)) {
break;
}
$output[] = $value;
}
//print_r($output); // to print the output array. This will return the values like 9, 8, 7
// do a reverse again to get the values in the format you want.
$final= array_reverse($output);
print_r($final); // to print the final array. This will return the values like 7, 8, 9
Here is a DEMO
You can just pop values off the end until you get to something that isn't an int.
while (is_int($n = array_pop($data))) {
$result[] = $n;
}
This will modify the data array, so if you need to keep the original values, then do this with a copy.
The test you'll want depends on whether your example data is representative and all the numeric values you want in the array are of type int, or perhaps some might be numeric strings.
$data= [1,2,3,'web',4,5,6,'web',7,8,9];
$result = [];
for($i = count($data)-1; $i >= 0; $i++) {
// If some of the values you want might be numeric strings
// (e.g. "7") or decimals (e.g. 1.5), use this
if(!is_numeric($data[$i])) {
break;
}
// If your values will only be integer type, use this
if(!is_int($data[$i])) {
break;
}
array_unshift($result,$data[$i]);
}
// If you want the results in original order
print_r($result); // Output: [7,8,9]
// If you want the results inverted
$result = array_reverse($result);
print_r($result); // Output: [9,8,7]
Try this, here i am storing values in to an array $newArray if value is integer. if string found in value just reset the array $newArray
<?php
$data = [1,2,3,'web',4,5,6,'web',7,8,9];
$newArray = array(); // resultant array
foreach ($data as $key => $value) {
if(is_int($value)){
$newArray[] = $value;
}
else{
$newArray = array(); // reset if not integer
}
}
echo "<pre>";
print_r($newArray);
?>
DEMO
Result:
Array
(
[0] => 7
[1] => 8
[2] => 9
)
Now, you can use implode() for comma separated result, like:
echo implode(",",$newArray); // 7,8,9
Edit: (08-07-2019)
If Example:
$data = [1,2,3,'web',4,5,6,'web',7,8,9,'web'];
Code:
<?php
$data = [1,2,3,'web',4,5,6,'web',7,8,9,'web'];
$newArray = array(); // resultant array
end($data);
$keyLast = key($data); // fetches the key of the element pointed to by the internal pointer
foreach ($data as $key => $value) {
if(is_int($value) ){
$newArray[] = $value;
}
else{
if($keyLast === $key) { // if last key is string overwrite the previous array.
$newArray = $newArray;
}
else{
$newArray = array(); // reset if not integer
}
}
}
echo "<pre>";
print_r($newArray);
?>
Edit:
It's better to store all integer values into an array and then use array_slice() to get let three elements from new integer array like:
<?php
$data = [1,2,3,'web',4,5,6,'web',7,8,9,'web','web','web'];
$newArray = array(); // resultant array
foreach ($data as $key => $value) {
if(is_int($value) ){
$newArray[] = $value;
}
}
$lastThreeElement = array_values(array_slice($newArray, -3, 3, true));
echo "<pre>";
print_r($lastThreeElement);
?>
Multiple values in a single array (some value similar)
how to get
One of them is to get the array that is similar values
minimum 1 and maximum 2 times repeat
for Example This array -
$array_value = array('ab','ab','cd','de','ab','cd','ab','de','xy');
foreach($array_value as $value){
}
I want output - ab, ab,cd, cd, de, xy
array_count_values return the repetition of specific value in array. So, you can use it to simplify the code and quickly implement it.
$array_value = array('ab','ab','cd','de','ab','cd','ab','de','xy');
// Get count of every value in array
$array_count_values = array_count_values($array_value);
$result_array = array();
foreach ($array_count_values as $key => $value) {
// Get $value as number of repetition of value and $key as value
if($value > 2) {
$value = 2;
array_push($result_array, $key);
array_push($result_array, $key);
} else {
for ($i=0; $i < $value; $i++) {
array_push($result_array, $key);
}
}
}
print_r($result_array);
I think your output shuold have two de not one?
Anyway here is the code with explanations in comments:
<?php
$array_value = array('ab','ab','cd','de','ab','cd','ab','de','xy');
$arr_count = []; //we use this array to keep track of how many times we've added this
$new_arr = []; //we add elements to this array, or not.
foreach($array_value as $value){
// we've added it before
if (isset($arr_count[$value])) {
// we only add it again one more time, no more.
if ($arr_count[$value] < 2) {
$arr_count[$value]++;
$new_arr[] = $value;
}
}
// we haven't added this before
else {
$arr_count[$value] = 1;
$new_arr[] = $value;
}
}
sort($new_arr);
print_r($new_arr);
/*
(
[0] => ab
[1] => ab
[2] => cd
[3] => cd
[4] => de
[5] => de
[6] => xy
) */
PHP Demo
I have one array and i want to keep only blank values in array in php so how can i achieve that ?
My array is like
$array = array(0=>5,1=>6,2=>7,3=>'',4=>'');
so in result array
$array = array(3=>'',4=>'');
I want like this with existing keys.
You can use array_filter.
function isBlank($arrayValue): bool
{
return '' === $arrayValue;
}
$array = array(0 => 5, 1 => 6, 2 => 7, 3 => '', 4 => '');
var_dump(array_filter($array, 'isBlank'));
use for each loop like this
foreach($array as $x=>$value)
if($value=="")
{
$save=array($x=>$value)
}
if you want print then use print_r in loop
there is likely a fancy built in function but I would:
foreach($arry as $k=>$v){
if($v != ''){
unset($arry[$k]);
}
}
the problem is; you are not using an associative array so I am pretty sure the resulting values would be (from your example) $array = array(0=>'',1=>''); so you would need to:
$newArry = array();
foreach($arry as $k=>$v){
if($v == ''){
$newArry[$k] = $v;
}
}
Here is my array: [["STR_License_Driver",1],["STR_License_Truck",0],["STR_License_Pilot",1],["STR_License_Firearm",0],["STR_License_Rifle",0]]
My goal is to make an array or string (unsure of best method) called results where the value of the licenses is 1.
For example: The results should be something like: STR_Licenses_Driver, STR_Licenses_Pilot.
I am currently using PHP Version 7, and Laravel Version 5.5
JSON Decode the $this->license and do the loop.
Whole code:
$licenses = $this->licenses;//This is your posted array string
$result = json_decode($licenses);
$licenses_with_1 = array();
foreach($result as $i){
foreach($i as $key => $value){
if($value == 1){
$licenses_with_1[] = $i[0];
}
}
}
print_r($licenses_with_1);
Result:
Array ( [0] => STR_License_Driver [1] => STR_License_Pilot )
I remember Python on your data structure.
Try my solution:
https://3v4l.org/nrfqZ
$licenses = [["STR_License_Driver",1],["STR_License_Truck",0],["STR_License_Pilot",1],["STR_License_Firearm",0],["STR_License_Rifle",0]];
Map over filtered array having second item equal to 1 to get the license names.
$filterVal = 1;
$licenseNames = array_map(
function ($item) { return $item[0]; },
array_filter(
$licenses,
function ($item) use ($filterVal) { return $item[1] === $filterVal; }
)
);
Then implode array of license names to join by glue string.
echo implode($licenseNames, ', ');
I have inserted some elements (fruit names) queried from mySQL into an array. Now, I would like to remove certain items from the array. I want to remove 'Apple' and 'Orange' if the exist from the array. This is what I tried but I am getting an error message.
Array Example:
Array ( [1] => Orange [2] => Apple)
foreach($terms as $k => $v)
{
if (key($v) == "Apple")
{
unset($terms[$k]);
}
elseif( key($v) == "Orange")
{
unset($terms[$k]);
}
}
>>> Warning: key() expects parameter 1 to be array, string given //same error repeated 4 times
I referred to this link here: How do you remove an array element in a foreach loop?
I would be grateful if anyone can point out what I did wrong.
Have you tried it this way:
foreach($terms as $k => $v)
{
if ($v == "Apple")
{
unset($terms[$k]);
}
elseif($v == "Orange")
{
unset($terms[$k]);
}
}
Explanation:
The $fr is your actual array of all fruits.. and your $rm is another array that contains list of items to be removed from your $fr array.
Using a foreach cycle through the $rm array and see if the element exists on the $fr array , if found, unset() it.
The code...
<?php
$fr = array('Apple','Orange','Pineapple'); //<-- Your actual array
$rm = array('Apple','Orange'); //<--- Elements to be removed
foreach($rm as $v)
{
if(in_array($v,$fr))
{
unset($fr[array_search($v,$fr)]);
}
}
print_r($fr);
OUTPUT :
Array
(
[2] => Pineapple
)
Using array_diff()
print_r(array_diff($fr,$rm));
Code Demonstration