PHP delete elements in associate array by value [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Remove integers in array less than X with PHP
$array = array(
"a" => 10,
"b" => 9,
"c" => 8,
"d" => 7,
"e" => 6,
"f" => 5,
"g" => 4,
"h" => 3
);
How can I dele the item if the value is smaller than 6? i.e., how can I get the following array?
$array = array(
"a" => 10,
"b" => 9,
"c" => 8,
"d" => 7,
"e" => 6
);

array_filter is perfect for this:
$new = array_filter($old,function($a) {return $a >= 6;});

A simple iteration would be:
$new_array = array();
foreach($arras as $key => $value){
if($value >= 6)new_array[$key] = $value;
}

foreach($array as $index=>$value) {
if ( $value < 6) {
unset($array[$index]);
}
}
or
foreach($array as $index=>$arr_value) {
if ( $value >= 6) {
$new_array[$index] = $arr_value;
}
}

Related

Array Keys Comparison Without In-Build Function

I am having two arrays of key-value pairs.
Arrays as follows:
$array1 = ["a" => 2, "b" => 3, "c" => 1, "d" => 2];
$array2 = ["c" => 1, "d" => 1, "a" => 2, "b" => 3, "x" => 4, "z" => 1];
Problem Statement
I need to find the count of the number of keys of array1 present in array2 having the same value.
Example
Count will be 3 as keys a, b, and c of array1 are present in array2 and having same values 2, 3, and 1 respectivily.
Tried method
foreach($array1 as $key => $value){
if($array2.$key === $array1.$key){
if($array2[$value] === $array1[$value]){
$matchCount++;
}
}
}
NOTE: I am not sure about accessing the keys from the array object, so using dot(.), somehow I am getting the count, but not correct count.
<?php
$array1 = ["a" => 2, "b" => 3, "c" => 1, "d" => 2];
$array2 = ["c" => 1, "d" => 1, "a" => 2, "b" => 3, "x" => 4, "z" => 1];
$count = 0;
foreach($array1 as $key => $value){
$array_2_value = $array2[$key] ?? null;
if($array_2_value !== null && $array_2_value === $value) $count++;
}
echo $count;
Demo: https://3v4l.org/BREbg
You can just loop over $array1 and check if the key exists in $array2 with the same value. If yes, we increment count. You can make use of null coalescing operator ?? to check if the $key exists in $array2 or not.
function countSameKeyAndValues(array $array1, array $array2): int
{
$sameKeys = array_keys(array_intersect($array1, $array2));
$count = 0;
foreach ($sameKeys as $key) {
if ($array1[$key] === $array2[$key]) {
$count++;
}
}
return $count;
}
You can loop over the $array1 using foreach loop.
Then you can check for the values of $array2 using the key for $array1.
Then if value exist for that key, you can check if value is same at that of $array1
<?php
$array1 = ["a" => 2, "b" => 3, "c" => 1, "d" => 2];
$array2 = ["c" => 1, "d" => 1, "a" => 2, "b" => 3, "x" => 4, "z" => 1];
$count = 0;
foreach($array1 as $key => $value){
$array2Value = $array2[$key] ?? null;
if($array2Value !== null && $array2Value === $value)
$count++;
}
echo $count;

Array slice like hash slice in perl

Is there built-in function, or shorter way to extract elements into new array, as described here?
<?php
function arr_slice ($arr, $keys) {
$ret = array();
foreach ($keys as $k) { $ret[$k] = $arr[$k]; }
return $ret;
}
$array = array(
"a" => 1,
"b" => 2,
"c" => 3,
"d" => 4,
);
var_export(
arr_slice($array, array("x","d","b"))
);
output (key order matters)
array (
'x' => NULL,
'd' => 4,
'b' => 2,
)

How to create an index array for an associative array?

I have this multidimensional array:
$arr = [
2 => ["a", "b", "c"],
5 => ["j", "k", "l"],
9 => ["w", "x", "y", "z"]
];
For which I'd like to create a new index array like this one:
$index = [
"a" => 2,
"b" => 2,
"c" => 2,
"j" => 5,
"k" => 5,
"l" => 5,
"w" => 9,
"x" => 9,
"y" => 9,
"z" => 9
]
I couldn't find any PHP function that appears to do that, but I'm sure there is one. Or maybe there's some known code that does this efficiently?
$index = array();
foreach ($arr as $k => $a) {
foreach ($a as $v) {
$index[$v] = $k;
}
}

add the values of two different associative arrays in php . display unique key-values too in the result array [duplicate]

This question already has answers here:
How to sum all column values in multi-dimensional array?
(20 answers)
Closed 9 years ago.
i need to merge two associative arrays which may or may not contain same key,if key is same the values need to added and stored in the resultant array
$array1 = array(
'a' => 5,
'b' => 10,
'c' => 6,
'k' = > 10
);
$array2 = array(
'a' => 100,
'c' => 200,
'd' => 30,
'k' => 10
);
how to add the above two associative arrays and receive the following output/associative array -
$array2 = array(
'a' => 105,
'b' => 10,
'c' => 206,
'd' => 30,
'k' => 20
);
Try
$array1 = array(
'a' => 5,
'b' => 10,
'c' => 6,
'k' => 10
);
$array2 = array(
'a' => 100,
'c' => 200,
'd' => 30,
'k' => 10
);
$sums = array();
foreach (array_keys($array1 + $array2) as $key) {
$sums[$key] = (isset($array1[$key]) ? $array1[$key] : 0) + (isset($array2[$key]) ? $array2[$key] : 0);
}
print_r($sums);
$a3 = array_merge_recursive ($array1, $array2);
foreach ($a3 as $key => $value)
{
$a3[$key] = (is_array($value))?array_sum($value):$value;
}
for finding unique index value
$array2 =array_merge($array1,$array2);
and for finding unique value you can try
$array2 =array_unique(array_merge($array1,$array2));
I don't know if you can do this with an internal function but it is doable by simply iterating through both arrays.
function merge_add($array1, $array2)
{
$result = array();
merge_add_array($array1, $result);
merge_add_array($array2, $result);
return $result;
}
function merge_add_array($array, &$result)
{
foreach ($array as $key=>$value)
{
if (array_key_exists($key, $result))
{
$result[$key] += $value;
}
else
{
$result[$key] = $value;
}
}
}
$result = merge_add($array1, $array2);
The & will cause passing by reference
<?php
$array1 = array(
'a' => 5,
'b' => 10,
'c' => 6,
'k' => 10
);
$array2 = array(
'a' => 100,
'c' => 200,
'd' => 30,
'k' => 10
);
$keys = array_keys($array1 + $array2);
foreach ($keys as $key) {
$array2[$key] = isset($array1[$key]) && isset($array2[$key]) ? ($array1[$key] + $array2[$key]) : (isset($array1[$key]) ? $array1[$key] : $array2[$key]);
}
print_r($array2);

Change array keys according to another one

I have 2 arrays:
$array1 = array(1 => "aaa", 4 => "bbb", 5 => "ccc", 8 => "ddd", 9 => "eee", 11 => "fff");
$array2 = array(2 => "", 3 => "", 6 => "", 7 => "", 9 => "", 13 => "");
I want to change the keys of $array1 according to $array2. I'm given the information that an element of the second array has to correspond to another of the first one. For example I know that $array2[6] has to correspond to $array1[4].
So I should have all the keys of $array1 changed according to this rule:
$array1 = array(3 => "aaa", 6 => "bbb", 7 => "ccc", 9 => "ddd", 13 => "eee", 2 => "fff");
I don't know how to solve this. I've tried to split the first array where the element given is but I'm stuck.
You can define a function that maps and constructs a new array.
function transfer_keys($key_array,
$value_array) {
$a = array_map(null, array_keys($key_array),
$value_array);
$result = array();
foreach($a as $kv) {
$result[$kv[0]] = $kv[1];
}
return $result;
}
$array1 = array(1 => "aaa", 4 => "bbb", 5 => "ccc",
8 => "ddd", 9 => "eee", 11 => "fff");
$array2 = array(2 => "", 3 => "", 6 => "", 7 => "",
9 => "", 13 => "");
print_r(transfer_keys($array2, $array1));
if you wanna update all elements of your first array with the values in the array2 in the same order :
$i = 0;
foreach ($array1 as $v) {
while (!isset($array2[$v]))
$i++;
$array2[$i] = $v;
}
But if you want to update according to an order you pre-defined, you have to make something else, like a table defining the rule :
$assoc_array = array(
2 => 4,
3 => 7,
4 => 11,
6 => 5,
5 => 9,
8 => 1);
foreach ($assoc_array as $k => $v) {
$array1[$k] = $array2[$v];
}
Hope this helps.
Aw, you know the difference is always the same !
Then this should be something like :
function updateArray ($array1, $array2, $key_of_array1, $key_associated_of_array2) {
$diff = $key_associated_of_array2 - $key_of_array1;
foreach ($array1 as $k => $v) {
$array2[$k + $diff] = $array1[$k];
}
}
you can get first keys of both arrays using function array_keys();
suppose, $keys1 contains keys of $array1 and $keys2 cotains keys of $array2
Then move into for loop as follows:
for($i=0 ; $i<count($array1) ; $i++)
{
$result[$keys2[$i+1]] = $array1[$i];
}
print_r($result);
Hope this will be helpful to you
foreach($array1 as $item => $value)
if(isset($array2[($item + 2)]) && item != 11)
$temp[$item + 2] = $value;
elseif($item == 11)
$temp[2] = $value;
$array1 = #$temp;
Example code.. but again, you need to tell us the pattern that determines where the first array elements are being inserted into the second array elements. I think it's.. +2? Maybe?

Categories