Unset value from array by value, not key - php

I have the following array:
Array (
[0] => 1
[1] => 2
[2] => 2
[3] => 4
[4] => 4
[5] => 8)
I want to remove some items of the array but by value, not by key. How I can do that if I want to remove all the items with the value "4", or with the value "x"?

Use array_search
$key = array_search(4, $arr);
unset($arr[$key]);
If occurences of value in array is more than once use array_keys:
$keys = array_keys($arr, 4);
foreach ($keys as $k)
unset($arr[$k]);

you could try it this way:
<?php
$data = array('haha', 'hehe', 'hihi', 'gtfo', 'hoho', 'huhu');
$data = preg_grep('/^(?!gtfo).*$/', $data);
print_r($data);
?>
Output:
Array
(
[0] => haha
[1] => hehe
[2] => hihi
[4] => hoho
[5] => huhu
)

Related

PHP: How to set value into empty index in an Array?

I have array in my PHP, for example:
array
Array
(
[0] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] => Grape
[6] => Apple
[7] => Pineaple
[8] => Avocado
[9] => Banana
)
)
and I need to fill the empty element (index 0 upto 4) with new value from an array or $variable.
Maybe for example, I get the data from another array:
newArray
Array
(
[0] => Array
(
[0] => Lemon
[1] => Lime
[2] => Mango
[3] => Watermelon
[4] => Starfruit
)
)
so I can get a result like this:
finalArray
Array
(
[0] => Array
(
[0] => Lemon
[1] => Lime
[2] => Mango
[3] => Watermelon
[4] => Starfruit
[5] => Grape
[6] => Apple
[7] => Pineaple
[8] => Avocado
[9] => Banana
)
)
Any help would be appreciated. Thanks
You can loop through your array, check if the index is empty and then, set it's value.
<?php
foreach($array as $index=>$value)
{
if(empty($array[$index]))
{
$array[$index] = $newValue;
}
}
<?php
$array=array("0"=>"","1"=>"","2"=>"","5"=>"Grape","6"=>"Apple","7"=>"Pineaple","8"=>"Avocado","9"=>"Banana");
echo "<pre>";print_r($array);echo"</pre>";
$newarray= array();
foreach($array as $key => $value){
if($value == ''){
//echo $key."<br>";
$value = 'Somevalue';
}
$newarray[] = $value;
//echo "<pre>";print_r($value);echo"</pre>";
}
echo "<pre>";print_r($newarray);echo"</pre>";
?>
Link
There is already a function in a standard library called array_replace. If the new values in the other array have the same indices you can use it:
$result = array_replace($array1, $array2);
If you just need to set up default values for empty elements use array_map:
$defaultValue = 'Foo';
$result = array_map(function ($item) use ($defaultValue) {
return $item ?: $defaultValue;
}, $array1);
Here is working demo.
Use only array_merge() function
For Example
print_r( array_merge($array, $newarray) );

How to concatenate arrays element recursively [duplicate]

