Deleting values in an array from another array - php

I have 2 arrays and I would like to delete everything in the first array that is in the second array. In thise case I would like to delete "ibomber" and "apphero" in array one. I tried something with unset, but it doesn't look like it works.
array (size=5)
0 => string 'Air Hippo' (length=9)
1 => string 'iBomber Defense Pacific' (length=23)
3 => string 'AppHero' (length=7)
5 => string 'Pillboxie' (length=9)
6 => string 'Monogram' (length=8)
array (size=2)
0 => string ' iBomber Defense Pacific' (length=24)
1 => string ' AppHero' (length=8)
This is what I tried:
foreach ($_SESSION["appsarray"] as $k=>$v)
{
foreach ($_SESSION["finalapps"] as $k2=>$v2)
{
if ($v == $v2)
{
unset ($_SESSION["appsarray"][$k]);
}
}
}
Session appsarray is my first array and session finalapps is my second array.
Thanks!

function TrimmedStrCaseCmp($str1,$str2)
{
return strcasecmp(trim(str1),trim(str2));
}
$result = array_udiff(values,to_remove_from_values,'TrimmedStrCaseCmp');
http://php.net/manual/en/function.array-udiff.php

You're looking for array_diff i.e.;
$appsarray = array('Air Hippo','iBomber Defense Pacific','AppHero','Pillboxie','Monogram');
$finalapps = array('iBomber Defense Pacific','AppHero');
$result = array_diff($appsarray,$finalapps);
print_r($result);
Will output;
Array ( [0] => Air Hippo [3] => Pillboxie [4] => Monogram )

Related

php array count values in 4 deep array

Im trying to count how many times a delivery date is in my array but i seem to only be able to count the first level.
array (size=48)
'2000-01-01' =>
array (size=2)
'date' => string '2000-01-01' (length=10)
0 =>
array (size=2)
'van' => string '0' (length=1)
0 =>
array (size=619)
'drop' => string '0' (length=1)
0 =>
array (size=29)
'id' => string '18137' (length=5)
'order_number' => string '13550' (length=5)
'reference' => string '' (length=0)
'delivery_date' => string '2000-01-01' (length=10)
I've tried:
$counts = array_count_values(array_flip(array_column($output, 'delivery_date')));
and
$array = array_map(function($element){
return $element['delivery_date'];
}, $output);
$array2 = (array_count_values($array));
print_r($array2);
in the end i either end up with a array to string error or the value 1.
how Would i go about counting these?
Thanks.
You could make use of array_walk_recursive and increment an array value every time the delivery_date key is present in the array at any level:
$counts = [];
array_walk_recursive(
$output,
static function ($value, string $key) use (&$counts): void {
if ($key === 'delivery_date') {
$counts[$value] = ($counts[$value] ?? 0) + 1;
}
}
);

slice and merge 2 arrays

