php array remove n amount of items based on value - php

I have this array, there are 4 values = 3, how can i delete just two of them?
Array (
[0] => 1
[1] => 2
[2] => 1
[3] => 2
[4] => 3
[5] => 3
[6] => 3
[7] => 2
[8] => 2
[9] => 2
[10] => 1
[11] => 2
[12] => 3
[13] => 2
)
I already tried unset(). is there any way to achieve this ?
so the array looks like this
Array (
[0] => 1
[1] => 2
[2] => 1
[3] => 2
[4] => 3
[5] => 3
[6] => 2
[7] => 2
[8] => 2
[9] => 1
[10] => 2
[11] => 2
)

Just make two calls to unset, using array_search:
$array = array(1,2,3,3,3,4,5,6);
print_r($array);
unset($array[array_search(3, $array)]);
unset($array[array_search(3, $array)]);
print_r($array);
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 3
[4] => 3
[5] => 4
[6] => 5
[7] => 6
)
Array
(
[0] => 1
[1] => 2
[4] => 3
[5] => 4
[6] => 5
[7] => 6
)
But this assumes that you are OK with the behavior of array_search, which would return the first index which matches the value 3. If you had some other order of removal in mind, then state it, and the code might have to be changed.

You might use a loop and skip the first 2 times you encounter a 3. Then for the next matches, you could use unset. Then you might use array_values to reset the keys:
$items = [
1,2,1,2,3,3,3,2,2,2,1,2,3,2
];
$found = 0;
foreach ($items as $key => $item) {
if ($item === 3) {
if ($found < 2) {
$found++;
continue;
}
unset($items[$key]);
}
}
print_r(array_values($items));
Result
Array
(
[0] => 1
[1] => 2
[2] => 1
[3] => 2
[4] => 3
[5] => 3
[6] => 2
[7] => 2
[8] => 2
[9] => 1
[10] => 2
[11] => 2
)
Php demo

Related

array combine is not working in php?

my code is:
array set 1:
Array
(
[0] => 15-3
[1] => 16-3
[2] => 15-4
[3] => 16-4
[4] => 15-3
[5] => 16-3
[6] => 15-4
[7] => 16-4
[8] => 15-3
[9] => 16-3
[10] => 15-4
[11] => 16-4
)
My second array set is:
Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 1
[4] => 2
[5] => 2
[6] => 2
[7] => 2
[8] => 3
[9] => 3
[10] => 3
[11] => 3
)
i just combine the above both the two array into one like below
$data1=array_combine($store_attri_ids, $store_ids);
but its shows like that
Array
(
[15-3] => 3
[16-3] => 3
[15-4] => 3
[16-4] => 3
)
The remaining values are not combined, wt we do nw????
You can try this by creating a sub-array -
$data1 = array();
foreach($store_attri_ids as $key => $id) {
$data1[$id][] = $store_ids[$key];
}
The output would be like -
Array
(
[15-3] => array(1, 2, 3),
[16-3] => array(...),
[15-4] => array(...),
[16-4] => array(...)
)
If you use array_combine the result is totally correct, like putting here: http://php.net/manual/en/function.array-combine.php
You need use array_merge, take a look at the documentation :http://php.net/manual/en/function.array-merge.php
Use like this:
$data1=array_merge($store_attri_ids, $store_ids);
print_r($data1);

how we remove index in the array and merge

