I need to remove Jackie Jackson from this array, I tried unset(I do not want to use key), array_diff, array_search. Nothing is working for me.
$employeeList= array(
array(
"ID" => "ID",
"Name" => "Name",
"Surname" => "Surname",
),
array(
"ID" => 1,
"Name" => "John",
"Surname" => "Smith",
),
array(
"ID" => 2,
"Name" => "Jackie",
"Surname" => "Jackson",
),
array(
"ID" => 3,
"Name" => "Chris",
"Surname" => "Jones",
),
array(
"ID" =>4,
"Name" => "Amanda",
"Surname" => "Cullen",
),
array(
"ID" =>5,
"Name" => "Jeremy",
"Surname" => "Goodwin",
),
);
if you want to unset you can use the id of Jackie Jackson to match his key.
$id = 2;
unset($employeeList[array_search($id,array_column($employeeList, "ID"))]);
Have fun :)
Array filter to check if the name and surname match. The string must contain name and surname separated by a space in this case so we can "explode" and pick up each individually. If name or surname do not exist in the "name_to_exclude" the function will simply return the original array.
$name_to_exclude = "Jackie Jackson";
$exclude = explode(' ', $name_to_exclude);
$employeeList = array_filter($employeeList, function($val) use ($exclude) {
if(array_key_exists(0, $exclude) && array_key_exists(1, $exclude)) {
return $val['Name'] != $exclude[0] && $val['Surname'] != $exclude[1];
}
return true;
});
Related
This question already has answers here:
Filter/Remove rows where column value is found more than once in a multidimensional array
(4 answers)
Closed 10 months ago.
I have the following Associative Array below, and I wish to remove from the $filtered_results or $results the duplicated values based on the nid (number_id) field.
The solution I came up with, was to do a foreach loop, populate the new array with the key unique value that I wish and then return the array_values of that new array.
Is there a more elegant solution to tackle this problem ?
The order is irrelevant ( Replacing or keeping the current value);
Solution 1: Replacing the current value for the next.
$filtered_results = [];
foreach ($results as $k => $v) {
$filtered_results[ $v['nid'] ] = $v ;
}
return array_values ( $filtered_results ) ;
Solution 2: Keeping the first value found.
$filtered_results = [];
foreach ($results as $k => $v) {
if ( isset( $filtered_results[ $v['nid'] ] )) {
continue;
}
$filtered_results[ $v['nid'] ] = $v ;
}
Associative Array
$results = [
[
"date" => "2019-03-09",
"name" => "NameOne",
"phone" => "56784678",
"nid" => 1,
],
[
"date" => "2019-03-09",
"name" => "NameTwo",
"phone" => "123123123",
"nid" => 2,
],
[
"date" => "2019-03-09",
"name" => "NameThree",
"phone" => "6784568",
"nid" => 3,
],
[
"date" => "2019-03-09",
"name" => "NameFour",
"phone" => "90909090",
"nid" => 2,
],
[
"date" => "2019-03-09",
"name" => "NameFive",
"phone" => "3456356",
"nid" => 1,
],
];
Filtered Results
$filtered_results = [
[
"date" => "2019-03-09",
"name" => "NameThree",
"phone" => "6784568",
"nid" => 3,
],
[
"date" => "2019-03-09",
"name" => "NameFour",
"phone" => "90909090",
"nid" => 2,
],
[
"date" => "2019-03-09",
"name" => "NameFive",
"phone" => "3456356",
"nid" => 1,
],
]
Since is a duplicated question, the best solution I have found is :
array_values(array_column($results,NULL,'nid'))
Demo : https://onlinephp.io/c/fe78a
return array_intersect_key($results, array_unique(array_column($results, 'nid')));
use this :
$filtered_results = array_combine(array_column($results, 'nid'), $results);
I have a json array and i want to filter an json object which **state **keys contains this word "ALTS".
<pre>
$arr = array(
array(
"region" => "valore1",
"price" => "valore11",
"state" => "declare par /AMG/OMS/FRA/"
),
array(
"region" => "valore2",
"price" => "valore22",
"state" => "declare par /AMG/OMS/ALTS/"
),
array(
"region" => "valore4",
"price" => "valore44",
"state" => "declare par /AMG/OMS/FRA/"
),
array(
"region" => "valore5",
"price" => "valore55",
"state" => "declare par /AMG/OMS/ALTS/"
),
array(
"region" => "valore3",
"price" => "valore33",
"state" => "declare par /AMG/OMS/ALTS/PT"
)
);
$myJsonArray = json_encode($arr);
echo $myJsonArray;
</pre>
I want to find an example of regular expression which filters this array and only returns an array containing objects with a state containing "/ ALTS".
Any ideas ?
You don't need regex for that. If you find one word you can use strpos function.
$newArr = [];
$findTerm = '/ALTS/';
foreach($arr as $key => $valArr)
{
if(strpos($valArrp['state'], $findTerm) !== false) {
$newArr[] = $valArr;
}
}
There are 2 arrays Say
1. Groups
$groups = array("user", "account", "client")
2. Results
$results = array(
0 => array(
"user" => "U1",
"account" => "A1",
"client" => "C1"
),
1 => array(
"user" => "U1",
"account" => "A2",
"client" => "C1"
),
0 => array(
"user" => "U1",
"account" => "A3",
"client" => "C1"
),
0 => array(
"user" => "U1",
"account" => "A2",
"client" => "C2"
),
0 => array(
"user" => "U1",
"account" => "A1",
"client" => "C4"
),
0 => array(
"user" => "U1",
"account" => "A1",
"client" => "C5"
),
0 => array(
"user" => "U1",
"account" => "A2",
"client" => "C5"
)
) ;
I want following OUTPUT
$output = array(
"U1" => array(
"A1" => array(C1,C4,C5),
"A2" => array(C1,C2,C5),
"A3" => array(C1)
)
);
The Groups array values are dynamic and may be any order. I want output in order the first value of groups array is the parent element of Output array and second value of group array is the child of parent Output array and so on.
I hope you are using PHP greater or equal to 5.6. There we have ... (splat operator) that will come really handy:
$output = [];
array_map(function (...$keys) use (&$output) {
// Pop the last key, because it is actually a value.
$value = array_pop($keys);
// Prepare "element" to assign the value to using keys and references.
$element = &$output;
while($key = array_shift($keys)) {
if (!isset($element[$key])) {
$element[$key] = [];
}
$element = &$element[$key];
}
$element[] = $value;
}, ...array_map(function ($group) use ($results) {
return array_column($results, $group);
}, $groups));
Here is working demo.
In short, we take advantage of the ability of array_map to take any number of arrays as arguments and traverse them kinda parallelly.
Simply make a foreach loop like this
$new = array();
foreach($results as $key=>$value){
$new[$value["user"]][$value["account"]][] = $value["client"];
}
print_r($new);
live demo : https://eval.in/857969
Using $groups : https://eval.in/857970
$new = array();
foreach($results as $key=>$value){
$new[$value[$groups[0]]][$value[$groups[1]]][] = $value[$groups[2]];
}
Example for multiple user's : https://eval.in/857973
Update
For dynamic groups array : You can use eval if you are not interacting user input here. : https://eval.in/858306
For example a multidimensional array like an example below
$arr = array(
[H1] => array(
"name" => "A"
"title" => "T1"
)
[H2] => array(
"name" => "B"
"title" => "B1"
)
)
Let's say I would like to search name which equals to A in $arr and if it's matched, the searching should return the key which is H1
How can I do that in php ?
I tried array_keys($arr, "A") but it returns me with an array instead of the key.
This may help -
$arr = array(
'H1' => array(
"name" => "A",
"title" => "T1",
),
'H2' => array(
"name" => "B",
"title" => "B1",
)
);
// Generate a new array with 'keys' and values in 'name'
$new = array_combine(array_keys($arr), array_column($arr, 'name'));
// Search in that new array
$search = array_search('A', $new);
var_dump($search);
Output
string(2) "H1"
Demo
Another simple way would be -
$serach= false;
foreach($arr as $key => $val) {
if($val['name'] == 'A') {
$search= $key;
break;
}
}
var_dump($search);
Suppose i have a array like this :
Array(
'1' => Array(
"ID" => 1,
"Name" => "name 1"
),
'2' => Array (
Array(
"ID" => 2,
"Name" => "name 2"
)
),
'3' => Array(
Array(
Array(
Array(
"ID" => 3,
"Name" => "name3"
)
)
),
'4' => Array (
Array {
"ID" => 4,
"Name" => "name 4"
),
Array(
"ID" => 5,
"Name" => "name 5"
),
Array(
"ID" => 6,
"Name" => "name 6"
)
);
number of sub-arrays is not ordered it may be 3, 4 or 5 etc...
and i wanted to get :
Array(
Array( "ID" => 1, "Name" => "name 1"),
Array( "ID" => 2, "Name" => "name 2"),
Array( "ID" => 3, "Name" => "name 3"),
Array( "ID" => 4, "Name" => "name 4"),
Array( "ID" => 5, "Name" => "name 5"),
Array( "ID" => 6, "Name" => "name 6"));
Is there an easy way to do this ?
EDIT :
Edited the above array to add :
'4' => Array (
Array {
"ID" => 4,
"Name" => "name 4"
),
Array(
"ID" => 5,
"Name" => "name 5"
),
Array(
"ID" => 6,
"Name" => "name 6"
)
);
which aren't nested but i still want them to be in my final array.
Thanks.
//Assuming your data is in $in
$out=array();
foreach($in as $k=>$v) {
while ((is_array($v)) && (isset($v[0]))) $v=$v[0];
//See below for next line
$out[]=$v;
}
print_r($out);
With the marked line being either $out[$k]=$v; or $out[]=$v; depending on wether you want to keep the 1st-level keys. In your desired output you do not ,so use the shown version
Edit
With you changed input array, you need to do something like
function addtoarray($inarray, &$outarray) {
foreach ($inarray as $i) {
if (!is_array($i)) continue;
if (isset($i['ID'])) $outarray[]=$i;
else addtoarray($i,$outarray);
}
}
$out=array();
addtoarray($in,$out);
print_r($out);
This was tested against your new input data
You could recurse through the array and check for the ID field.
function combine_array(array $src, array &$dest)
{
if( isset( $src['ID'] ) ) {
$dest[] = $src;
return;
}
foreach( $sub in $src ) {
combine_array( $sub, $dest );
}
}
Just wrote this off the top of my head, no testing, so it might have a few problems. But that's the basic idea.
This may work for you, assuming $array is the variable and php 5.3
//process
$array = array_map(function($block) {
$return = '';
foreach ($block as $sub) {
if (true === isset($sub['ID']) {
$return = $block;
break;
}
}
return $block;
}, $array);
array_filter($array); //remove empty elements
Wrote up a quick recursive function. You'll need to write any appropriate error handling and check the logic, since a discrepancy in your data could easily turn this into an infinite loop:
$output = array();
foreach ( $data as $d ) {
$output[] = loop_data( $d );
}
function loop_data( $data ) {
// Check if this is the right array
if( ! array_key_exists( "ID", $data ) ) {
// Go deeper
$data = loop_data( $data[0] );
}
return $data;
}