I have an array [0,1,2,3,4] if i got ApplicationStatus = 868 at first then it return only that value other are remove from array. My actual array and i want expected array as below.
Actual Array -
Array
(
[0] => Array
(
[Name] => DENNIS VICENCIO BLANCO
[ApplicationStatus] => 826
)
[1] => Array
(
[Name] => ARPITA RANJAN DUTTA
[ApplicationStatus] => 826
)
[2] => Array
(
[Name] => MARLUNA LIM URUBIO
[ApplicationStatus] => 868
)
[3] => Array
(
[Name] => BREDJET - ALEXANDER
[ApplicationStatus] => 868
)
[4] => Array
(
[Name] => DENNIS VICENCIO BLANCO
[ApplicationStatus] => 826
)
)
Expected Array -
Array
(
[0] => Array
(
[Name] => DENNIS VICENCIO BLANCO
[ApplicationStatus] => 826
)
[1] => Array
(
[Name] => ARPITA RANJAN DUTTA
[ApplicationStatus] => 826
)
[2] => Array
(
[Name] => MARLUNA LIM URUBIO
[ApplicationStatus] => 868
)
)
So,how to remove remaining key from array.please suggest mi appropriate solution for this.
You can use array slice function:
$desired_array = array();
for($i = 0; $i < count($my_array); $i++)
{
if($my_array[$i]["ApplicationStatus"] == 868)
{
$desired_array = array_slice($my_array, 0, $i);
}
}
Not very clear, but you can delete a value from an array with unset() function.
For example:
unset($arr[3]); // removes array with key = 3
...but I would use the array_filter() and would create a function to select the right elements what I need.
Related
I want to remove parent array index in array.
Following is my array.
Array
(
[0] => Array
(
[0] => Array
(
[id] => 296
[username] => David0123
[profile_slug] => david-love
)
)
[1] => Array
(
[0] => Array
(
[id] => 297
[username] => Anne_wils
[profile_slug] => anne-chase
)
)
[2] => Array
(
[0] => Array
(
[id] => 300
[username] => malina001
[profile_slug] => malina-reid
)
)
)
And I want like this way..
Array(
[0] => Array
(
[id] => 296
[username] => David0123
[profile_slug] => david-love
)
[1] => Array
(
[id] => 297
[username] => Anne_wils
[profile_slug] => anne-chase
)
[2] => Array
(
[id] => 300
[username] => malina001
[profile_slug] => malina-reid
)
)
I used following script for it but not work.
$myMainArray = json_decode(json_encode($allEscorts),true);
$i=0;
foreach( array_values($myMainArray) as $k=> $val){
echo $val[$i]['id'];
$i++;
}
I want to display data each element but first i have to remove parent array indexes.
You can use array_map to pull values up one level
$myMainArray = json_decode(json_encode($allEscorts),true);
$myMainArray = array_map(function($el) {
return $el[0];
}, $myMainArray);
You should check if the first array could be generate as you wish.
If not you can use array_map to get the first index from the inner-array.
for example:
$result = array_map(function($item){
return $item[0]; // always return the first array-index
}, $first_array);
I've spent the day playing with deceze's answer but I'm no closer to making it work. I may have part of it, but not sure how to get recursion in array_filter.
My Array looks like this (sample):
Array
(
[name] => root
[ChildCats] => Array
(
[0] => Array
(
[name] => Air Conditioning
[ChildCats] => Array
(
[0] => Array
(
[name] => Ducted Air Conditioning
[ChildCats] => Array
(
[0] => Array
(
[name] => Supply & Install
[ChildCats] => Array
(
[0] => Array
(
[name] => Daiken
[S] => 6067
)
)
)
[1] => Array
(
[name] => Supply Only
[ChildCats] => Array
(
[0] => Array
(
[name] => Mitsubishi
[S] => 6026
)
)
)
)
)
[1] => Array
(
[name] => Split System Air Conditioning
[ChildCats] => Array
(
[0] => Array
(
[name] => Supply & Install
[ChildCats] => Array
(
[0] => Array
(
[name] => Daiken
[S] => 6067
)
[1] => Array
(
[name] => Fujitsu Split Air Conditioning Systems
[S] => 6464
)
[2] => Array
(
[name] => Mitsubishi Electric Split Air Conditioning Systems
[S] => 6464
)
)
)
)
)
)
)
[1] => Array
(
[name] => Appliance / White Goods
[ChildCats] => Array
(
[0] => Array
(
[name] => Clearance
[S] => 6239
)
[1] => Array
(
[name] => Cooktops
[ChildCats] => Array
(
[0] => Array
(
[name] => Ceramic Cooktops
[S] => 6239
)
[1] => Array
(
[name] => Element Cooktops
[S] => 6067
)
[2] => Array
(
[name] => Gas Cooktops
[S] => 6239
)
[3] => Array
(
[name] => Induction Cooktops
[S] => 6239
)
)
)
)
)
Now lets say I try to extract just the parts of the array relevent to the following keypair:
S => 6067.
I'd like the result to look like:
Array
(
[name] => root
[ChildCats] => Array
(
[0] => Array
(
[name] => Air Conditioning
[ChildCats] => Array
(
[0] => Array
(
[name] => Ducted Air Conditioning
[ChildCats] => Array
(
[0] => Array
(
[name] => Supply & Install
[ChildCats] => Array
(
[0] => Array
(
[name] => Daiken
[S] => 6067
)
)
)
)
)
[1] => Array
(
[name] => Split System Air Conditioning
[ChildCats] => Array
(
[0] => Array
(
[name] => Supply & Install
[ChildCats] => Array
(
[0] => Array
(
[name] => Daiken
[S] => 6067
)
)
)
)
)
)
)
[1] => Array
(
[name] => Appliance / White Goods
[ChildCats] => Array
(
[0] => Array
(
[name] => Cooktops
[ChildCats] => Array
(
[0] => Array
(
[name] => Element Cooktops
[S] => 6067
)
)
)
)
)
)
)
What I cannot get my head arround is should I be creating a new array or using array filter.
Playing with deceze code I've got the search working using the following:
function recursive_assoc_in_array(array $haystack, array $needle, $childKey = 'ChildCats') {
if (array_intersect_assoc($haystack, $needle)) {
echo "Found via array_intersect_assoc ".$haystack[name]."\n";
return true;
}
foreach ($haystack[$childKey] as $child) {
if (recursive_assoc_in_array($child, $needle, $childKey)) return true;
}
return false;
}
But if I try to process with,
$array = array_filter($array, function (array $values) {
return recursive_assoc_in_array($values, array('S' => '6067'));
});
I get the original array which leads me to think I have to get recursion running on the array_filter query.
At this point I just go blank.
Additionally, the array keys will need to be reindexed on the produced new array. Any ideas?
--Additional 7/7/14
How about if I try to build a new array from the old one?
I'm trying:
function exploreArrayandAdd($Array) {
if ($Array['ChildCats']) {
foreach ($Array['ChildCats'] as $key => $value) {
$NewArray['ChildCats'][] = exploreArrayandAdd($value);
}
} else {
if ($Array['S'] == 6026) {
//echo "1";
return $Array;
}
}
}
But cannot work out how to pass the new array out of the function?
Tried removing branches that don't match using:
function exploreArray(&$Array) {
if ($Array['ChildCats']) {
foreach ($Array['ChildCats'] as $key => $value) {
$result = exploreArray($Array['ChildCats'][$key]);
if ($result === false)
unset($Array['ChildCats'][$key]);
}
} else {
// print_r($Array);
if ($Array['S'] == 6026) {
return true;
} else {
unset($Array);
return false;
}
}
//if ($NoChildCat==true) print_r($Array);
}
But I believe it is the wrong way as it does work at the bottom of the array but not back up towards the top as siblings make result true.
Also this won't reindex the array keys.
function recursive_array_filter(array $array, $childKey, callable $test) {
if (isset($array[$childKey]) && is_array($array[$childKey])) {
foreach ($array[$childKey] as $key => &$child) {
if (!$child = recursive_array_filter($child, $childKey, $test)) {
unset($array[$childKey][$key]);
}
}
if (!$array[$childKey]) {
unset($array[$childKey]);
}
}
return !empty($array[$childKey]) || $test($array) ? $array : [];
}
$array = recursive_array_filter($array, 'ChildCats', function (array $array) {
return array_intersect_assoc($array, ['S' => 6026]);
});
To express the algorithm in words: you descend down into the array first, following all ChildCats branches to their end. In each level you return the values as they are back to the caller if they match your test or if they have children, or you return an emptied array (you could also return false if you prefer). If some child turns out empty, you prune it with unset.
I have implemented the test as a callback function here for best reusability of the code.
I have an associative array that looks like this:
Array (
[0] => Array (
[amount] => 3
[name] => Chuck
)
[1] => Array (
[amount] => 2
[name] => Steve
)
[2] => Array (
[amount] => 5
[name] =>
)
[3] => Array (
[amount] => 4
[name] => Chuck
)
[4] => Array (
[amount] =>
[name] => Chuck
)
)
I need to remove values that are missing a name or amount e.g. [2] and [4] and then sum the totals for each name so that the final array is:
Array (
[0] => Array (
[amount] => 7
[name] => Chuck
)
[1] => Array (
[amount] => 2
[name] => Steve
)
)
For anyone looking for this nowadays, this would be much cleaner:
$sum = array_sum(array_column($data, 'amount'));
Try this:
$starting_array = array( ... ); // Initial array with your setup
$final_array = array();
$sum = 0;
foreach ($starting_array as $idx => $data) {
if (!empty($data['amount']) && !empty($data['name'])) {
$final_array[$idx] = $data;
$sum += $data['amount'];
}
}
// After looping through all of the items, $final_array should contain all
// of the specific items that have an amount and name set. In addition, the
// total of all of the amounts will be in $sum.
Take a look at php's empty(). Note: If 0 is an allowed value, you may want to use is_null() instead.
I'm sure this is easy for someone well-versed in php, but I've made the mistake of overloading my brain, so now I'm really confused as to whether I should use array_combine, array_merge, or something else... I've been googling and reading php.net for 4 hours and I think I'm just confusing myself even more...
Essentially, I just want to combine an array while keeping the keys?
//Here are the original arrays
[field_sreference] => Array
(
[0] => Array
(
[nid] => 28
)
[1] => Array
(
[nid] => 28
)
[2] => Array
(
[nid] => 29
)
)
[field_idelta] => Array
(
[0] => Array
(
[value] => 0
)
[1] => Array
(
[value] => 1
)
[2] => Array
(
[value] => 0
)
)
[field_iswitch] => Array
(
[0] => Array
(
[value] => 0
)
[1] => Array
(
[value] => 0
)
[2] => Array
(
[value] => 0
)
)
//Here is what I'm trying to achieve:
[combinedarray] => Array
(
[0] => Array
(
[nid] => 28
[idelta] => 0
[iswitch] => 0
)
[1] => Array
(
[nid] => 28
[idelta] => 1
[iswitch] => 0
)
[2] => Array
(
[nid] => 29
[idelta] => 0
[iswitch] => 0
)
)
You can solve this is O(n) by simply iterating the arrays...
$combinedarray = array();
$len = count($field_sreference);
for ($i = 0; $i < $len; $i++) {
$combinedarray[] = array("nid" => $field_sreference[$i]['nid'],
"idelta" => $filed_idelta[$i]['value'],
"iswitch" => $field_iswitch[$i]['value']);
}
This assumes, the 3 arrays are all of equal length.
A bit quickly typed, but this should work:
$result = array();
foreach ($arrays as $array)
{
foreach ($array as $index => $data)
{
$result[$index] += $data;
}
}
As you have not provided some input array in some easy form, you need to test it on your own. Let's say it's pseudo-code and I leave it here as an exercise. The + operator is the array union operator.
I would like to remove duplicates for the following array. I want to group by the first value and then rebuild the index.
ie
Array
(
[0] => Array
(
[title] => California
[state_id] => 1
)
[1] => Array
(
[title] => California
[state_id] => 1
)
[2] => Array
(
[title] => New Mexico
[state_id] => 2
)
[3] => Array
(
[title] => Washington
[state_id] => 3
)
[4] => Array
(
[title] => Montana
[state_id] => 4
)
[5] => Array
(
[title] => Montana
[state_id] => 4
)
)
To
Array
(
[0] => Array
(
[title] => California
[state_id] => 1
)
[2] => Array
(
[title] => New Mexico
[state_id] => 2
)
[3] => Array
(
[title] => Washington
[state_id] => 3
)
[4] => Array
(
[title] => Montana
[state_id] => 4
)
)
and rebuild key
Array
(
[0] => Array
(
[title] => California
[state_id] => 1
)
[1] => Array
(
[title] => New Mexico
[state_id] => 2
)
[2] => Array
(
[title] => Washington
[state_id] => 3
)
[3] => Array
(
[title] => Montana
[state_id] => 4
)
)
$array = array_values(array_combine(array_map(function ($i) { return $i['title']; }, $array), $array));
I.e.
extract the title key (array_map)
use the extracted titles as keys for a new array and combine it with the old one (array_combine), which de-duplicates the array (keys have to be unique)
run the result through array_values, which discards the keys
Broken down:
$keys = array_map(function ($i) { return $i['title']; }, $array);
$deduped = array_combine($keys, $array);
$result = array_values($deduped);
For PHP 5.2- you'll need to write the anonymous callback function like this:
array_map(create_function('$i', 'return $i["title"];'), $array)
Sort your array by title (usort does the trick)
create a new empty array
Cycle original array and store the value you find in the new empty array
If next array value equals last then skip it
At the end of this you have the array you want.
There is another way:
Sort your array by title
Cycle original array
If this array value equals last then unset it
Sort the array again with PHP sort function to rebuild keys
$array = array_unique($array);
Yes, it works with multi-dimentional arrays too; it just checks to equality.