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);
Related
I have two arrays I want to search from first array by key only which i have in second array and as third array i want to print result:
$colldata=array("bench-press-rod"=>'',"adidas-classic-backpack"=>'93549559913',"adidas-classic-backpack-legend-ink-multicolour"=>'',"puma-suede-classic-regal"=>'93549920361,93549723753');
$colldata2=array(0 => 'bench-press-rod',1 => 'adidas-classic-backpack');
Expected result:
array('bench-press-rod'=>'',"adidas-classic-backpack"=>'93549559913');
You can do this in one line in PHP, using array_flip to swap the keys and values of the second array, and then array_intersect_key to merge the two arrays on matching keys:
$colldata=array("bench-press-rod"=>'',"adidas-classic-backpack"=>'93549559913',"adidas-classic-backpack-legend-ink-multicolour"=>'',"puma-suede-classic-regal"=>'93549920361,93549723753');
$colldata2=array(0 => 'bench-press-rod',1 => 'adidas-classic-backpack');
print_r(array_intersect_key($colldata, array_flip($colldata2)));
Output:
Array
(
[bench-press-rod] =>
[adidas-classic-backpack] => 93549559913
)
Demo on 3v4l.org
About the simplest I can come up with is to loop through the second array and add the matching key from the first array into the output. If the item isn't present then it puts Not found in the output...
$output = [];
foreach ( $colldata2 as $item ) {
$output[$item] = $colldata[$item] ?? 'Not found';
}
print_r($output);
gives..
Array
(
[bench-press-rod] =>
[adidas-classic-backpack] => 93549559913
)
Check this.
$colldata=array("bench-press-rod"=>'',"adidas-classic-backpack"=>'93549559913',"adidas-classic-backpack-legend-ink-multicolour"=>'',"puma-suede-classic-regal"=>'93549920361,93549723753');
$colldata2=array(0 => 'bench-press-rod',1 => 'adidas-classic-backpack');
$result = [];
foreach ($colldata2 as $key => $value) {
if (array_key_exists($value, $colldata)) {
array_push($result,$colldata[$value]);
}
}
echo '<pre/>';
print_r($result);
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.
How should I make this array value that is in string form
Array
(
[0] => ["1","2"]
[1] => ["5"]
)
into int form
Array
(
[0] => 1
[1] => 2
[2] => 5
)
Is it complicated? Anyone can help?
you can use array_map to parse string to int and array_reduce with array_merge to join the arrays
$data = Array(["1","2"],["5"]);
$result = array_map('intval', array_reduce($data, 'array_merge', array()));
print_r($result);
Try this:
$array = [
["1", "2"],
["5"],
];
$newArray = [];
foreach ($array as $rowArr) {
foreach ($rowArr as $str) {
$newArray[] = intval($str);
}
}
var_dump($newArray);
This returns:
array (size=3)
0 => int 1
1 => int 2
2 => int 5
This works by iterating $array, then iterating $array's child elements ($rowArr) and adding each element to $newArray after running intval on it.
Use array_reduce (https://3v4l.org/H4eIV)
$a = Array
(
0=> ["1","2"],
1=> ["5"]
);
$r = array_reduce($a, 'array_merge', array());
var_export($r);
Result:
array (
0 => '1',
1 => '2',
2 => '5',
)
You only need to loop through the two layers of your array.
$input=[["1","2"],["5"]];
foreach($input as $a){
foreach($a as $v){
$result[]=+$v; // make integer
}
}
var_export($result);
Another way: create a closure that casts variables to ints and appends them to an array.
$appendInt = function($x) use (&$result) { $result[] = (int) $x; };
// reference to result array^ append to result^ ^cast to int
Apply it to every element of your multidimensional array with array_walk_recursive.
array_walk_recursive($your_array, $appendInt);
The flat array of ints will be in the $result variable.
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);
I have this foreach loop:
foreach($aMbs as $aMemb){
$ignoreArray = array(1,3);
if (!in_array($aMemb['ID'],$ignoreArray)){
$aMemberships[] = array($aMemb['ID'] => $aMemb['Name']);
}
}
This prints out the right fields but they are arrays inside arrays. I need the foreach loop to output a simple array like this one:
$aMemberships = array('1' => 'Standard', '2' => 'Silver');
What am I doing wrong?
You need to change your $aMemberships assignment
$aMemberships[] = $aMemb['Name'];
If you want an array
$aMemberships[$aMemb['ID']] = $aMemb['Name'];
if you want a map.
What you are doing is appending an array to an array.
Associative array in foreach statement:
foreach($nodeids as $field => $value) {
$field_data[$field]=$value;
}
Output:
Array(
$field => $value,
$field => $value
...
);
insertion in CodeIgniter:
$res=$this->db->insert($bundle_table,$field_data);
Instead of
$aMemberships[] = array($aMemb['ID'] => $aMemb['Name']);
Try
$aMemberships[$aMemb['ID']] = $aMemb['Name'];
Your existing code uses incremental key and uses the array as corresponding value.
To make make $aMemberships an associative array with key as $aMemb['ID'] and value being $aMemb['Name'] you need to change
$aMemberships[] = array($aMemb['ID'] => $aMemb['Name']);
in the foreach loop to:
$aMemberships[$aMemb['ID']] = $aMemb['Name']);
it prints an array of arrays because you are doing so in this line
$aMemberships[] = array($aMemb['ID'] => $aMemb['Name']);
where you [] after a variable your are indicating to assign the value in a new row of the array and you are inserting an other array into that row
so you can use the the examples the others haver already gave or you can use this method:
int array_push ( array &$array , mixed $var [, mixed $... ] )
here is an example that you can find in the api
<?php
$stack = array(0=>"orange",1=>"banana");
array_push($stack, 2=>"apple",3=>"raspberry");
print_r($stack);
?>
//prints
Array
(
[0] => orange
[1] => banana
[2] => apple
[3] => raspberry
)
http://php.net/manual/en/function.array-push.php
You get key and value of an associative array in foreach loop and create an associative with key and value pairs.
$aMemberships=array();//define array
foreach($aMbs as $key=>$value){
$ignoreArray = array(1,3);
if (!in_array($key,$ignoreArray)){
$aMemberships[$key] = $value;
}
}
It will give you an expected output:
array('1' => 'Standard', '2' => 'Silver');