Get values (plural) from multi-dimensional array with key - php

I know the key, now I need ALL of the results that would yield when I search the 5000+ user database. Any user may have none, one or multiple locations, identified by an id and name field. Therefore I need the results in an array, not just the first/last one, but all of them. Below is an example of a user (actual array setup, fake data ;) )
Array
(
[ID] => 2
[user_login] => SomeGuy
[user_pass] => GreatEncryptedPassword
[user_nicename] => Some Guy
[user_email] => someguy#has-email.com
[user_url] => someguy.com
[user_registered] => 2013-04-11 11:18:58
[user_activation_key] =>
[user_status] => 0
[display_name] => Some Guy
[umeta_id] => 31
[user_id] => 2
[meta_key] => facebookmeta
[meta_value] => Array
(
[id] => 1234567890
[name] => Some Guy
[first_name] => Some
[last_name] => Guy
[link] => http://www.facebook.com/someguy
[username] => someguy
[birthday] => 02/21/1983
[location] => Array
(
[id] => 108276092536515 //actual ID, try at facebook.com/108276092536515
[name] => Arnhem, Netherlands
)
[gender] => male
[relationship_status] => In a Relationship
[significant_other] => Array
(
[name] => Some Chick
[id] => 12345678906789
)
[email] => someguy#has-email.com
[timezone] => 2
[locale] => nl_NL
[verified] => 1
[updated_time] => 2013-04-02T09:28:30+0000
)
)
As you can make out, this guy has 1 location in his facebook data, but other locations include the same [location] => Array ( [id] => 123 [name] => SomePlace ) for example for [work] or [education], or, or, or...
I've tried a recursive function like this:
function get_value_by_key( $array, $key ){
foreach( $array as $k => $each ){
if( $k == $key ){
return $each;
}
if( is_array( $each )){
if( $return = get_value_by_key($each,$key)){
return $return;
}
}
}
}
print_r( get_value_by_key( $users, 'location' );
How can I modify the above function to return an array of location arrays? This one return the above data of the first user containing a location, not the location itself or an array of them.
EDIT:
What I need as a result is something like this:
Array(
[0] => Array(
[id] => 1234567890
[name] => GreatCity, World
)
[1] => Array(
[id] => 2345678901
[name] => SomeCity, Blop
)
[2] => Array(
[id] => 3456789012
[name] => AnotherCity, Boo
)
)
Edit based on answer
Answer from Thomas David Plat gives me the correct results, but still a not very usable data structure. How to get the results out of the structure and into that shown in the above edit?
Results from David's answer:
Array
(
[0] => Array
(
[0] => Array
(
[0] => Array
(
[id] => 108276092536515
[name] => Arnhem, Netherlands
)
[1] => Array()
)
)
[1] => Array
(
[0] => Array
(
[0] => Array
(
[id] => 108276092536515
[name] => Arnhem, Netherlands
)
[1] => Array()
)
)
[2] => Array()
[3] => Array()
[4] => Array()
I've tried variations of array_filter, array_map, array_values and others like below:
$locations = array_map( 'array_filter', find_array_values( $users, 'location' ));
But the structure seems to stay...

UPDATED ANSWER
$matches = array();
function find_array_values($haystack, $needle)
{
global $matches;
$collection = array();
foreach($haystack AS $key => $value)
{
if($key === $needle)
{
$matches[] = $value;
}
else if(is_array($value))
{
find_array_values($value, $needle);
}
}
}
find_array_values($array, 'location');
print_r($matches);
?>
This will work, but as I already said in the comments it's a bad practice since it uses the global keyword. If you implement this function as a method into a class, instead of using the global keyword you could use a class variable which would be a neat solution.
OLD Answer
function find_array_values($haystack, $needle)
{
$collection = array();
foreach($haystack AS $key => $value)
{
if($key === $needle)
{
$collection[] = $value;
}
else if(is_array($value))
{
$collection[] = find_array_values($value, $needle);
}
}
return $collection;
}
echo "<pre>";
print_r(find_array_values($users, 'location'));
echo "</pre>";
Here you are. Just pass an array of your users into the function. The function will return an array of arrays of locations.
There's one restriction. The function will not search trough locations that are within locations.

you can modify ur location part of your array like this
[location] => Array
(
[work]=> Array(
[id] => 108276092536515
[name] => Arnhem, Netherlands
)
[home] => Array(
[id] => 108276092536515
[name] => Arnhem, Somewhere else
)
)
if you modify your array like this then your function will return array of arrays for location

Related

php recursive function doesn't work

I have a problem to understand why this doesn't work. I have recursive function who check if value in multidimensional array exists.
Then I have get_teams() function from which I want to return only unique values. I check for unique values with my find_value() function but it still return all the values.. Can someone explain me what happens? Thanks in advance
function find_value( $array, $searched_val ) {
foreach ( $array as $key => $val ) {
if ( $val === $searched_val ) {
return true;
}
if ( is_array( $val ) ) {
return find_value( $val, $searched_val );
}
}
return 0;
}
get_teams();
function get_teams() {
$people = get_data( 'some/file.json' );
$teams = [];
foreach ( $people as $person ) {
if ( ! find_value( $teams, $person['team'] ) ) {
$teams[] = [ 'text' => $person['team'] ];
}
}
return $teams;
}
This is sample input
Array
(
[0] => Array
(
[id] => 1
[name] => Friedrich Robel
[team] => WordPress
[position] => Frontend Developer
[salary] => 4400
)
[1] => Array
(
[id] => 2
[name] => Mr. Christop Veum
[team] => HTML
[position] => Manager
[salary] => 1200
)
[2] => Array
(
[id] => 3
[name] => Demarco Rippin
[team] => HTML
[position] => QA
[salary] => 4400
)
[3] => Array
(
[id] => 4
[name] => Felicia Farrell
[team] => HTML
[position] => QA
[salary] => 1200
)
[4] => Array
(
[id] => 5
[name] => Torrance Fritsch
[team] => HTML
[position] => Assistant Manager
[salary] => 2500
)
[5] => Array
(
[id] => 6
[name] => Erica Daugherty
[team] => Mail
[position] => Assistant Manager
[salary] => 500
)
)
And I want this output
Array
(
[0] => WordPress,
[1] => HTML,
[2] => Mail
)
Your problem is that you returns the result of the sub-search without testing it.
Instead of return find_value(...), you have to test its value.
function find_value( $array, $searched_val ) {
foreach ( $array as $key => $val ) {
if ( $val === $searched_val ) {
return true;
}
if ( is_array( $val ) ) {
// Here, you have to test the result before to return TRUE.
if (find_value( $val, $searched_val )) {
return true ;
}
}
}
return 0;
}
The fix is, as Syscall posted, to test the return of find_value(), and only return if it is true.
As your code is structured now, it will return either true or false from the recursion call, which will always be required, since you are storing your values as [ 'text' => $person['team'] ];
The way your code is designed, with the return 0 outside the foreach is only to return false in the case a match was not found. For this reason, returning a false from the contents of that conditional is premature. Your loop does not have a chance to iterate and test all the values of the passed array. This early returning results in duplicate array entries.
In any case, the results at the end will be an array of ['text' => '<job>'] array members. If you want only the job string, assign like this instead:
$teams[] = $person['team'];
If you make this change, the other is not required. Note that it also removes the need for recursion, as $teams will contain only an array of strings.

Intersection of two arrays using common key value for comparison

I want to perform an intersection of two arrays that have different structures, but both have one key common (fid). I want a new (filtered second) array after intersection with first array. below is my code and two arrays :
first array:
Array
(
[0] => Array
(
[fid] => 1
)
[1] => Array
(
[fid] => 3
)
)
Second array:
Array
(
[0] => Array
(
[fid] => 9
[functionality] => testing
[funcat_id] => 1
[name] => functionality
)
[1] => Array
(
[fid] => 1
[functionality] => add functionality
[funcat_id] => 1
[name] => functionality
)
[2] => Array
(
[fid] => 2
[functionality] => view functionality category
[funcat_id] => 1
[name] => functionality
)
[3] => Array
(
[fid] => 3
[functionality] => view functionality
[funcat_id] => 1
[name] => functionality
)
[4] => Array
(
[fid] => 4
[functionality] => edit functionality
[funcat_id] => 1
[name] => functionality
)
)
I want this Output :
Array
(
[0] => Array
(
[fid] => 1
[functionality] => add functionality
[funcat_id] => 1
[name] => functionality
)
[1] => Array
(
[fid] => 3
[functionality] => view functionality
[funcat_id] => 1
[name] => functionality
)
)
I tried this code but I'm not getting the right answer:
$result=array_intersect($array1,$array2);
//Or this also
$result=recursive_array_intersect_key($array1,$array2);
Please let me know, if any one can do this ?
I do not know if a function does exists to do this outright, but alternatively, you can just loop them instead:
$result = array();
foreach($array2 as $val2) {
foreach ($array1 as $val1) {
if($val2['fid'] == $val1['fid']) {
$result[] = $val2;
}
}
}
echo '<pre>';
print_r($result);
Sample Output
Or if you're using PHP 5.5 or greater:
$val1 = array_column($array1, 'fid');
$result = array_filter($array2, function($val2) use($val1) {
return in_array($val2['fid'], $val1);
});
foreach($array2 as $val)
{
$i=0;
foreach($array1 as $val1)
{
if($val['fid']==$val1['fid'])
{
$i++;
}
}
if($i!=0)
{
$a[]=$val;
}
}
print_r($a);

Search value in multidimension array and keep keys association

I have this array converted from xml of webservice. I have 500 items in this array.
I want to search any value and return all found array items with key association ( similar to database select query ). So if I search 'dummy' then it should return first item of this array.
Array
(
[12.12.2014] => Array
(
[7] => Array
(
[id] => 1672
[date] => 12.12.2014
[description] => rummy dummy data
[room] => delux
[Type] => garden
[from] => 17:00
[to] => 17:45
[assets] => Array
(
[asset] => Array
(
[0] => Array
(
[number] => 5275
[detail] => primary one
)
[1] => Array
(
[number] => 19
[detail] => secondary one
)
)
)
[references] => Array
(
[reference] => Array
(
[personnumber] => 479470
[type] => worker
[name] => David
[department] => Sales
[cv] => Array
(
[pdetails] => follow later
)
[profile] => True
)
)
)
)
[13.12.2014] => Array
(
[17] => Array
(
[id] => 1672
[date] => 13.12.2014
[description] => brown fox jump
[room] => star
[Type] => city
[from] => 17:00
[to] => 17:45
[assets] => Array
(
[asset] => Array
(
[number] => 5275
[detail] => prime two
)
)
[references] => Array
(
[reference] => Array
(
[personnumber] => 479470
[type] => manager
[name] => Albert
[department] => Purchase
[cv] => Array
(
[pdetails] => follow later
)
[profile] => True
)
)
)
)
)
I tried stripos to search string in array value and in_array based functions but either it gives incorrect result or key association is not maintained.
I am unable to find a way to maintain key->value.
function search($array, $key, $value)
{
$results = array();
if (is_array($array))
{
if (isset($array[$key]) && $array[$key] == $value)
$results[] = $array;
foreach ($array as $subarray)
$results = array_merge($results, search($subarray, $key, $value));
}
return $results;
}
This may be worst function you have ever seen but this do the job. If some one can make it recursive ( array may be further deeper ).
function search_in_multi_array($srchvalue, $array)
{
$foundkey = '';
if (is_array($array) && count($array) > 0)
{
foreach($array as $pkey => $pvalue)
{
foreach($pvalue as $ckey => $cvalue)
{
if (is_array($cvalue) && count($cvalue) > 0)
{
if(in_array($srchvalue,$cvalue))
{
$foundkey[$pkey][$ckey] = $cvalue;
}
foreach($cvalue as $dkey => $dvalue)
{
if(!is_array($dvalue))
{
$pos = stripos($dvalue, $srchvalue);
if ($pos !== false)
{
$foundkey[$pkey][$ckey] = $cvalue;
}
}
}
}
}
}
}
return $foundkey;
}
Function call -
$needle = 'fox';
search_in_multi_array($needle, $my_array);
This is the output
Array
(
[13.12.2014] => Array
(
[17] => Array
(
[id] => 1672
[date] => 13.12.2014
[description] => brown fox jump
[room] => star
[Type1] => city
[from] => 17:00
[to] => 17:45
[assets] => Array
(
[asset] => Array
(
[number] => 5275
[detail] => prime two
)
)
[references] => Array
(
[reference] => Array
(
[personnumber] => 479470
[Type1] => manager
[name] => Albert
[department] => Purchase
[cv] => Array
(
[pdetails] => follow later
)
[profile] => 1
)
)
)
)
)
You can use array_search() function to look through the array values. But array_search only looks in single-dimensional array.
Since you have a multi-dimensional array, you can write custom recursive function to search recursively in the array
function recursive_array_search($needle,$haystack) {
foreach($haystack as $key=>$value) {
$current_key=$key;
if($needle===$value OR (is_array($value) && recursive_array_search($needle,$value) !== false)) {
return $current_key;
}
}
return false;
}
But please note, using array_search() is a easier approach but not a optimised function. Which means if you're more concerned about your memory utilisation then I would also addtionally suggest.
Create a new array say 'dictionary' and store them like
$dictonary[<key-searched>] = array('12.12.2014', '13.12.2014')
So you process them once, and cache them
So next time when you want search again for the same key, you can first check if the key exists in dictionary. If exists return from there, else use array_search and cache the results in dictionary
Its always easier to search by key-value than to search in a multi-dimensional array.
I've made a simple routine that will extract the values in the array you're looking for:
function search_keys($needle,$haystack)
{
foreach($haystack as $key => $value)
{
if (is_array($value)) $output[$key] = search_keys($needle,$value);
else if (strpos($value,$needle) !== FALSE) $output[$key] = $value;
}
return isset($output) ? $output : NULL; // prevent warning
}
echo '<pre>';
print_r(search_keys('garden',$data));
echo '</pre>';
This will return 'garden' as:
Array
(
[12.12.2014] => Array
(
[7] => Array
(
[Type] => garden
)
)
)
You can further process the output of this function, or change the function, as needed.
You may want to have a look at XPath to run queries against the "raw" XML, see DOMXPath for example.
Something like /myRootTag/someDayTag[.//text()[contains(.,'MY_SEARCH_VALUE')]] should do the trick, selecting and returning all the someDayTag XML elements below the myRootTag which have the text MY_SEARCH_VALUE in any child node.

Delete array child where value = somevalue in multidimentional array

I have a multidimentional array like this $membergroups :
Array ( [0] =>
Array ( [id] => 1645819602
[name] => Oryza NurFa
[first_name] => Oryza
[last_name] => NurFa
[work] => MAN 2 Yogyakarta )
[1] =>
Array ( [id] => 100000251643877
[name] => Lathif Pambudi
[first_name] => Muhammad Lathif
[last_name] => Pambudi
[work] => Omah TI )
[2] =>
Array ( [id] => 1152078197
[name] => Novantio Bangun
[first_name] => Novantio
[last_name] => Bangun
[work] => Pertamina))
How to delete one of child array with specific value. For the example, I want to delete an array with [id] => 100000251643877 inside? So the output will be :
Array ( [0] =>
Array ( [id] => 1645819602
[name] => Oryza NurFa
[first_name] => Oryza
[last_name] => NurFa
[work] => MAN 2 Yogyakarta )
[1] =>
Array ( [id] => 1152078197
[name] => Novantio Bangun
[first_name] => Novantio
[last_name] => Bangun
[work] => Pertamina))
Here is my php code, but it doesn't work :
if (($key = array_search($user_fbid, $membergroups)) !== false) {
unset($membergroups[$key]);
}
Any help would be greatly appreciated. Thank you
You can make use of array_column but only for php >= 5.5
if (($key = array_search($user_fbid, array_column( $membergroups, 'id') ) !== false) {
unset($membergroups[$key]);
}
array_column( $membergroups, 'id') search in membergroup multidimensional array for id column, and return tou you an array containing all rows values entries with id key.
array_column -> MANUAL
Using a foreach you can do the job like this
$id = 100000251643877;//Example
foreach($membergroups as $key => $value){
if($value['id'] == $id){
unset($membergroups[$key]);
}
}
Loop through the whole array:
foreach ($membergroups as $idx => $group) {
if ($group['id'] === $user_fbid) {
unset($membergrouops[$idx]);
break;
}
}
foreach($membergroups as $key => $value){
if($value['id'] == $user_fbid){
unset($membergroups[$key]);
}
}
Don't forget to merge the array after removing key to keep the index sequantially
$membergroups = array_merge($membergroups);

PHP Multidimensional sort?

I have a rather annoying array structure to work with and I need to sort it by any arbitrary key combination. 2 records are displayed below but multiple records with or without the same structure will be present when sorting is actioned.
Here are two records.
Array(
[0] => Array
(
[cid] => 1
[title] => Mr
[first_name] => Abet
[last_name] => Simbad
[emails] => Array
(
[374] => Array
(
[eid] => 374
[name] => ski lodge
[email] => simbad#skifree.com
)
[373] => Array
(
[eid] => 373
[name] => work
[email] => simbad#work.com
)
[375] => Array
(
[eid] => 375
[name] => personal
[email] => simbad#gmail.com
)
)
)
[1] => Array
(
[cid] => 2
[title] => Mrs
[first_name] => Angie
[last_name] => Stokes
[emails] => Array
(
[590] => Array
(
[eid] => 590
[name] => work
[email] => angie#gmail.com
)
)
)
So if I wanted to sort by email in ascending order in the emails array, how can I get the second complete record to come first in the result array? angie#gmail.com comes before simbad#....
Also Some records will not contain an emails array. They would be last in the result set.
Any help would be much appreciated.
The array shown is a cut down version but I have addresses, notes, phones and websites in the same annoying structure. Ideally I could sort with something like
$sort = array('emails','email')
$data = sort_data_func('ASC',$sort,$data);
But anything steps in the right direction will help. :)
Here's some code I have so far
$sort = array('emails','email');
foreach($contacts as $ckey => $c){
if(is_array($c[$sort[0]])){
foreach($c[$sort[0]] as $key1 => $sort0){
if($sort0[$sort[1]]!=''){
$res[$sort[0]][$ckey][$sort[1]][] = $sort0[$sort[1]];
}
}
}
}
print_r($res);
Which produces:
Array
(
[emails] => Array
(
[0] => Array
(
[0] => simbad#skifree.com
[1] => simbad#work.com
[2] => simbad#gmail.com
)
[1] => Array
(
[0] => angie#gmail.com
)
)
)
But I have no idea where to go from here.
EDIT
OK I have the records in the currect order now but how can I keep the initial record ID in the resulting array?
Here's what I'm using.
$direction=='ASC'
function cmp_asc($a, $b){
$key = current(array_keys($a));
sort($a[$key]);
$a[$key] = current($a[$key]);
sort($b[$key]);
$b[$key] = current($b[$key]);
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
function cmp_desc($a, $b){
$key = current(array_keys($a));
asort($a[$key]);
$a[$key] = current($a[$key]);
asort($b[$key]);
$b[$key] = current($b[$key]);
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
if($direction=='ASC'){
usort($res[$sort[0]], 'cmp_asc');
}else{
usort($res[$sort[0]], 'cmp_desc');
}
In
Array
(
[emails] => Array
(
[0] => Array
(
[email] => Array
(
[0] => simbad#skifree.com
[1] => simbad#work.com
[2] => simbad#gmail.com
)
)
[1] => Array
(
[email] => Array
(
[0] => angie#gmail.com
)
)
)
)
Out
Array
(
[emails] => Array
(
[0] => Array
(
[email] => Array
(
[0] => angie#gmnail.com
)
)
[1] => Array
(
[email] => Array
(
[0] => simbad#skifree.com
[1] => simbad#work.com
[2] => simbad#gmail.com
)
)
)
)
One of the usort functions, combined with a self-written comparison function that detects the order in which two elements should be sorted, should do the trick.

Categories