This question already has answers here:
Joining similar data from two indexed arrays
(2 answers)
Closed 5 months ago.
I'm working on a project and I'm stacked since 2 days, this is my problem: I have two arrays and want to retrieve the second item in each object in Array_2 and concatenate it to the content of each object in first Array_1 in PHP.
Array_1
[[1453274700000,24011],[1453275000000,24222],[1453275300000,24284],[1453275600000,24331],...]
Array_2
[[1453274700000,51951],[1453275000000,52093],[1453275300000,52251],[1453275600000,52288],...]
Wanted_array
[[1453274700000,24011,51951],[1453275000000,24222,52093],[1453275300000,24284,52251],[1453275600000,24331,52288]...]
A functional solution:
$result = array_map(function (array $a1, array $a2) {
return array_merge($a1, [$a2[1]]);
}, $array_1, $array_2);
This assumes that all items are in order and only need to be merged by their order, not by their first value.
If you want $item[0] to define what "group" each value belongs to, you can iterate through the first array and save $item[0] as the key and $item[1] as the value. Do the same for the second array. Now iterate through the saved array for array1, and check if the saved array for array2 contains the same keys. Do the same for array2 (in case it has key that array1 doesn't have), and save it all to a new array:
<?php
$arr1 = array(
array('1453274700000',24011),
array('1453275000000',24222),
array('1453276000000',24222), // inexistent in $arr2
);
$arr2 = array(
array('1453275000000',52093),
array('1453274700000',51951),
array('1453273000000',24222), // inexistent in $arr1
);
$arr1dictionary = [];
$arr2dictionary = [];
$result = [];
foreach ($arr1 as $collection) {
$arr1dictionary[$collection[0]] = $collection[1];
}
foreach ($arr2 as $collection) {
$arr2dictionary[$collection[0]] = $collection[1];
}
foreach ($arr1dictionary as $key => $value) {
if (isset($arr2dictionary[$key])) {
$result[$key] = [$key, $value, $arr2dictionary[$key]];
} else {
$result[$key] = [$key, $value, null];
}
}
foreach ($arr2dictionary as $key => $value) {
if (isset($result[$key])) {
continue;
}
$result[$key] = [$key, null, $value];
}
$result = array_values($result);
print_r($result);
Output:
Array
(
[0] => Array
(
[0] => 1453274700000
[1] => 24011
[2] => 51951
)
[1] => Array
(
[0] => 1453275000000
[1] => 24222
[2] => 52093
)
[2] => Array
(
[0] => 1453276000000
[1] => 24222
[2] => (null, the value only exists in $arr1)
)
[3] => Array
(
[0] => 1453273000000
[1] => (null, the value only exists in $arr2)
[2] => 24222
)
)
DEMO
Use array_walk and add second item from $array2 if it exists.
$array1 = array(
array(1453274700000,24011),
array(1453275000000,24222),
array(1453275300000,24284),
array(1453275600000,24331)
);
$array2 = array(
array(1453274700000,51951),
array(1453275000000,52093),
array(1453275300000,52251),
array(1453275600000,52288),
);
array_walk($array1, function(&$item, $key) use ($array2){
if(isset($array2[$key][1])){
$item[] = $array2[$key][1];
}
});
print_r($array1);
Output
Array
(
[0] => Array
(
[0] => 1453274700000
[1] => 24011
[2] => 51951
)
[1] => Array
(
[0] => 1453275000000
[1] => 24222
[2] => 52093
)
[2] => Array
(
[0] => 1453275300000
[1] => 24284
[2] => 52251
)
[3] => Array
(
[0] => 1453275600000
[1] => 24331
[2] => 52288
)
)
EDIT
As #h2ooooooo pointed out that there could be possibility that array items are in random order. If array items can be in random order and they are matched with first index value, use this (works with PHP >= 5.5.0):
$array1 = array(
array(1453274700000,24011),
array(1453275000000,24222),
array(1453275300000,24284),
array(1453275600000,24331),
array(1453276000000,24222) // no match in $array2
);
$array2 = array(
array(1453275000000,52093),
array(1453274700000,51951),
array(1453275300000,52251),
array(1453275600000,52288),
);
array_walk($array1, function(&$item, $key) use ($array2){
// Find match in $array2
$array2_key = array_search($item[0], array_column($array2, 0));
// If match found
if($array2_key !== false && isset($array2[$array2_key][1])){
$item[] = $array2[$array2_key][1];
}
// No match
else{
$item[] = null;
}
});
print_r($array1);
OUTPUT
Array
(
[0] => Array
(
[0] => 1453274700000
[1] => 24011
[2] => 51951
)
[1] => Array
(
[0] => 1453275000000
[1] => 24222
[2] => 52093
)
[2] => Array
(
[0] => 1453275300000
[1] => 24284
[2] => 52251
)
[3] => Array
(
[0] => 1453275600000
[1] => 24331
[2] => 52288
)
[4] => Array
(
[0] => 1453276000000
[1] => 24222
[2] =>
)
)

How to remove the parent numeric keys of php array

I've an array in php something like below
Array
(
[0] => Array
(
[0] => 40173
[1] => 514081
[2] => 363885
[3] => 891382
),
[1] => Array
(
[0] => 40173
[1] => 5181
[2] => 385
[3] => 891382
)
)
Now I want to remove the parents indexes 0,1... and finally want to get all the values (only unique values).
Thanks.
One possible approach is using call_user_func_array('array_merge', $arr) idiom to flatten an array, then extracting unique values with array_unique():
$new_arr = array_unique(
call_user_func_array('array_merge', $old_arr));
Demo. Obviously, it'll work with array of any length.
$startArray = Array
(
[0] => Array
(
[0] => 40173
[1] => 514081
[2] => 363885
[3] => 891382
),
[1] => Array
(
[0] => 40173
[1] => 5181
[2] => 385
[3] => 891382
)
);
//Edited to handle more the 2 subarrays
$finalArray = array();
foreach($startArray as $tmpArray){
$finalArray = array_merge($finalArray, $tmpArray);
}
$finalArray = array_unique($finalArray);
Using RecursiveArrayIterator Class
$objarr = new RecursiveIteratorIterator(new RecursiveArrayIterator($yourarray));
foreach($objarr as $v) {
$new_arr[]=$v;
}
print_r(array_unique($new_arr));
Demo
OUTPUT:
Array
(
[0] => 40173
[1] => 514081
[2] => 363885
[3] => 891382
[5] => 5181
[6] => 385
)
$new_array = array_merge($array1, $array2);
$show_unique = array_unique($new_array);
print_r($show_unique);
array_merge is merging the array's, array_unique is removinge any duplicate values.
Try something like this:
$new_array = array();
foreach($big_array as $sub_array) {
array_merge($new_array, $sub_array);
}
$new_array = array_unique($new_array);
(code not tested, this just a concept)
Try this:
$Arr = array(array(40173, 514081, 363885, 891382),
array(40173,5181, 385,891382));
$newArr = array();
foreach($Arr as $val1)
{
foreach($val1 as $val2)
{
array_push($newArr, $val2);
}
}
echo '<pre>';
print_r(array_unique($newArr));
Output:
Array
(
[0] => 40173
[1] => 514081
[2] => 363885
[3] => 891382
[5] => 5181
[6] => 385
)
Refer: https://eval.in/124240
--
Thanks

sort results of array count values into a new array

I want to sort the results to a new array but without the $key
so the most UNunique number will be first (or the most duplicated number will be first).
<?php
$a = array (1,1,2,2,2,3,3,3,3,4,4,4,4,4,5);
foreach (array_count_values($a) as $key => $value) {
echo $key.' - '.$value.'<br>';
}
//I am expecting to get the most duplicated number FIRST (without the $key)
//so in that case :
// $newarray = array(4,3,2,1,5);
?>
$a = array (1,1,2,2,2,3,3,3,3,4,4,4,4,4,5);
$totals = array_count_values($a);
arsort( $totals );
echo "<pre>";
print_r($totals);
Output
Array
(
[4] => 5
[3] => 4
[2] => 3
[1] => 2
[5] => 1
)
Do like this
<?php
$a = array (1,1,2,2,2,3,3,3,3,4,4,4,4,4,5,6,7,8,9);
$n=array_count_values($a);
arsort($n);
print_r(array_keys($n));
Demo
OUTPUT:
Array
(
[0] => 4
[1] => 3
[2] => 2
[3] => 1
[4] => 9
[5] => 8
[6] => 5
[7] => 6
[8] => 7
)
$newarray = array_keys(array_count_values($a));

Replacement of array to the number from two-dimensional array

I have two-dimensional array and I want replace second of two-dimensional array on random number of second array
Array
(
[1] => Array
(
[0] => 1
[1] => 3
[2] => 5
[3] => 500
[4] => 600
[5] => 700
)
[2] => Array
(
[0] => 2
[1] => 4
[2] => 6
)
)
I want get
Array (
[1] => 5 (<- random from first array)
[2] => 6 (<- random from second array)
)
I tried to do:
foreach($variables as $key => $val) {
$variables = str_replace($val, $val[array_rand($val)], $variables);
}
Why it doesnt work?
foreach($variables as $key => $val) {
$variables[$key] = $val[array_rand($val)];
}
foreach ($variables as &$var) {
$var = array_rand($variables[$var]);
}

Categories