I need to merge a new array of alternative information into the loop if they have the alternative information in their profile.
Here's my loop:
foreach ($doctor->getVars() as $k => $v)
{
$data['doctor_'. $k] = $v;
}
foreach ($patient->get_data() as $k=>$v)
{
if (is_string($v) || is_numeric($v))
$data["patient_" . $k] = strtoupper($v);
}
Here's the $data var_dump:
Array
(
[employee] => person
[date] => 05/08/2013
[datetime] => 05/08/2013 9:41:15 AM
[department] => stuff
[employee_ext] => 7457
[employee_email] =>
[barcode] => *NZS01*
[doctor_df_code] => 09HQ
[doctor_npi] => 1111111111
[doctor_dea] => B4574
[doctor_upin] =>
[doctor_license] =>
[doctor_phone] => (111)111-1111
[doctor_fax] => (000)000-0000
[doctor_fname] => UNDEFINED
[doctor_lname] => UNDEFINED
[doctor_title] =>
[doctor_intake_rx_caller_id] =>
[doctor_costco_rx_caller_id] =>
[doctor_reorder_rx_caller_id] =>
[doctor_address1] => 24 CABELL st
[doctor_address2] => SUITE 10
[doctor_city] => places
[doctor_state] => CA
[doctor_zip] => 91111
[doctor_active_events] =>
[doctor_dont_call] => 0
[doctor_dont_fax] => 1
)
I need to merge the below array into the above array.
Here's the print var for the function addr($dfcode):
Array
(
[0] => Array
(
[CODE_] => 09HQ
[doctor_address1] => alternate addy
[doctor_address2] => 45854
[doctor_city] => different city
[doctor_state] => CA
[doctor_zip] => 963545
[doctor_phone] => (619)111-2548
[doctor_fax] => (157)123-4569
)
)
I'm new to array merge and I'm assuming right after the $data['doctor_'. $k] = $v i could list out the new function and the fields i want to merge in particular?
syntax is what i'm not sure on:
$data['doctor_'. $k] . array_merge(addr($dfcode))['doctor_address1'] = $v;
Any help would be greatly appreciated, thank you.
The general formula for merging two arrays is as follows (merging $array_m into $array_o):
foreach($array_m as $key=>$value){
$array_o[$key] = $value;
}
$array_o would now contain all of the elements of $array_m
EDIT: I just noticed in your post that you seem to want to use the array_merge function. You could also do the following:
$array_o = array_merge($array_o, array_m);
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 9 months ago.
First of all this is not a duplicate question as i have tried most of the stack answer.
I have about 138886 records in my array.
records are like
[1] => Array
(
[country] => US
[state] => Albama
[city] => Brest
[postcode] => 225001-225003
[shipping_info] => Delivery Available
[is_zip_range] => 1
[zip_from] => 225001
[zip_to] => 225003
)
[2] => Array
(
[country] => BY
[state] => Brest
[city] => Brest
[postcode] => 225001-225003
[shipping_info] => Delivery Available
[is_zip_range] => 1
[zip_from] => 225001
[zip_to] => 225003
)
I want to unique all record from postcode value i have tried some method are
Method 1
$temp = array_unique(array_column($data, 'postcode'));
$filteredData = array_intersect_key($data, $temp);
but it is still giving duplicate value.
Method 2
$filteredData = array_map("unserialize", array_unique(array_map("serialize", $data)));
this is won't work
Method 3
function unique_multidim_array($array, $key) {
$temp_array = array();
$i = 0;
$key_array = array();
foreach($array as $val) {
if (!in_array($val[$key], $key_array)) {
$key_array[$i] = $val[$key];
$temp_array[$i] = $val;
}
$i++;
}
return $temp_array;
}
$details = unique_multidim_array($array,'postcode');
It is working but too slow taking about 2/3 mins.
let me know any other method i can use for unique array.
help will be apprecitaed
Why are you making it so hard,
$in = array (
1 =>
array (
'country' => 'US',
'state' => 'Albama',
'city' => 'Brest',
'postcode' => '225001-225003',
'shipping_info' => 'DeliveryAvailable',
'is_zip_range' => 1,
'zip_from' => 225001,
'zip_to' => 225003
),
2 =>
array (
'country' => 'BY',
'state' => 'Brest',
'city' => 'Brest',
'postcode' => '225001-225003',
'shipping_info' => 'DeliveryAvailable',
'is_zip_range' => 1,
'zip_from' => 225001,
'zip_to' => 225003
)
);
$out = [];
foreach($in as $i) if(!isset($out[$i['postcode']])) $out[$i['postcode']] = $i;
Sandbox
You can do the same thing with in_array, but the isset is faster.
In fact you dont even need to do the isset
foreach($in as $i) $out[$i['postcode']] = $i;
Array keys are always unique, but this will retain the last duplicate where as the previous code keeps the first one.
And if the keys bug you latter just do $out = array_values($out) to reset them.
I have an array with same customerid. I want to merge all same customerid arrays in to one with few amends to the array.
Array
(
[0] => Array
(
[customerid] => 13
[customer_fullname] => Chris
[profession_id] => 8
[profession_name] => Producer
)
[1] => Array
(
[customerid] => 1
[customer_fullname] => John
[profession_id] => 8
[profession_name] => Producer
)
[2] => Array
(
[customerid] => 13
[customer_fullname] => Chris
[profession_id] => 7
[profession_name] => Camera
)
)
So now I want a new array to be created like this:
Array(
[customerid] => 13
[customer_fullname] => Chris
[new_array] => array(
[0]=>[profession_id] => 8, [profession_name] => Producer,
[1]=>[profession_id] => 7, [profession_name] => Camera
)
)
Spent some time on it but wasn't able to get it right
There are better approaches if you're merging lots of records, but if you want a way to just merge two records as stated, I'd just do this:
$array1 = array(
'customerid' => 13
'customer_fullname' => 'John',
'profession_id' => 8,
'profession_name' => 'Producer'
);
$array2 = array(
'customerid' => 13
'customer_fullname' => 'John',
'profession_id' => 7,
'profession_name' => 'Director'
);
function merge_customers($customerA, $customerB)
{
$newCustomer = array();
if ($customerA['customerid'] == $customerB['customerid'])
{
$newCustomer['customerid'] = $customerA['customerid'];
$newCustomer['customer_fullname'] = $customerA['customer_fullname'];
$newCustomer['new_array'] = array(
array(
'profession_id' => $customerA['profession_id'],
'profession_name' => $customerA['profession_name']
),
array(
'profession_id' => $customerB['profession_id'],
'profession_name' => $customerB['profession_name']
)
);
return $newCustomer;
}
/* We can't merge these if they're different customers. */
return NULL;
}
The extended solution which is also well-suited for finding and "merging" multiple groups of entries which has same customerid. Used functions: array_filter, array_count_values, array_keys, array_walk, array_chunk and array_values:
// supposing $arr is your initial array
// finds which 'customerid' has multiple entries
$dupIds = array_filter(array_count_values(array_column($arr, "customerid")), function($v) {
return $v > 1;
});
$dupIds = array_keys($dupIds);
$result = [];
array_walk($arr, function($v) use(&$result, $dupIds) {
if (in_array($v['customerid'], $dupIds)) {
$parts = array_chunk($v, 2, true);
if (!isset($result[$v['customerid']])) {
$result[$v['customerid']] = $parts[0] + ['new_array' => [$parts[1]]];
} else {
$result[$v['customerid']]['new_array'][] = $parts[1];
}
}
});
print_r(array_values($result));
The output:
Array
(
[0] => Array
(
[customerid] => 13
[customer_fullname] => Chris
[new_array] => Array
(
[0] => Array
(
[profession_id] => 8
[profession_name] => Producer
)
[1] => Array
(
[profession_id] => 7
[profession_name] => Camera
)
)
)
)
Quick hack, maybe there is a nicer solution.
Note: The second "for each" loop is only needed if there is the possibility that the arrays don't have the same fields.
function merge($array1, $array2){
$result = array();
foreach($array1 as $key => $value){
if(isset($array2[$key]) && $array2[$key]!=$array1[$key]){
$result[$key][]=$value;
$result[$key][]=$array2[$key];
}else{
$result[$key]=$value;
}
}
foreach($array2 as $key => $value){
if(!isset($result[$key])){
$result[$key] = $value;
}
}
return $result;
}
print_r(merge($array1, $array2));
I need to display a certain object from an array before showing the rest of the array.
The array looks like this:
Array
(
[0] => stdClass Object
(
[template_id] => 91
[template_name] => Alphabet
[template_thumbnail] => blank-template-thumbnail.jpg
[template_create_date] => 1456821665
[template_customer_id] => 0
[template_is_responsive] => no
[template_type] => builder
[template_category] => simple
[sort] => 2
)
[1] => stdClass Object
(
[template_id] => 92
[template_name] => Blank Template
[template_thumbnail] => blank-template-thumbnail.jpg
[template_create_date] => 1456821670
[template_customer_id] => 0
[template_is_responsive] => no
[template_type] => builder
[template_category] => simple
[sort] => 2
)
[2] => stdClass Object
(
[template_id] => 31
[template_name] => Holiday Specials
[template_thumbnail] => accommodation-1-20110926.jpg
[template_create_date] => 1456821660
[template_customer_id] => 0
[template_is_responsive] => no
[template_type] => builder
[template_category] => Accommodation
[sort] => 3
)
)
I need to show Blank Template first and then show the rest alphabetically (the order it is in now.
Is there a more elegant solution than looping through the array twice? The size of the array can be anything from 1 (the blank template) to countless objects.
$str="";
for($i=0;$i<=count($arr);$i++){
if($arr[$i]['template_name'] == "Blank Template"){
echo $arr[$i]['template_name'];
}else{
$str .= $arr[$i]['template_name']. "<br>";
}
}
echo $str;
Try this:
$firstItem = null;
$outArray = [];
foreach($yourArray as $item){
if($item->template_name == 'Blank Template'){
$firstItem = $item;
}else{
$outArray[$item->template_name] = $item;
}
}
ksort($outArray,SORT_STRING);
array_unshift($outArray,$firstItem);
Just one loop. Pay attention that this way of doing things, just work if you have uniqueness on the template_name!
Hope it helps.
This will work for you, try
<?php
$dataArray = array(0=>array('template_id'=>91,'template_name'=>'Alphabet'),
1=>array('template_id'=>92,'template_name'=>'Blank Template'),
2=>array('template_id'=>31,'template_name'=>'Holiday Specials')
);
$newArray = array();
foreach($dataArray as $key => $val)
{
if(in_array('Blank Template',$val))///find the key for black template
{
unset($dataArray[$key]); ///unset black temp from original array
$newArray[] = $val;///push black temp into new array at 0 index
foreach($dataArray as $k => $v)
{
$newArray[] = $v; ///push the renaming values into new array
}
}
}
echo "<pre>"; print_r($newArray);
?>
This will give you :
Array
(
[0] => Array
(
[template_id] => 92
[template_name] => Blank Template
)
[1] => Array
(
[template_id] => 91
[template_name] => Alphabet
)
[2] => Array
(
[template_id] => 31
[template_name] => Holiday Specials
)
)
LIVE EXAMPLE : CLICK HERE
i have an array
Array
(
[0] => Array
(
[folding_knives__no_assist__possession19] => G
[folding_knives__no_assist__possession___length20] =>
[folding_knives__no_assist___open_cary21] => G
[folding_knives__no_assist__open_carry_length22] =>
[folding_knives__no_assist__concealed23] => G
[folding_knives__no_assisted__concealed_length24] =>
[folding_knives__no_assist__concealed_w__ccw_required25] => R
[folding_knives__no_assist__concealed_w__ccw_required_l26] =>
[folding_knives__no_assist___notes27] =>
)
[1] => Array
(
[folding_knives__assisted_opening__possession28] => G
[folding_knives__assisted_opening__possession_length29] =>
[folding_knives__assisted_opening__open_carry30] => G
[folding_knives__assisted_opening__open_carry_length31] =>
[folding_knives__assisted_opening___concealed32] => G
[folding_knives__assisted_opening__concealed_length33] =>
[folding_knives__assisted_opening___concealed_w__ccw_re34] => R
[folding_knives__assisted_opening__concealed_w__ccw_req35] =>
[folding_knives___assisted_opening___notes36] =>
)
)
I already trying to show my array data into another array but cant
foreach ($chunks as $key => $val)
{
$allknife[] = array(
'name'=>$key[0],
'possession'=>$val[0],
'possession_length'=>$val[1]
);
}
Here name'=>$key[0] here will be arry first item key like folding_knives__no_assist__possession19
It should be link this, and you want field count link 1/2/3/4... then you have to use flag logic like, i=0; and i++
$allknife[] = array();
foreach ($chunks as $key => $val)
{
$allknife['name']=>$val['yourvaluesoeshere'],
$allknife['possession']=>$val['yourvaluesoeshere'],
$allknife['possession_length']=>$val['yourvaluesoeshere']
}
Since your arrays have inconsistent key names you can't rely on this an need consistent names first!
Map the array more like this:
array(
0 => array(
'assist' => true,
'open_carry_nr' => 21,
'open_carry_length' => 22
// and so on...
),
2 => array(
'assist' => false,
'open_carry_nr' => 18,
'open_carry_length' => 1337
// and so on...
)
);
Way better practice.
The problem is to change a tree structure to a simple array structure, in which each child has the parents who belongs to, the example is a directories and files structure, but I'm looking for a generic solution.
If the writing is bad, feel free to improve it.
Any help is welcome.
Example.
$array_1=array(
'f1' =>
array(
'f2' =>array('file1.php','file2.php'),
'f3' =>array('file3.php','file4.php'),
'f4' =>
array(
'fol5'=>
array('fileAA.php','fileBB.php')
,
'fileDD.php'
),
),
'f2' =>
array(
'f2' =>array('file1.php','file2.php'),
'f3' =>array('file3.php'),
)
);
The result should be like this:
/*
0 => '/f1/f2/file1.php',
1 => '/f1/f2/file2.php',
2 => '/f1/f3/file3.php',
3 => '/f1/f3/file4.php',
4 => '/f1/f4/fol5/fileAA.php',
5 => '/f1/f4/fol5/fileBB.php',
6 => '/f1/f4/fileDD.php',
7 => '/f2/f2/file1.php',
8 => '/f2/f2/file2.php',
9 => '/f2/f3/file3.php',
*/
here is simple recursive function:
function tree2array($input, &$output, $prefix = '')
{
foreach ($input as $i => $v)
if (is_array($v))
tree2array($v, $output, $prefix.'/'.$i);
else
$output[] = $prefix.'/'.$v;
}
usage:
tree2array($array_1, $array2);
output:
print_r($array2);
Array (
[0] => /f1/f2/file1.php
[1] => /f1/f2/file2.php
[2] => /f1/f3/file3.php
[3] => /f1/f3/file4.php
[4] => /f1/f4/fol5/fileAA.php
[5] => /f1/f4/fol5/fileBB.php
[6] => /f1/f4/fileDD.php
[7] => /f2/f2/file1.php
[8] => /f2/f2/file2.php
[9] => /f2/f3/file3.php )
I made an alternative solution using SPL
$arrayiter = new RecursiveArrayIterator($array_1);
$iteriter = new RecursiveIteratorIterator($arrayiter,RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iteriter as $key => $value) {
$this_depth = $iteriter->getDepth();
if(!is_array($value)){
$array_2[] = '/'.$value;
$level[]=$this_depth;
}
foreach($array_2 as $key2 => $value2){
if($this_depth < $level[$key2]){
$level[$key2] = $this_depth;
$array_2[$key2] = '/'.$key.$value2;
}
}
}
echo'<pre>';
print_r($array_2);
echo'</pre>';