array search multidimensional in php? - php

I want to create a function that for a random array will search for a given key and will return all values for that key in an array. Here is what I have written so far...
$testArray = array (
'subArray1' => array(
'key1' => "Sub array 1 value 1",
'key2' => "Sub array 1 value 2",
'key3' => array(
'subkey1' => array(
'subsubkey' => 'sub sub sub value',
),
),
),
'subArray2' => array(
'key1' => "Sub array 2 value 1",
'key2' => "Sub array 2 value 2",
'key3' => array(
'subkey1' => array(
'subsubkey' => 'sub sub sub value 2',
),
),
),
);
function recursiveSearch($testArray,$key,$results)
{
// var_dump($testArray);
if(is_array($testArray)){
foreach($testArray as $k => $v){
if($k == $key){
return $v;
}
else if(is_array($v)){
array_push($results, recursiveSearch($v,$key,$results));
}
}
}
else{
return ;
}
return $results;
}
$empt = array();
$output = recursiveSearch($testArray,'subsubkey',$empt);
var_dump($output);
This the output I get at the moment... I want to get a simple array with the values in it.
array(2) {
[0]=>
array(1) {
[0]=>
array(1) {
[0]=>
string(17) "sub sub sub value"
}
}
[1]=>
array(2) {
[0]=>
array(1) {
[0]=>
array(1) {
[0]=>
string(17) "sub sub sub value"
}
}
[1]=>
array(2) {
[0]=>
array(1) {
[0]=>
array(1) {
[0]=>
string(17) "sub sub sub value"
}
}
[1]=>
string(19) "sub sub sub value 2"
}
}
}
I am not really sure why the result array is like this ...
The format for the result array that I want is :
['sub sub sub value','sub sub sub value 2']

An improved version of xPheRe
/**
* Recursive find in the given $array and $needle.
* #param $array The input array.
* #param $needle The needle to find.
* #return Returns all the values find by the given key.
*/
function recursiveFind(array $array, $needle)
{
$iterator = new RecursiveArrayIterator($array);
$recursive = new RecursiveIteratorIterator($iterator,
RecursiveIteratorIterator::SELF_FIRST);
$results = array();
foreach ($recursive as $key => $value) {
if ($key === $needle) {
array_push($results, $value);
}
}
return $results;
}
Usage
$output = recursiveFind($testArray, 'subsubkey');
var_dump($output);
Output
array(2) {
[0]=>
string(17) "sub sub sub value"
[1]=>
string(19) "sub sub sub value 2"
}

The updated function of my version is as follows:
function recursiveSearch($testArray,$key){
$empt = array();
function recurSearch($testArray,$key,$results)
{
if(is_array($testArray)){
foreach($testArray as $k => $v){
if($k === $key){
array_push($results,$v);
}
else if(is_array($v)){
$temp = array();
$tempres = recurSearch($v,$key,$temp);
$results = array_merge($results,$tempres);
}
}
}
return $results;
}
$res = recurSearch($testArray,$key,$empt);
return $res;
}
seems to work : )

Related

How to merge subarrays using keys and sum the values?

