I have array like and used array_keys to get keys:
$arr = array( 1 => 1,
2 => 3,
3 => 2,
5 => 0,
6 => 0 );
$new_arr = array_keys($arr);
Now, I want to get array_keys if value is not zero. How can i do this?
Please help.
Run array_filter on your array before you get the keys; that removes the 0 values and you only get the keys you need.
$new_arr = array_keys(array_filter($arr));
Output
Array
(
[0] => 1
[1] => 2
[2] => 3
)
You can remove all elements with values before passing array for array_keys:
NULL
null
''
0
With the following:
array_filter($array, function($var) {
// Remove all empty values defined in the above list.
return !is_empty($var);
});
$num_array = array(1,2,3,4,0,0);
$zero_val = array_keys($num_array,!0);
print_r($zero_val);
Related
I am trying to create an batch of similar array's based on first key of array.
Here is the input array
$headers = array(
0 => 'order_number',
1 => 'product_sku',
);
$input_array = array (
0 =>
array (
0 => '311000000706',
1 => 'S503053'
),
1 =>
array (
0 => '311000000706',
1 => 'S516135'
),
2 =>
array (
0 => '311000000703',
1 => 'S526587'
),
3 =>
array (
0 => '311000000703',
1 => 'S526587'
),
);
I am trying to create an seperate array if it has same order id like if two array has same order id = "311000000706"
then desired output will be
$desired_output = array (
0 =>
array (
0 => '311000000706',
1 => 'S503053'
),
1 =>
array (
0 => '311000000706',
1 => 'S516135'
));
Here is what i have tried
$prev_order_id_s = null;
$similar_order_ids = array();
foreach ($input_array as $key => $level):
if ($level['0'] == $prev_order_id_s) {
$similar_order_ids[][] = $level;
}
$prev_order_id_s = $level['0'];
endforeach;
but i am not getting desired output, i need some advice on this.
can you please advice some logic on this ?
Here is the compiler with above code - paiza.io/projects/fB-8CfiEuTX1cJXnXAZS3g
Instead of maintaining variables for previous order IDs, I would rather suggest you to take advantage of order ID as an associative key and keep adding rows to it whenever you find a matching order ID. In the end, you could array_values() on the collected data to remove order IDs as keys.
<?php
$set = [];
foreach($input_array as $row){
$set[$row[0]][] = $row;
}
$result = array_values($set);
print_r($result);
Do not use foreach to filter your array. You can use the array_filter function in PHP. Let's assume you want to check the first element of each array (means the zero index) and your desired value for this key is '311000000706'. It transaltes to PHP like this:
$desired_output= array_filter($input_array, function ($value) {
return $value[0] === '311000000706';
});
I have an array like this:
$array = array(
0 => "a,b",
1 => "c,d",
2 => "e,f",
3 => "g,h",
);
I would like to merge the last two array elements (2 and 3) into a one like this:
$array = array(
0 => "a,b",
1 => "c,d",
2 => "e,f,g,h",
);
How can I do it using PHP?
Remove tow last items by array_splice and add implode of them
$temp = array_splice($array,-2);
$result = array_merge($array, (array) implode(',', $temp));
demo
As #Nick mentioned, you can do it by
$temp = array_splice($array,-2);
$array[] = implode(',', $temp);
Simple, use array_pop() to remove the last 2 elements, then concatenate them, then add them back to the original array.
$array = array(
0 => "a,b",
1 => "c,d",
2 => "e,f",
3 => "g,h",
);
$element3 = array_pop($array); //grab value of the last element, and remove it from the array.
$element2 = array_pop($array);
$array[] = "$element2,$element3";
this will always work if it's always supposed to be the last 2 elements.
This can be achieved with a one-liner that unconditionally pushes that joined strings of the last two elements after consuming them with array_splice(). In other words, the last two elements are removed, joined, then re-pushed into the array as a single element.
The $temp variable in #solash58's answer is not necessary -- the consumption of the last two elements will occur before the auto-incremented index is generated.
Code: (Demo)
$array = [
0 => "a,b",
1 => "c,d",
2 => "e,f",
3 => "g,h",
];
$array[] = implode(",", array_splice($array, -2));
var_export($array);
Output:
array (
0 => 'a,b',
1 => 'c,d',
2 => 'e,f,g,h',
)
I am trying to get those values from array which are repeating N number of times in array. I know it sounds confusing. Let me give an example:
I have an array as $filter:
array[
0 => "556878",
1 => "12345",
2 => "12345",
3 => "234567",
4 => "45673",
5 => "45673"
]
If you can see that 12345 is repeating 2 times. Same is the case with 45673.
I want to create a function or if exists already in PHP, that helps me to get only those values which are repeating N number of times. Like from the example above, value 12345 and 45673 is repeating 2 times. So If I give 2 as a parameter to a function like:
getNtimes($filter,2)
Its going to return:
array[
0 => "12345",
1 => "45673"
]
I am in search of most efficient way to get the desire results. Can anyone help me with this please?
Thanks in advance.
Use array_count_values() with foreach() inside a function:-
function getNtimes($array,$count){
$finalArray = [];
$repeatedValuesCountArray = array_count_values($array);
foreach($repeatedValuesCountArray as $key=>$value){
if($value == $count){
$finalArray[] = $key;
}
}
return $finalArray;
}
print_r(getNtimes($array,2));
Output:- https://3v4l.org/g846e
Here is your function using array_filter,
function getNtimes($arr, $n)
{
$temp = array_count_values($arr);
// filter with count and then get those keys.
return array_keys(array_filter($temp, function ($value) use (&$n) {
return $value == $n;
}));
}
array_keys — Return all the keys or a subset of the keys of an array
array_count_values — Counts all the values of an array
array_filter — Filters elements of an array using a callback function
working demo.
As others has answered, start with array_count_values.
Then I use array_intersect to get the values matching $n.
Lastly since the "values" are keys, I use array_keys to get the duplicated values from original array.
$n = 2;
$result = array_keys(array_intersect(array_count_values($arr), [$n]));
var_dump($result);
Output:
array(2) {
[0]=>
int(12345)
[1]=>
int(45673)
}
https://3v4l.org/BIgCD
Use array_unique() + array_filter() + array_count_values() :
print_r
(
array_unique(
array_filter($arr = [
0 => 556878,
1 => 12345,
2 => 12345,
3 => 234567,
4 => 45673,
5 => 45673,
],
function ($v) use($arr) {
return array_count_values($arr)[$v] == 2;
}
)
)
);
If you need to re-index elements from 0 - apply array_values() to array_unique() result
You can use array_filter with array_count_values
$arr = [
0 => "556878",
1 => "12345",
2 => "12345",
3 => "234567",
4 => "45673",
5 => "45673"
];
$N = 2;
$final_result = array_filter(array_count_values($arr), function($v) use($N){
return $v==$N;
});
print_r($final_result);
Output :
Array
(
[12345] => 2
[45673] => 2
)
Live Example
Here is an example array I want to split:
(1428,217,1428)
How do I split it in 2 array like this?
(1428,1428)
(217)
I have tried following way but it's only return 1428 array.
$counts = array_count_values($array);
$filtered = array_filter($array, function ($value) use ($counts) {
return $counts[$value] > 1;
});
One way to solve this for your example data is to sort the array and use array_shift to get the first element of the array and store that in an array.
$a = [1428,217,1428];
sort($a);
$b = [array_shift($a)];
print_r($a);
print_r($b);
Result
Array
(
[0] => 1428
[1] => 1428
)
Array
(
[0] => 217
)
You can try this.
$array = array(1428,217,1428);
$array1 = array_slice($array, 0, 2);
$array2 = array_slice($array, 2, 3);
print_r($array1);
print_r($array2);
And the output will like this:-
Array
(
[0] => 1428
[1] => 217
)
Array
(
[0] => 1428
)
In your case it will only return 1428 since array_count_values returns an array with values as keys and their frequency as array value therefore $counts will be equal to array('1428' => 2, '217' => 1);
If I understood your question well you should do something like this:
$array1 = [1428, 217, 1428];
$result = [];
foreach($array1 as $value){
$result[$value][] = $value;
}
This will not create an array for each different value but will create a new element for each unique value in $result. The final value of $result will be array('1428' => [1428, 1428], '217' => [217]) . Which can be easily manipulated as if they were 2 different arrays.
Let me know if this works for you, if not I will try to update my answer according to your specification.
php:
$arr = array(1=>'1233',2=>'12334',3 =>'Hello' ,4=>'Hello' ,5 =>'awesome' ,6 =>'awesome' ,7 =>'UK');
// Convert every value to uppercase, and remove duplicate values
$withoutDuplicates = array_unique(array_map("strtoupper", $arr));
$duplicates = array_diff($arr, $withoutDuplicates);
print_r($duplicates);
foreach ($duplicates as $key => $value) {
echo $value . ":" . $key. ' ';
}
Output:
Array
(
[3] => Hello
[4] => Hello
[5] => awesome
[6] => awesome
)
Hello:3 Hello:4 awesome:5 awesome:6
check online:
http://writecodeonline.com/php/
I need to get duplicate values keys in a separate array.
for example:
array1 includes 3,4 for Hello.
array2 includes 5,6 for awesome.
the above code can output duplicate values and also can get their keys. Now i want to put duplicate values keys in a array.
If I have understood your question properly, you want to get the array keys of the duplicate array values for each array?
This can be done with the array_keys() function and provide it the optional search parameter.
/*
* A side note: you do not have to specify the array index if their are numerical. PHP
* will do that for you.
*/
$array = array('1233', '12334', 'Hello', 'Hello', 'awesome', 'awesome', 'UK');
$keys = [];
$unique = array_unique($array);
foreach($unique as $search) {
$found = array_keys($array, $search);
/*
* If array_keys provided more than two results duplicate array values must exist.
*/
if(count($found) > 1) {
$keys[strtoupper($search)] = $found;
}
}
var_dump($keys);
This will result in associative array where the array index is the value searched for and the array value is an array of all the keys.
array (size=2)
'HELLO' =>
array (size=2)
0 => int 3
1 => int 4
'AWESOME' =>
array (size=2)
0 => int 5
1 => int 6
Hope this helps.
Regards.
If I understand you, you can use array_keys
$keys = array_keys($duplicates);