is there a good way to slice and merge 2 arrays based on empty values for example
first array
0 => string 'Perfect all gorgeous and arrived in less than 1 month for brazil' (length=64)
1 => string '' (length=0)
2 => string '' (length=0)
3 => string 'Good figures for their money, only instead of bits normal stick child bit rastroilsya' (length=85)
4 => string '' (length=0)
5 => string '' (length=0)
second array
0 => string '' (length=0)
1 => string 'http://g01.a.alicdn.com/kf/UTB8jjnecFfFXKJk43Otq6xIPFXaw.jpg" data-eid="eid-201782563197' (length=88)
2 => string 'http://g01.a.alicdn.com/kf/UTB87.bdcNHEXKJk43Jeq6yeeXXaZ.jpg" data-eid="eid-201782563197' (length=88)
3 => string '' (length=0)
4 => string 'http://g01.a.alicdn.com/kf/UTB8cxXwg4HEXKJk43Jeq6yeeXXam.jpg" data-eid="eid-201833045441' (length=88)
5 => string 'http://g04.a.alicdn.com/kf/UTB824Xwg4HEXKJk43Jeq6yeeXXaB.jpg" data-eid="eid-201833045441' (length=88)
I want them to be like this array
array (size=2)
0 =>
array (size=2)
'comment' => string 'Perfect all gorgeous and arrived in less than 1 month for brazil' (length=64)
'images' =>
array (size=2)
0 => string 'http://g01.a.alicdn.com/kf/UTB8jjnecFfFXKJk43Otq6xIPFXaw.jpg" data-eid="eid-201782563197' (length=88)
1 => string 'http://g01.a.alicdn.com/kf/UTB87.bdcNHEXKJk43Jeq6yeeXXaZ.jpg" data-eid="eid-201782563197' (length=88)
1 =>
array (size=2)
'comment' => string 'Good figures for their money, only instead of bits normal stick child bit rastroilsya' (length=85)
'images' =>
array (size=2)
3 => string 'http://g01.a.alicdn.com/kf/UTB8cxXwg4HEXKJk43Jeq6yeeXXam.jpg" data-eid="eid-201833045441' (length=88)
4 => string 'http://g04.a.alicdn.com/kf/UTB824Xwg4HEXKJk43Jeq6yeeXXaB.jpg" data-eid="eid-201833045441' (length=88)
How to do it ?
Got something that will help. It will work with more inputs if you need. It might not work best if your second array has more than one breaking blank. Just working on updated code to solve such issues.
<?php
$arr1 = array("input", "", "", "another input", "", "", "yet another input", "");
$arr2 = array("", "p1", "p2", "", "p01", "p02", "","p11" );
$inp = array("comment" => $arr1, "images" => $arr2);
function mangle_arrays($input) {
$out = array();
$gen = 0;
foreach($input as $key=>$val) {
$id = $gen?-1:0;
if ($gen) {
foreach($val as $v) {
if ($v) {
$out[$id][$key][] = $v;
} else {
$id++;
}
}
} else {
foreach($val as $v) {
if ($v) {
$out[$id] = array();
$out[$id][$key] = $v;
$id++;
}
}
}
$gen++;
}
return $out;
}
// your code goes here
echo "<pre>";
print_r(mangle_arrays($inp));
Results
Array
(
[0] => Array
(
[comment] => input
[images] => Array
(
[0] => p1
[1] => p2
)
)
[1] => Array
(
[comment] => another input
[images] => Array
(
[0] => p01
[1] => p02
)
)
[2] => Array
(
[comment] => yet another input
[images] => Array
(
[0] => p11
)
)
)

Convert array index to custom value?

I have an array $indexedarray
printr($indexedarray) gives something like this
array (size=3)
0 => string 'Homes' (length=5)
1 => string 'Apartments' (length=10)
2 => string 'Villas' (length=6)
I want to change this arrays index also same as value, like
array (size=3)
'Homes' => string 'Homes' (length=5)
'Apartments' => string 'Apartments' (length=10)
'Villas' => string 'Villas' (length=6)
is it posssible??
You can use array_combine:
$indexedarray= ['Homes', 'Apartments', 'Villas'];
print_r(array_combine($indexedarray, $indexedarray));
Gives:
Array
(
[Homes] => Homes
[Apartments] => Apartments
[Villas] => Villas
)
But be aware that your duplicate values will be dropped. Keys will be unique!
Try This :
$myArray = [
0 => 'Homes',
1 => 'Apartments',
2 => 'Villas' ];
$newArray = [];
foreach($myArray as $key => $value){
$newArray[$value] = $value;
}
var_dump($newArray);

Explode Funciton not working in PHP Shows error line expects parameter 2

