Can I read array key from inside the array itself , please to suggest php functions and not foreach loops , as I am trying to avoid loops as much as possible?
code looks like this :
array_fill_keys(array('a','b','c', 'd'),array(
'action'=>'getUserLongTermCategoriesAction',
'params'=> 'place key here',
)
Check this.
$arr = array('a','b','c','d');
$temp = array_map(function ($keys) {
return array(
'action'=>'getUserLongTermCategoriesAction',
'params'=> $keys,
);
}, $arr);
$result = array_combine($arr, $temp);
Output:
Array
(
[a] => Array
(
[action] => getUserLongTermCategoriesAction
[params] => a
)
[b] => Array
(
[action] => getUserLongTermCategoriesAction
[params] => b
)
[c] => Array
(
[action] => getUserLongTermCategoriesAction
[params] => c
)
[d] => Array
(
[action] => getUserLongTermCategoriesAction
[params] => d
)
)
Related
I have a multidimensional array that looks like such:
Array
(
[0] => Array
(
[email] => email1#gmail.com
[added] => style-narcotics
)
[1] => Array
(
[email] => email1#gmail.com
[added] => style-edm
)
[2] => Array
(
[email] => email2#gmail.com
[added] => style-codeine
)
[3] => Array
(
[email] => email2#gmail.com
[added] => style-food
)
)
I want to merge all the inner arrays combining the "added" key like such:
Array
(
[0] => Array
(
[email] => email1#gmail.com
[added] => array(
[0]=>style-narcotics
[1]=>style-edm
)
)
[1] => Array
(
[email] => email2#gmail.com
[added] => array(
[0]=>style-codeine
[1]=>style-food
)
)
I have tried merge array recursive in different forms and call_user_func but it doesnt cut it. Any advice? Thanks!
I would call it "grouping", but not "merging".
Use the following approach with array_walk and array_values functions:
$grouped = [];
// $arr is your initial array
array_walk($arr, function($v) use (&$grouped){
if (array_key_exists($v["email"], $grouped)) {
$grouped[$v["email"]]["added"][] = $v["added"];
} else {
$v["added"] = [$v["added"]];
$grouped[$v["email"]] = $v;
}
});
print_r(array_values($grouped));
The output:
Array
(
[0] => Array
(
[email] => email1#gmail.com
[added] => Array
(
[0] => style-narcotics
[1] => style-edm
)
)
[1] => Array
(
[email] => email2#gmail.com
[added] => Array
(
[0] => style-codeine
[1] => style-food
)
)
)
A simple solution that uses array_reduce():
$output = array_reduce(
$input,
function (array $carry, array $item) {
$email = $item['email'];
if (! isset($carry[$email])) {
// It's a new email, make room for it in the output
$carry[$email] = array('email' => $email, 'added' => array(), );
}
// Add the value of $item['added'] into the existing array
$carry[$email]['added'][] = $item['added'];
return $carry;
},
array()
);
// Output is indexed by email addresses. If you need numeric keys then...
$output = array_values($output);
The same logic as above but with an explicit iteration over the input array (the code is a couple of lines shorter):
$output = array();
foreach ($input as $item) {
$email = $item['email'];
if (! isset($carry[$email])) {
// It's a new email, make room for it in the output
$carry[$email] = array('email' => $email, 'added' => array(), );
}
// Add the value of $item['added'] into the existing array
$carry[$email]['added'][] = $item['added'];
}
$output = array_values($output);
I have this array
$array = Array
(
[a] => Array
(
[0] => b
[1] => h
)
[b] => c
[c] => d
[h] => m
)
And I need to convert the array to like below
Array
(
[a] => Array
(
[b] => Array
(
[c] => Array
(
[d] => Array
(
)
)
)
[h] => Array
(
[m] => Array
(
)
)
)
)
I already asked this question for One Dimensional array.
I tried with [Creating nested parent child array from one dimensional array in php and I got the below array
Array
(
[a] => Array
(
[b] => Array
(
[a] => Array
(
[h] => Array
(
[b] => Array
(
[c] => Array
(
[d] => Array
(
)
[h] => Array
(
[m] => Array
(
)
)
)
)
)
)
)
)
)
How to Check wheather the key is present in multi dimensional array and if present add the child to the existing key. Help to resolve the Problem. Thanks in Advance
<?php
$array = array(
'a' => array(0=>'b',1=>'h'),
'b' => 'c',
'c' => 'd',
'h' => 'm',
);
$newArray = array();
$secondarray = array();
$part = &$newArray;
$i=1;
foreach($array as $first => $second)
{
if($i==1)
{
$firstone=$first;
}
else
{
if($i==count($array))
{
$newArray[$first] = array($second => array());
$secondarray[$firstone]=$newArray;
}
else
{
$part = &$part[$first];
$part[$second] = array();
}
}
$i++;
}
echo '<pre>';print_r($secondarray);
output
Array
(
[a] => Array
(
[b] => Array
(
[c] => Array
(
[d] => Array
(
)
)
)
[h] => Array
(
[m] => Array
(
)
)
)
)
I have array multi dimension from looping foreach.
I want to insert new data from array at the end of array multi dimension like this :
Array
(
[setting_code] => gen_logo
[setting_value] => logo.png
)
Array
(
[0] => Array
(
[setting_code] => gen_site_name
[setting_value] => Codeigniter School CMS
)
[1] => Array
(
[setting_code] => gen_email_info
[setting_value] => info#schoolapp.sch
)
[2] => Array
(
[setting_code] => gen_meta_author
[setting_value] => awnLabs.co
)
)
then the result I want like this :
Array
(
[0] => Array
(
[setting_code] => gen_site_name
[setting_value] => Codeigniter School CMS
)
[1] => Array
(
[setting_code] => gen_email_info
[setting_value] => info#schoolapp.sch
)
[2] => Array
(
[setting_code] => gen_meta_author
[setting_value] => awnLabs.co
)
[3] =>Array
(
[setting_code] => gen_logo
[setting_value] => logo.png
)
)
how to get result like the above? thank's for help before :)
Simply can assign using $arr2[] = $arr1 or array_push(). Example:
$arr1 = Array
(
"setting_code" => "gen_logo",
"setting_value" => "logo.png",
);
$arr2 = array
(
0 => array
(
"setting_code" => "gen_site_name",
"setting_value" => "Codeigniter School CMS"
),
1 => array
(
"setting_code" => "gen_email_info",
"setting_value" => "info#schoolapp.sch"
),
2 => array
(
"setting_code" => "gen_meta_author",
"setting_value" => "awnLabs.co",
)
);
$arr2[] = $arr1;
// or
//array_push($arr2, $arr1);
print "<pre>";
print_r($arr2);
print "</pre>";
Use array_push to insert data at the end of array.
You should use array_merge()function.
Here is the example:
$arr1 =
Array
(
"setting_code" => "gen_logo",
"setting_value" => "logo.png",
);
$arr2 =
Array
(
Array
(
"setting_code" => "gen_site_name",
"setting_value" => "Codeigniter School CMS"
),
//Rest elements
);
$result = array_merge($arr2, $arr1);
array_push is simple and easy to for this situation. For example:
$logo_1 = array
(
'setting_code' => 'gen_logo',
'setting_value' => 'logo.png'
);
$logo_2 = array
(
'setting_code' => 'gen_logo',
'setting_value' => 'logo.png'
);
$arr = array();
array_push($arr, $logo_1);
array_push($arr, $logo_2);
var_dump($arr);
$var=array();
$test_arr['setting_code']='gen_site_name';
$test_arr['setting_value']='Codeigniter School CMS';
array_push($var, $test_arr);
$test_arr['setting_code']='gen_email_info';
$test_arr['setting_value']='info#schoolapp.sch';
array_push($var, $test_arr);
$test_arr['setting_code']='gen_meta_author';
$test_arr['setting_value']='awnLabs.co';
array_push($var, $test_arr);
foreach ($var as $key=>$value){
$arr[$key]=$value;
}
By using this code you will get the output you need, a small example of array_push
Thanks
I am new in PHP and love to learn it.
I want to merge two or more arrays having same keys only. And neglect the arrays whose keys are not in both the Arrays. Like
Here is the First Array :
Array
(
[1] => Array
(
[111] => 36265
)
[2] => Array
(
[222] => 36265
)
[3] => Array
(
[333] => 36265
)
)
and Second Array as :
Array
(
[1] => Array
(
[444] => 36265
)
[2] => Array
(
[555] => 36265
)
[4] => Array
(
[666] => 36265
)
)
And i want my result to be as :
Array
(
[1] => Array
(
[111] => 36265
[444] => 36265
)
[2] => Array
(
[222] => 36265
[555] => 36265
)
)
Neglecting the rest Array with key [3] & [4]....
So Please anyone tell me how to get this. I try out "array_merge_recursive()" but this one displays all keys.
Thanks in advance.
You'd have to loop over one of the arrays, check for the existence of the current key in the other array, if it exists, merge them, eg:
$output = array();
foreach ($array1 as $key => $value) {
if (array_key_exists($key, $array2)) {
$output[$key] = $value + $array2[$key];
}
}
Here's a demo
You're probably looking for array_intersect_key.
You could also use a foreacah loop and create a new one. While in this loop you can use one of the arrays keys to match them both. Consider this example:
$array1 = array( 1 => array(111 => 36265), 2 => array(222 => 36265), 3 => array(333 => 36265), ); $array2 = array( 1 => array(444 => 36265), 2 => array(555 => 36265), 4 => array(666 => 36265), );
$new_array = array();
foreach($array1 as $key => $value) {
if(isset($array2[$key])) {
$new_array[$key][key($array1[$key])] = reset($array1[$key]);
$new_array[$key][key($array2[$key])] = reset($array2[$key]);
}
}
echo '<pre>';
print_r($new_array);
echo '</pre>';
Should yield something like this:
Array
(
[1] => Array
(
[111] => 36265
[444] => 36265
)
[2] => Array
(
[222] => 36265
[555] => 36265
)
)
I have the following Two array results coming from MySQL query result.
Array One (Orignal-Data):
Array
(
[w_a] => Array
(
[0] => Array
(
[cod] => CRR
[pr] => LL
[aid] => VM2254
[gender] => m
[title] =>
)
)
[w_a_ml] => Array
(
)
[w_a_ol] => Array
(
)
[w_a_rl] => Array
(
[0] => Array
(
[rol] => 1
)
)
)
Array Two (Changed-Data)
Array
(
[w_a] => Array
(
[0] => Array
(
[cod] => CRR
[pr] => LL
[aid] => VM2254
[gender] => f
[title] => Mr
)
)
[w_a_ml] => Array
(
[0] => Array
(
[wl] => 255
[care] => Sahan
[heigh] =>
[adam] =>
[instance] => Look
)
)
[w_a_ol] => Array
(
)
[w_a_rl] => Array
(
[0] => Array
(
[rol] => 1
)
)
)
What I wan to achieve from the above two array is to compare and show the result in table format or Inside Form To each Field (Original and New-Change) like below.
FiledsN Original New Change
title Empty Mr
Wl Empty 255
Care Empty Sahan
gender m f
instance Empty Look
I've tried making the array a single array using this function:
function array_single($arr) {
if (!is_array($arr)) {
return FALSE;
}
$res = array();
foreach ($arr as $keys => $values) {
if (is_array($values)) {
$res = array_merge($res, array_single($values));
} else {
$res[$keys] = $values;
}
}
return $res;
}
And then comparing using this:
function diff($new,$old) {
$del=array_diff_assoc($old,$new);
$add=array_diff_assoc($new,$old);
return $diff=array("old"=>$del, "new"=>$add);
}