How to move Array up another level by key - php

I have an array like below and what I want to achieve is basically remove the keys like #attributes and AddressSop and make them all one level so it removes the parent keys and array but keeps the key value pairs:
[Addresses] => Array
(
[0] => Array
(
[AddrCountry] => US
[AddrLine1] => Test Street
[AddrLine2] => Test
[AddrLine3] => Test
[AddrName] => Mr John Doe
[AddrEmail] => test#test.com
[AddrMasterPriceBook] =>
[AddrMobile] => 123132142242
)
[1] => Array
(
...
)
)
This is how the original array looks. Any help will be awesome thank you.
[Addresses] => Array
(
[0] => Array
(
[#attributes] => Array
(
[AddrCountry] => US
[AddrLine1] => Test Street
[AddrLine2] => Test
[AddrLine3] => Test
[AddrName] => Mr John Doe
)
[AddressSOP] => Array
(
[#attributes] => Array
(
[AddrEmail] => test#test.com
[AddrMasterPriceBook] =>
[AddrMobile] => 123132142242
)
)
)
[1] => Array
(
[#attributes] => Array
(
[AddrCountry] => US
[AddrLine1] => Test Street
[AddrLine2] => Test
[AddrLine3] => Test
[AddrName] => Mr John Doe
)
[AddressSOP] => Array
(
[#attributes] => Array
(
[AddrEmail] => test#test.com
[AddrMasterPriceBook] =>
[AddrMobile] => 123132142242
)
)
)
)

Try this:
foreach ($addresses as $key => $address) {
$addresses[$key] = array_merge($address['#attributes'], $address['AddressSOP']);
}
If you want it to be recursive, you can do something like this:
$filtered = [];
foreach ($addresses as $addressKey => $address) {
array_walk_recursive($address, function($val, $key) use(&$filtered, $addressKey) {
$filtered[$addressKey][$key] = $val;
});
}
print_r($filtered);

Related

Find Key By Value In Array Inside Array Using Php

Hey Dear I am Making Referal System With 11 Level Of Referal So Ho Can I Show All Referals And There Level I Have Tried
Array
(
[L1] => Array
(
[0] => TL422632
[1] => TL626461
)
[L2] => Array
(
[0] => TL4321
[1] => TL191123
)
[L3] => Array
(
[0] => TL555938
)
[L4] => Array
(
[0] => TL197752
)
[L5] => Array
(
[0] => TL835309
)
[L6] => Array
(
[0] => TL495903
)
[L7] => Array
(
[0] => TL207447
)
[L8] => Array
(
[0] => TL427427
)
[L9] => Array
(
[0] => TL288884
)
[L10] => Array
(
[0] => TL251399
)
[L11] => Array
(
[0] => TL284394
)
)
But I Don't Know Ho To Get L Number By Value For Example I Have TL284394 And I Want To Know Level So It Will Show L11 How Can I Do It
you can use two foreach loop inside each other and array_search() like this:
$a = array("L1" => [ 0 => "TL422632" ],"L2"=> [ 0 => "TL422635" ]);
$x = "TL422635";
foreach($a as $key=>$value){
if($x === $value){
return $key;
}else{
foreach($value as $k=>$v){
if($x === $v){
return array_search([$k=>$v],$a);
}
}
}
}

Manipulation or modification of array

I'm new to PHP. I'm doing one my project with php and I'm new to array functions and all those things. I have tried but not get success in that. let me show you my sql query array.
I have one my array which is as below:
Array
(
[0] => Array
(
[pc_eventDate] => 2016-08-25
[ufname] => Rutul
[ulname] => Shah
[name] => Clinic
)
[1] => Array
(
[pc_eventDate] => 2016-08-26
[ufname] => Rutul
[ulname] => Shah
[name] => Clinic
)
[2] => Array
(
[pc_eventDate] => 2016-08-25
[ufname] => Administrator
[ulname] => Administrator
[name] => Clinic
)
[3] => Array
(
[pc_eventDate] => 2016-08-26
[ufname] => Administrator
[ulname] => Administrator
[name] => Clinic
)
[4] => Array
(
[pc_eventDate] => 2016-08-25
[ufname] => Administrator
[ulname] => Administrator
[name] => Clinic
)
[5] => Array
(
[pc_eventDate] => 2016-08-26
[ufname] => Amit
[ulname] => Mahida
[name] => Cancer Specialist
)
[6] => Array
(
[pc_eventDate] => 2016-08-26
[ufname] => Amit
[ulname] => Mahida
[name] => Breach Candy Hospital
)
)
Now I want my resulted array as below :
Array
(
[2016-08-25] => Array
(
[ Clinic] => Array
(
[Rutul Shah] => Array
(
[appointments] => 1
)
[Administrator Administrator] => Array
(
[appointments] => 2
)
)
)
[2016-08-26] => Array
(
[Clinic] => Array
(
[Rutul Shah] => Array
(
[appointments] => 1
)
[Administrator Administrator] => Array
(
[appointments] => 1
)
)
[Cancer Specialist] => Array
(
[Amit Mahida] => Array
(
[appointments] => 1
)
)
[Breach Candy Hospital] => Array
(
[Amit Mahida] => Array
(
[appointments] => 1
)
)
)
)
you want to loop through your appointments array and use its contents to generate the other data structure. let's call your first array $input and your second array $output:
// initialize output array
$output = [];
// loop through each $appt in the $input array
foreach($input as $appt) {
// get shorter var names for appt data
$date = $appt['pc_eventDate'];
$name = $appt['name'];
$uname = $appt['ufname'].' '.$appt['ulname'];
// initialize each level of the data structure if it doesn't already exist
if(!isset($output[$date])) $output[$date] = [];
if(!isset($output[$date][$name])) $output[$date][$name] = [];
if(!isset($output[$date][$name][$uname])) $output[$date][$name][$uname] = [];
// initialize the number of appts to 0
if(!isset($output[$date][$name][$uname]['appointments'])) $output[$date][$name][$uname]['appointments'] = 0;
// increment the number of appts
$output[$date][$name][$uname]['appointments']++;
}
the important thing is the intialization of each sub-array in the new structure according to the data in the old structure -- from there we're just counting the number of appointments that match the new data.
good luck!
Say the array is question is $arr
This will arrange the array in the way you wanted as solution
foreach ($arr as $key => $value) {
if(!is_array($value['pc_eventDate]))
$value['pc_eventDate] = [];
if(!is_array($value['pc_eventDate]['name']))
$value['pc_eventDate]['name'] = [];
if(!is_array($value['pc_eventDate']['name']['ufname'.' ulname'])){
$value['pc_eventDate']['name']['ufname'.' ulname'] = [];
$value['pc_eventDate']['name']['ufname'.' ulname']['appointments'] = 1;
}else{
$value['pc_eventDate']['name']['ufname'.' ulname']['appointments'] += 1;
}
}
You need to do something like the above.
Try running the above code, there could be some typo. If its doesnt yields your desired result, try commenting out all the lines in the body of the foreach and var_dump() each step to test the building of the array structure.
Thanks

Multidimensional array element if empty delete entire sub-array PHP

I'm trying to delete sub array of my multidimensional array, if any of the value is empty, than delete entire sub array. I want a universal function for the same! Dont want to type specific keys. And than ReIndex the newly formed array.
My array is like
Array
(
[0] => Array
(
[name] => Test
[mobile] => 613594551
[email] => test#test.com
)
[1] => Array
(
[name] => Test1
[mobile] => 613594552
[email] => test2#test.com
)
[2] => Array
(
[name] => Test2
[mobile] => 613594553
[email] => test3#test.com
)
[3] => Array
(
[name] => Test3
[mobile] => 613594554
[email] => test4#test.com
)
)
So if my array is
Array
(
[0] => Array
(
[name] =>
[mobile] => 613594551
[email] => test#test.com
)
[1] => Array
(
[name] => Test1
[mobile] =>
[email] => test2#test.com
)
[2] => Array
(
[name] => Test2
[mobile] => 613594553
[email] =>
)
[3] => Array
(
[name] => Test3
[mobile] => 613594554
[email] => test4#test.com
)
)
Than display
Array
(
[0] => Array
(
[name] => Test3
[mobile] => 613594554
[email] => test4#test.com
)
)
Elaborating on Martin's answer, you can use array_filter() for both the source array and the nested array:
$filtered_array = array_filter($array, function($item){
return count($item) == count(array_filter($item));
});
sort($filtered_array); // to reindex
Working example: https://eval.in/521449
Use array_filter() to iterate all records for each person and remove those that have an empty value. Number of records before and after array_filter() has to be equal if all records are filled. If they're not remove the person using unset().
Note, that this function works in-place, so it modifies the original array.
<?php
$array = [
[
'name' => 'Test1',
'mobile' => 123456789,
'email' => null
], [
'name' => 'Test2',
'mobile' => 123456789,
'email' => 'test2#test.com'
], [
'name' => null,
'mobile' => 123456789,
'email' => 'test3#test.com'
],
];
function removeEmpty(&$arr) {
foreach ($arr as $index => $person) {
if (count($person) != count(array_filter($person, function($value) { return !!$value; }))) {
unset($arr[$index]);
}
}
}
removeEmpty($array);
print_r($array);
Prints:
Array
(
[1] => Array
(
[name] => Test2
[mobile] => 123456789
[email] => test2#test.com
)
)
Assuming that an array item might have different keys:
$array = array(
0=>array('name'=>'','test'=>2),
1=>array('name'=>'sadad','test'=>2),
);
foreach($array as $index=>$item) {
$keys = array_keys($item); //here is the assumption
//you can define it before the foreach
//by checking the first array if YOU are 100% sure
//all items in the array have the same keys: name, mobile, email
foreach($keys as $key) {
if(empty($item[$key])) {
unset($array[$index]);
break;
}
}
}
var_dump($array);
Output:
array(1) {
[1]=>
array(2) {
["name"]=>
string(5) "sadad"
["test"]=>
int(2)
}
}

Using PHP to transform a 3D array into a 2D one

My form produces a 3 dimensional array. I would like to transform this array into a 2 dimensional one.
I tried this with no success:
$_rows = array();
foreach ($_contacts as $name => $_arr) {
foreach ($_arr as $key => $val) {
$_rows[] = array ($name => $val);
}
}
Data Source:
[_contacts] => Array
(
[name] => Array
(
[0] => foo
[1] => bar
)
[phone] => Array
(
[0] => 012345
[1] => 098765
)
[email] => Array
(
[0] => mail.com
[1] => yahoo.com
)
)
Desired output:
Array
(
[0] => Array
(
[name] => foo
[phone] => 012345
[email] => mail.com
)
[1] => Array
(
[name] => bar
[phone] => 098765
[email] => yahoo.com
)
)
Any thoughts were I went wrong?
$_rows = array();
foreach ($_contacts as $name => $_arr) {
foreach ($_arr as $key => $val) {
$_rows[$key][$name] = $val;
}
}

How can I flip an array when each value is also an array?

How would I go about flipping an array and establishing relationships between all the values and keys? For example:
I am trying to turn this:
Array (
[11913] => Array (
[0] => 4242
[1] => 3981
)
[9878] => Array (
[0] => 2901
[1] => 3981
)
[11506] => Array (
[0] => 3981
[1] => 2901
)
)
Into this:
Array (
[3981] => Array (
[0] => 11506
[1] => 9878
[2] => 11913
)
[2901] => Array (
[0] => 11506
[1] => 9878
)
[4242] => Array (
[0] => 11913
)
)
Are there any PHP functions that will already do this automatically? If not what would be a way of going about this? Can't seem to wrap my head around it.
Here you go.
$final_array = array();
foreach($initial_array as $key => $val){
foreach($val as $v){
$final_array[$v][] = $key;
}
}

Categories