here is $_POST['members'] and I want to explode it with |
[members] => Array
(
[0] => test.com|test Melissa
[1] => eboo#abcd.com.au|Buckley test
[2] => testtest#test.com.au|test Ashley
[3] => testset.com.au|Forno test
[4] => get.com.au|test Nathan
[5] =>set.com.au|Brown test
)
I am trying with follows php code
$get=explode('|',$_POST['members']);
echo '<pre>';
print_r($get);
try something like
foreach($_POST['members'] as $str){
$get[] = explode('|',$str);
}
print_r($get);
as $_POST['members'] is array, you need to use explode in foreach by accessing all array elements:
foreach($_POST['members'] as $members)
{
$get=explode('|',$members);
echo '<pre>'; print_r($get);
}
Loop Through the Data Array, store the result of your Explode in a new Array and perhaps you got what you want like so:
<?php
$arr = [
0 => "test.com|test Melissa",
1 => "eboo#abcd.com.au|Buckley test",
2 => "testtest#test.com.au|test Ashley",
3 => "testset.com.au|Forno test",
4 => "get.com.au|test Nathan",
5 => "set.com.au|Brown test"
];
$arrSubData = array();
foreach($arr as $pipeDividedString){
$arrSubData[] = explode('|', $pipeDividedString);
}
var_dump($arrSubData);
DUMPS
array (size=6)
0 =>
array (size=2)
0 => string 'test.com' (length=8)
1 => string 'test Melissa' (length=12)
1 =>
array (size=2)
0 => string 'eboo#abcd.com.au' (length=16)
1 => string 'Buckley test' (length=12)
2 =>
array (size=2)
0 => string 'testtest#test.com.au' (length=20)
1 => string 'test Ashley' (length=11)
3 =>
array (size=2)
0 => string 'testset.com.au' (length=14)
1 => string 'Forno test' (length=10)
4 =>
array (size=2)
0 => string 'get.com.au' (length=10)
1 => string 'test Nathan' (length=11)
5 =>
array (size=2)
0 => string 'set.com.au' (length=10)
1 => string 'Brown test' (length=10)
$_POST['members'] is an array and explode(); works on strings. You'll have to loop through the array and explode each value. Something like this :
$arr = array(
'test.com|test Melissa',
'eboo#abcd.com.au|Buckley test',
'testtest#test.com.au|test Ashley',
'testset.com.au|Forno test',
'get.com.au|test Nathan',
'set.com.au|Brown test'
);
$get = array();
foreach ($_POST['members'] as $member) {
$get[] = explode('|',$member);
}
echo '<pre>'; print_r($get);
In order to explode the values you need to loop trough $_POST['members'], for that you can use foreach().
The following, is an example that uses explode() and list() :
<?php
$members = !empty($_POST['members']) ? $_POST['members'] : die("post members is empty");
foreach($members as $member)
{
list($siteEmail, $name) = explode("|", $member);
echo "<pre> $siteEmail $name </pre>";
}
http://ideone.com/qdDmSe
You can use like this
foreach($_POST['members'] as $val){
$get = explode('|',$val);
}
echo "<pre>";print_r($get);

count from several multidimensional arrays

i have foreach, which generate following arrays:
==== array 1 ====
array
0 =>
array
'tag' => string 'daf' (length=3)
1 =>
array
'tag' => string 'daa' (length=3)
2 =>
array
'tag' => string 'daf' (length=3)
3 =>
array
'tag' => string 'daaa' (length=4)
4 =>
array
'tag' => string 'daf' (length=3)
5 =>
array
'tag' => string 'daa' (length=3)
6 =>
array
'tag' => string 'daf' (length=3)
7 =>
array
'tag' => string 'daf' (length=3)
8 =>
array
'tag' => string 'daf' (length=3)
9 =>
array
'tag' => string 'abd' (length=3)
10 =>
array
'tag' => string 'abdaa' (length=5)
11 =>
array
'tag' => string 'abda' (length=4)
==== array 2 ====
array
0 =>
array
'tag' => string 'daf' (length=3)
1 =>
array
'tag' => string 'test1' (length=5)
As output i want to get something like:
array
'daf' => '7'
'daa' => '2'
'daaa' => '1'
'abd' => '1'
'abdaa' => '1'
'abda' => '1'
'test1' => '1'
The value of the new array is the count of the element from all aray generatet from the loop. array_count_values() doesn't work here...any suggestions, how to solve the problem?
Did not notice it was 2 dimensional array.
Here is another code.
var_export(
array_count_values(
call_user_func_array('array_merge', array_merge($array1, $array2))
)
);
Something a bit like this should work:
$result = array();
foreach (array_merge($array1, $array2) as $item) {
$name = $item['tag'];
if (!isset($result[$name])) {
$result[$name] = 0;
}
$result[$name]++;
}
Let's make some use of the Standard PHP Library (SPL).
You can "flatten" an array with an RecursiveArrayIterator and RecursiveIteratorIterator. As a result you get an iterator that visits each leaf of your n-dimensional array and still let's you access the actual key of the element. In the next step concat both RecursiveIteratorIterators with an AppendIterator acting like a single interator that visits each element in all of its inner (appended) iterators.
$ai = new AppendIterator;
$ai->append(new RecursiveIteratorIterator(new RecursiveArrayIterator($array1)));
$ai->append(new RecursiveIteratorIterator(new RecursiveArrayIterator($array2)));
$counters = array();
foreach($ai as $key=>$value) {
if ( 'tag'===$key ) {
// # because I don't care whether this array element exists beforehand or not.
// $value has to be something that can be used as an array key (strings in this case)
#$counters[$value] += 1;
}
}
If you want you can even use a FilterIterator instead of the if('tag'===$key). But imho this doesn't increase the readability/value of the code ;-)

Categories