I'm fairly new to PHP and I'm having some trouble with arrays and combining data. I have the following array which has been created from a foreach loop:
array(1) {
[36868]=> int(3)
}
array(1) {
[2112]=> int(3)
}
array(1) {
[35901]=> int(3)
}
array(1) {
[6496]=> int(3)
}
array(1) {
[87]=> int(3)
}
array(1) {
[36868]=> int(3)
}
array(1) {
[68]=> int(3)
}
array(1) {
[9068]=> int(3)
}
array(1) {
[47]=> int(3)
}
The key in each array is a user ID, so I need to preserve this, but I only want one instance of each key and where there are duplicate keys, sum the values. Like so:
array(1) {
[36868]=> int(6)
}
array(1) {
[2112]=> int(3)
}
array(1) {
[35901]=> int(3)
}
array(1) {
[6496]=> int(3)
}
array(1) {
[87]=> int(3)
}
array(1) {
[68]=> int(3)
}
array(1) {
[9068]=> int(3)
}
array(1) {
[47]=> int(3)
}
The I've tried looping through the array:
foreach ($array as $key => &$value) {
if ($value[0] == $value[1]) {
$value[1] += $value[1];
}
}
But with no luck. I've also tried rendering the arrays differently i.e. [userid]=>1,[score]=>3 and I feel like I'm going round in circles a bit, so any help would be hugely appreciated.
$data <-- this is your original array
$result = array_reduce(
$data,
function($carry, $item) {
foreach ($item as $id => $score) {
if (array_key_exists($id, $carry)) {
$carry[$id] += $score;
} else {
$carry[$id] = $score;
}
}
return $carry;
},
[]
);
If you are sure that each item only contains 1 entry you could also simplify the callback to not use foreach:
$result = array_reduce(
$data,
function ($carry, $item) {
$score = reset($item);
$id = key($item);
if (array_key_exists($id, $carry)) {
$carry[$id] += $score;
} else {
$carry[$id] = $score;
}
return $carry;
},
[]
);
You could also keep using foreach instead:
/** foreach to create a $data array like described below and afterwards do this: **/
$result = [];
foreach($data as $row) {
$score = reset($row);
$id = key($row);
if (array_key_exists($id, $result)) {
$result[$id] += $score;
} else {
$result[$id] = $score;
}
}
This will take an array $data like this:
array(
array('1' => 3),
array('1' => 3),
array('2' => 3),
);
and creates the variable $result like this:
array(
'1' => 6,
'2' => 3,
);
Here is a clean method that will not produce Notices. When merge-summing array data the efficient method is to generate temporary keys and use the very fast isset() function. I could have used current() and key() to access the lone subarray element, but the second foreach control structure is actually faster and more compact. (Ref:
https://stackoverflow.com/a/21219594/2943403 )
Code: (Demo)
$array = [
[36868 => 3],
[2112 => 3],
[35901 => 3],
[6496 => 3],
[87 => 3],
[36868 => 3],
[68 => 3],
[9068 => 3],
[47 => 3]
];
$result = [];
foreach ($array as $subarray) {
foreach ($subarray as $k => $v) {
if (!isset($result[$k])) {
$result[$k] = $subarray;
} else {
$result[$k][$k] += $v;
}
}
}
var_export(array_values($result));
Output:
array (
0 =>
array (
36868 => 6,
),
1 =>
array (
2112 => 3,
),
2 =>
array (
35901 => 3,
),
3 =>
array (
6496 => 3,
),
4 =>
array (
87 => 3,
),
5 =>
array (
68 => 3,
),
6 =>
array (
9068 => 3,
),
7 =>
array (
47 => 3,
),
)

How can I flatten a Multidimensional array into a string?

Ive researched this but im coming up blank, Im generating an array of tests from a database like so:
$descriptions = array();
foreach ($tests as $value) {
array_push($descriptions, ['name' => $value['name']]);
}
I'm getting the desired out put but i'm getting a Multidimensional array of an array with '[64]' arrays inside '$descriptions', I need to convert this array so I get the following output:
'name' => $value1, 'name' => $value2, etc etc for all results,
I've tried implode, array_merge etc but the closest I've got is a flat array with only my last test: [name] => Zika can anyone point me in the right direction? cheers
You can't have duplicate array keys. But you can pass an array in like so:
<?php
$descriptions = array();
$tests = array(
'Zika', 'SARS', 'AIDS', 'Mad Cow Disease', 'Bird Flu', 'Zombie Infection',
);
foreach ($tests as $value) {
$descriptions[] = array('name' => $value);
}
var_dump($descriptions);
Which gives you :
array(6) { [0]=> array(1) { ["name"]=> string(4) "Zika" } [1]=> array(1) { ["name"]=> string(4) "SARS" } [2]=> array(1) { ["name"]=> string(4) "AIDS" } [3]=> array(1) { ["name"]=> string(15) "Mad Cow Disease" } [4]=> array(1) { ["name"]=> string(8) "Bird Flu" } [5]=> array(1) { ["name"]=> string(16) "Zombie Infection" } }
So you could foreach ($descriptions as $desc) and echo $desc['name']';
Have a look here: https://3v4l.org/pWSC6
If you just want a string, try this:
<?php
$descriptions = '';
$tests = array(
'Zika', 'SARS', 'AIDS', 'Mad Cow Disease', 'Bird Flu', 'Zombie Infection',
);
foreach ($tests as $value) {
$descriptions .= 'name => '.$value.', ';
}
$descriptions = substr($descriptions, 0, -2); // lose the last comma
echo $descriptions;
Which will output:
name => Zika, name => SARS, name => AIDS, name => Mad Cow Disease, name => Bird Flu, name => Zombie Infection
See it here https://3v4l.org/OFGF4

Combine multiple form array into 1 array

["trnx_date"]=>
array(2) {
[0]=>
string(10) "2017-01-10"
[1]=>
string(10) "2017-01-10"
}
["curr_from"]=>
array(2) {
[0]=>
string(3) "USD"
[1]=>
string(3) "PHP"
}
["curr_from_amt"]=>
array(2) {
[0]=>
string(8) "4,000.00"
[1]=>
string(8) "3,000.00"
}
["curr_to"]=>
array(2) {
[0]=>
string(3) "GBP"
[1]=>
string(3) "SAR"
}
["curr_to_amt"]=>
array(2) {
[0]=>
string(8) "3,000.00"
[1]=>
string(8) "2,000.00"
}
["amount"]=>
array(2) {
[0]=>
string(8) "7,000.00"
[1]=>
string(8) "5,000.00"
}
I have the above array which was being submitted. This input was in sets of multiple field which was generated by dynamic table row. How can I group this into 1 (one) array so that I could save in the database? Like this:
[cust_row] => array(
'tranx_date' => "2017-01-10",
'curr_from' => "USD",
'curr_from_amt' => "4,000.00",
'curr_to' => "GBP",
'curr_to_amt' => "3,000.00",
'amount' => "7,000.00"
),
[cust_row] => array(
'tranx_date' => "2017-01-10",
'curr_from' => "PHP",
'curr_from_amt' => "3,000.00",
'curr_to' => "SAR",
'curr_to_amt' => "2,000.00",
'amount' => "5,000.00"
),
All of the above we being populated like this:
$trnx_date = $this->input->post('trnx_date');
$curr_from = $this->input->post('curr_from');
$curr_from_amt = $this->input->post('curr_from_amt');
$curr_to = $this->input->post('curr_to');
$curr_to_amt = $this->input->post('curr_to_amt');
$amount = $this->input->post('amount');
Assuming all the sub-arrays have the same length, you can use a simple for loop that iterates over the index of one of them, and use that to access each of the sub-arrays at that index. Then combine all of them into the associative array for each customer.
$result = array();
$keys = array_keys($array);
$len = count($array[$keys[0]]); // Get the length of one of the sub-arrays
for ($i = 0; $i < $len; $i++) {
$new = array();
foreach ($keys as $k) {
$new[$k] = $array[$k][$i];
}
$result[] = $new;
}
$arr = array(
'trnx_date' => array('2017-01-10', '2017-01-10'),
'curr_from' => array('USD', 'PHP'),
'curr_from_amt' => array('4,000.00', '3,000.00'),
'curr_to' => array('GBP', 'SAR'),
'curr_to_amt' => array('3,000.00', '2,000.00'),
'amount' => array('7,000.00', '5,000.00')
);
$arr_out = array();
$arr_keys = array_keys($arr);
for($i = 0; $i < count($arr[$arr_keys[0]]); $i++) {
$new_arr = array();
foreach($arr_keys as $key => $value) {
$new_arr[$value] = $arr[$value][$i];
}
$arr_out[] = $new_arr;
}
var_dump($arr_out);
Hope it helps!
How about this?
// assume $arr is your original data array
$keys = array_keys($arr);
$result = array();
foreach($keys as $key) {
$vals = $arr[$key];
foreach($vals as $i =>$val) {
if (!is_array($result[$i]) {
$result[$i] = array();
}
$result[$i][$key] = $val;
}
}
var_dump($result);
EDIT: added array check for $result[$i]

Sort array by the value of the key

i'm trying to sort an array by the value of a sub-key in DESC order but I'm stuck.
I've could make it with ksort but it was in ascending order..
Here's my array :
array_by_lang => array(
[no] => array(
[3-1] => array(//some informations),
[3-10] => array(//informations),
[3-7] => array(//informations),
[5-1] => array(//informations)
)
)
what i want to obtain is something like :
array_by_lang => array(
[no] => array(
[5-1] => array(//informations),
[3-10] => array(//some informations),
[3-7] => array(//informations),
[3-1] => array(//informations)
)
)
Is that possible ? Thanks a lot
I think, you need "reversing natural sort by key". Just with array_multisort and array_reverse (see also natsort):
$array_by_lang = array(
'no' => array(
'3-1' => array('info_1'),
'3-10' => array('info_2'),
'3-7' => array('info_3'),
'5-1' => array('info_4'),
)
);
array_multisort(array_keys($array_by_lang['no']),SORT_NATURAL, $array_by_lang['no']);
$array_by_lang['no'] = array_reverse($array_by_lang['no']); // reverse natural order - "DESC"
var_dump($array_by_lang);
Output
array(1) {
["no"]=>
array(4) {
["5-1"]=>
array(1) {
[0]=>
string(6) "info_4"
}
["3-10"]=>
array(1) {
[0]=>
string(6) "info_2"
}
["3-7"]=>
array(1) {
[0]=>
string(6) "info_3"
}
["3-1"]=>
array(1) {
[0]=>
string(6) "info_1"
}
}
}
This might help -
$a = array(
'3-1' => array('//some informations'),
'3-10' => array('//informations'),
'3-7' => array('//informations'),
'5-1' => array('//informations')
);
## Array for keys
$temp= array();
foreach(array_keys($a) as $v) {
$t = explode('-', $v);
$temp[$t[0]][] = $t[1];
}
## Sort the keys
foreach($temp as &$ar) {
rsort($ar);
}
krsort($temp);
## Final array
$final= array();
foreach($temp as $k => $f) {
foreach($f as $v) {
$key = $k . '-' . $v;
$final[$key] = $a[$key];
}
}
var_dump($final);
Output
array(4) {
["5-1"]=>
array(1) {
[0]=>
string(14) "//informations"
}
["3-10"]=>
array(1) {
[0]=>
string(14) "//informations"
}
["3-7"]=>
array(1) {
[0]=>
string(14) "//informations"
}
["3-1"]=>
array(1) {
[0]=>
string(19) "//some informations"
}
}
DEMO

uasort is removing keys/arrays

I'm trying to sort a multidimensional array (in fact that it is multidimensional doesn't matter since I'm always only sorting one dimension).
function orderSort($a, $b) {
if ($a['order'] == $b['order']) return 0;
return($a['order'] < $b['order']) ? -1 : 1;
}
$nav = array(
"section" => array(
"header" => array(),
"main" => array()
),
"link" => array(
"header" => array(),
"main" => array()
)
);
$DATA = array(
array(
"type" => "section",
"subtype" => "main",
"data" => array("name" => "/Basic", "order" => 1, "parent" => "bbb")
),
array(
"type" => "link",
"subtype" => "main",
"data" => array("name" => "Home", "link" => array("/"), "order" => 1, "parent" => "/Basic")
)
);
foreach($DATA as $ele) {
if(!array_key_exists($ele['data']['parent'], $nav[$ele['type']][$ele['subtype']]))
$nav[$ele['type']][$ele['subtype']][$ele['data']['parent']] = array($ele['data']);
else
array_push($nav[$ele['type']][$ele['subtype']][$ele['data']['parent']], $ele['data']);
}
var_dump($nav['section']['main']);
echo '<br><br>';
foreach($nav['section']['main'] as $ele) {
uasort($ele, 'orderSort');
$nav['section']['main'] = $ele;
}
var_dump($nav['section']['main']);
The first var_dump returns:
array(1) {
["bbb"]=> array(1) {
[0]=> array(3) {
["name"]=> string(6) "/Basic"
["order"]=> int(1) ["parent"]=> string(3) "bbb"
}
}
}
As you can see there is a key named bbb (Sorry for that... highly conceptional). Now the second output:
array(1) {
[0]=> array(3) {
["name"]=> string(6) "/Basic"
["order"]=> int(1)
["parent"]=> string(3) "bbb"
}
}
Suddenly bbb is gone. In fact the whole array (with only a single element) has disappeared.
My question is, can I somehow prevent this from happening? I never asked for this kind of of optimization. Of course sorting this with only one element doesn't make any sense but still.
foreach($nav['section']['main'] as $ele) {
uasort($ele, 'orderSort');
$nav['section']['main'] = $ele;
}
Okay. And which is the key? you just forgot the keys (you are overwriting the parent of the original array with a new array instead of the original array itself):
foreach($nav['section']['main'] as $key => $ele) {
uasort($ele, 'orderSort');
$nav['section']['main'][$key] = $ele;
}
would be correct, but by reference is even better:
foreach($nav['section']['main'] as &$ele) {
uasort($ele, 'orderSort');
}
Your problem is here:
foreach($nav['section']['main'] as $ele) {
uasort($ele, 'orderSort');
$nav['section']['main'] = $ele;
}
This needs to be
foreach($nav['section']['main'] as $index => $ele) {
uasort($ele, 'orderSort');
$nav['section']['main'][$index] = $ele;
}
You were replacing the whole $nav['section']['main'] with the first element.

Categories