Bellow is my Array in this array
****i want to remove second level index 1 and 2 and merge two arrays in one array****
$array1 = Array
(
[1] => Array
(
[1] => Array
(
[2] => 4
[3] => 2
[4] => 3
[5] =>
)
[2] => Array
(
[9] => 4
[10] =>
[11] => 4
[12] => 4
)
)
[2] => Array
(
[1] => Array
(
[2] => 3
[3] =>
[4] => 4.1428571428571
[5] => 5
)
[2] => Array
(
[9] => 3
[10] => 5
[11] => 5
[12] => 3
)
)
[3] => Array
(
[1] => Array
(
[2] => 5
[3] => 5
[4] =>
[5] => 4.1428571428571
)
[2] => Array
(
[9] => 3
[10] => 4.4285714285714
[11] => 4.4285714285714
[12] => 5
)
)
my aspected output is bellow where second level index is removed and arrays are merged
$array1= Array
(
[1] => Array
(
[2] => 4
[3] => 2
[4] => 3
[5] =>
[9] => 4
[10] =>
[11] => 4
[12] => 4
)
[2] => Array
(
[2] => 3
[3] =>
[4] => 4.1428571428571
[5] => 5
[9] => 3
[10] => 5
[11] => 5
[12] => 3
)
)
[3] => Array
(
[2] => 5
[3] => 5
[4] =>
[5] => 4.1428571428571
[9] => 3
[10] => 4.4285714285714
[11] => 4.4285714285714
[12] => 5
)
And i tried :
foreach($array1 as $arrkey => $arrvalue)
{
foreach($arrvalue as $innerkey => $innervalue){
//unset($cutvalu[$cutck]);
foreach($innervalue as $subkey => $subvalue){
#$array1[$arrkey][$subkey];
}
}
}
echo "<pre>"; print_r($array1);echo "<pre>";
Try this:
foreach($array1 as $arrkey => $arrvalue)
{
foreach($arrvalue as $innerkey => $innervalue){
foreach($innervalue as $subkey => $subvalue){
$new_array[$arrkey][$subkey] = $subvalue; // Change this line
}
}
}
echo "<pre>"; print_r($new_array);echo "<pre>";
This will give you what you're looking for.

Changing values in array based on values in another array

Here's an example in plain english of what I am trying to achieve:
if the number given is 4, then I want to add 1 to every value that is equal to or less than 4 into the corresponding index of another array. (hope that makes sense)
So my first array looks like this:
Array ( [0] => 5 [1] => 6 [2] => 7 [3] => 4 [4] => 3 [5] => 2 [6] => 9 [7] => 8 [8] => 1 [9] => 10 [10] => 11 [11] => 12 [12] => 13 [13] => 14 [14] => 15 [15] => 16 [16] => 17 [17] => 18 )
The second array looks like this:
Array ( [0] => 4 [1] => 3 [2] => 4 [3] => 4 [4] => 4 [5] => 5 [6] => 4 [7] => 4 [8] => 5 [9] => 4 [10] => 4 [11] => 5 [12] => 5 [13] => 4 [14] => 4 [15] => 4 [16] => 3 [17] => 3 )
And I am wanting the second array to look like this (after adding 1 to every value below 4 in the first array) so after the addition it would be
Array ( [0] => 4 [1] => 3 [2] => 4 [3] => 5 [4] => 5 [5] => 6 [6] => 4 [7] => 4 [8] => 6 [9] => 4 [10] => 4 [11] => 5 [12] => 5 [13] => 4 [14] => 4 [15] => 4 [16] => 3 [17] => 3 )
In which index 3,4,5,9 have changed.
I think you're looking for array_map
function increase( $m, $n )
{
if( $m < 4 )
{
return $n+1;
}
return $n
}
$arr1;
$arr2;
print_r( array_map("increase", $arr1, $arr2 ) );
Note: this will return a new array.
Using an array_walk
array_walk($arr2,function(&$v,$k) use($arr1) { if($arr1[$k]<=$v){ $v=$v+1;} });
Demo
[or]
A simple foreach will do
foreach($arr1 as $k=>$v)
{
if($v<=$arr2[$k])
{
$arr2[$k]=$arr2[$k]+1;
}
}
print_r($arr2);
Demo

Set array keys to value of another arrays values - PHP

I have the following:
( [0] => 3 [1] => 2 [2] => 12 [3] => 6 [4] => 8 [5] => 7 [6] => 9 [7] => 10 [8] => 5 [9] => 4 )
I want to use the value of each of those as the key in:
( [0] => 7 [1] => 2 [2] => 10 [3] => 3 [4] => 5 [5] => 6 [6] => 11 [7] => 9 [8] => 4 [9] => 8 )
I've tried this:
foreach ($iOrder as $i)
{
$pOrder[$i] = $pOrder[$p];
$p++;
}
I get this:
( [12] => 2 [10] => 6 [5] => 5 [4] => 7 )
Any thoughts?
Do you mean
$result = array_combine($keys, $values);
?
array_combine()
Frist array, $arr1,
( [0] => 3 [1] => 2 [2] => 12 [3] => 6 [4] => 8 [5] => 7 [6] => 9 [7] => 10 [8] => 5 [9] => 4 )
Second array, $arr2,
( [0] => 7 [1] => 2 [2] => 10 [3] => 3 [4] => 5 [5] => 6 [6] => 11 [7] => 9 [8] => 4 [9] => 8 )
If I understood correct, you want to use the values of $arr1 as keys in $arr2.
$values = array_values($arr1);
=> This gives you the values of $arr1.
you can use, array_combine($keys, $values);
so the resulting array would be,
$result_arr = array_combine(array_values($arr1), array_values($arr2));
However, it may not work as expected if the no of items in two arrays are different.

array manipulation

I have a two column array (array1), for each row of that array I need to compare the value in the 2nd column with a column value in each row of another array(array1) , when they equal I want to append another column value (from array2) to the first array.
in english:
if array1[x][1] = array2[y][0]
then array1[x][2] = array2[y][2]
screen dumps of both arrays
array1 (
[1] => Array (
[0] => 1 [1] => 2
)
[2] => Array (
[0] => 2 [1] => 3
)
[3] => Array (
[0] => 3 [1] =>
) [7] => Array (
[0] => 7 [1] => 1
)
[8] => Array (
[0] => 8 [1] => 1
)
[9] => Array (
[0] => 9 [1] => 10
)
[10] => Array (
[0] => 10 [1] => 2
)
)
array2 (
[0] => Array (
[0] => 1
[1] => 2
[2] => 2
[3] => Jane
[4] => Smith
[5] => jsmith#internet.com
[6] => jsmith
[7] => 12345
[8] => 1
[9] => no
)
[1] => Array (
[0] => 2
[1] => 2
[2] => 3
[3] => James
[4] => Beard
[5] => jasb#bellsouth.net
[6] => jbeard03
[7] => keeper
[8] => 1
[9] => no
)
[2] => Array (
[0] => 3
[1] => 2
[2] =>
[3] => Peter
[4] => Allen
[5] => pallen#rfgg.com
[6] => pallen
[7] => pallen
[8] => 1
[9] => no
)
[3] => Array (
[0] => 7
[1] => 2
[2] => 1
[3] => Joe
[4] => Blow
[5] => jasb#bellsouth.net
[6] => jblow
[7] => blow123
[8] => 5
[9] => yes
)
[4] => Array (
[0] => 8
[1] => 2
[2] => 1
[3] => John
[4] => Smith
[5] => logtest#bellsouth.net
[6] => jnsmith
[7] => jsmith123
[8] => 4
[9] => yes
)
[5] => Array (
[0] => 9
[1] => 2
[2] => 10
[3] => Frank
[4] => Smith
[5] => pallen#test.com
[6] => fsmith
[7] => fsmith123
[8] => 4
[9] => yes
)
[6] => Array (
[0] => 10
[1] => 2
[2] => 2
[3] => Loretta
[4] => Beard
[5] => lbeard#me.net
[6] => lbeard
[7] => lbeard123
[8] => 1
[9] => no
)
)
Does this work? I'm not sure I'm entirely clear on what you're looking for.
foreach($array1 as $x => $xarray) {
foreach($array2 as $y => $yarray) {
if($xarray[1] == $yarray[0]) {
$array1[$x][2] = $array[$y][2];
}
}
}
foreach ($array1 as &$a1) {
foreach ($array2 as $a2) {
if ($a1[1] == $a2[0]) {
$a1[] = $a2[2];
continue 2;
}
}
}
BTW, you should try to use explicit keys that actually mean something, e.g. $a1['id'] instead of $a1[1]. You'll thank yourself when you look at the code again two months down the road.
And because I threatened to do so:
btwyoushouldtrytouseexplicitkeysthatactuallymeansomethingeg$a1['id']insteadof$a1[1]youllthankyourselfwhenyoulookatthecodeagaintwomonthsdowntheroad ;-P

Categories