I have the following array
Array
(
[tags] => Array
(
[0] => hello
)
[assignee] => 60b6a8a38cf91900695dd46b
[multiple_assignee] => Array
(
[0] => Array
(
[accountId] => 60b6a8a38cf91900695dd46b
)
[1] => Array
(
[accountId] => 5b39d23d32e26a2de15f174f
)
)
)
I want to remove 60b6a8a38cf91900695dd46b from the multiple_assignee array.
I have tried with the following code:
if (($key = array_search($this->getUsersHashMapValue($responsiblePartyIds[0]), $mutipleAssignee)) !== false) {
unset($mutipleAssignee[$key]['accountId']);
}
But it is not removing that element. The intention is I don't want to repeat the 60b6a8a38cf91900695dd46b assignee in the multiple assignee array.
I have also tried with the following code:
foreach($mutipleAssignee as $subKey => $subArray){
if($subArray['accountId'] == $this->getUsersHashMapValue($responsiblePartyIds[0])){
unset($mutipleAssignee[$subKey]);
}
}
But it is resulting as
Array
(
[tags] => Array
(
[0] => hello
)
[assignee] => 60b6a8a38cf91900695dd46b
[multiple_assignee] => Array
(
[1] => Array
(
[accountId] => 5b39d23d32e26a2de15f174f
)
)
)
rather than
[multiple_assignee] => Array
(
[0] => Array
(
[accountId] => 5b39d23d32e26a2de15f174f
)
)
Thank you
Just extract the accountId column and search that. Then use that key:
$key = array_search($this->getUsersHashMapValue($responsiblePartyIds[0]),
array_column($mutipleAssignee, 'accountId'));
unset($mutipleAssignee[$key]);
After your edit it seems you just want to reindex the subarray after unset:
$mutipleAssignee = array_values($mutipleAssignee);
I would just use a simple for loop. All of the array_* functions are really helpful but I find that they hide nuances. Since I don't have your functions I'm just making a plain-old one, but you should be able to port this.
$data = [
'tags' => [
'hello',
],
'assignee' => '60b6a8a38cf91900695dd46b',
'multiple_assignee' => [
[
'accountId' => '60b6a8a38cf91900695dd46b',
],
[
'accountId' => '5b39d23d32e26a2de15f174f',
],
],
];
$assignee = $data['assignee'];
foreach ($data['multiple_assignee'] as $multiple_assignee_key => $multiple_assignee) {
// if the root assignee is also listed in the multiple assignee area
if ($multiple_assignee['accountId'] === $assignee) {
// remove the duplicate from the multiple area
unset($data['multiple_assignee'][$multiple_assignee_key]);
// re-index the array
$data['multiple_assignee'] = array_values($data['multiple_assignee']);
}
}
This outputs:
array(3) {
["tags"]=>
array(1) {
[0]=>
string(5) "hello"
}
["assignee"]=>
string(24) "60b6a8a38cf91900695dd46b"
["multiple_assignee"]=>
array(1) {
[0]=>
array(1) {
["accountId"]=>
string(24) "5b39d23d32e26a2de15f174f"
}
}
}
Demo here: https://3v4l.org/tYppK
Related
I have two multidimensional arrays:
Array (
[1] => Array
(
[id] => 1
[email] => aaa#mail.com
some irrelevant pairs
)
[2] => Array
(
[id] => 2
[email] => bbb#mail.com
some irrelevant pairs
)
[3] => Array
(
[id] => 3
[email] => ccc#mail.com
some irrelevant pairs
))
Array (
[1] => Array
(
[id] => 4
[email] => aaa#mail.com
some irrelevant pairs
)
[2] => Array
(
[id] => 5
[email] => bbb#mail.com
some irrelevant pairs
)
[3] => Array
(
[id] => 6
[email] => ccc#mail.com
some irrelevant pairs
))
As you see they both have key 'email' with the same values. How to extract the pairs of IDs as key-value pairs in the separate array as a map of the same emails ? I tried with foreach loops and in_array function, but something was wrong. This is what I need:
Result (pairs of IDs where e-mail values are the same:
my_array_with_pairs_of_IDs = ( "1" => "4", "2" => "5", "3" => "6" );
Please help, Thanks a lot!
Performance optimized, based on #Andreas answer:
$map1 = array_column($arr1, 'id', 'email');
$map2 = array_column($arr2, 'id', 'email');
$result = [];
foreach ($map1 as $email => $id)
{
if (isset($map2[$email]))
{
$result[$id] = $map2[$email];
}
}
removed $arr1 and $arr2 overriding;
added $result declaration.
You can use array_column to make two flat arrays that you can use array_search in.
If array_search returns not false, it's the key of the found match.
$arr1 = array_column($arr1, 'email', 'id');
$arr2 = array_column($arr2, 'email', 'id');
foreach($arr1 as $key => $val){
$find = array_search($val, $arr2);
if($find !==false) $result[$key] = $find;
}
var_dump($result);
Output:
array(3) {
["1 "]=>
string(2) "4 "
["2 "]=>
string(2) "5 "
["3 "]=>
string(2) "6 "
}
https://3v4l.org/6XhUd
You can use this method and store data in new array
$result = [];
foreach($a1 as $a){
foreach($b1 as $b){
if($b[email] == $a[email])
$result[] = [$a[id]=>$b[id]];
}
}
I have three following arrays.
Array ( [0] => 395 [1] => 295 )
Array ( [0] => 5a3a13f237715637629.jpeg [1] => 5a3b602654cfd527057.jpg )
Array ( [0] => Seller Test Product [1] => Offline Product for Test )
The first array is the quantity, the second array is for the images, the third array is for the name of the products.
I want to combine all these three array into one and display it using foreach loop in PHP.
if I use array_merge(), I am getting the output:
Array ( [0] => 395 [1] => 295 ) Array ( [0] => 5a3a13f237715637629.jpeg [1] => 5a3b602654cfd527057.jpg ) Array ( [0] => Seller Test Product [1] => Offline Product for Test ) Array ( [0] => 5a3a13f237715637629.jpeg [1] => 5a3b602654cfd527057.jpg [2] => Seller Test Product [3] => Offline Product for Test [4] => 395 [5] => 295 )
Now, how can I display it using foreach loop in the view.
in the view the code is :
<?php foreach($c as $key => $strs)
{ ?>
<img style="width:150px;" src="<?php echo #getimagesize(base_url("assets/upload/product/".$key)) ? base_url("assets/upload/product/".$key):'http://placehold.it/350x200';?>" class="img-responsive">
<input type="text" name="vala" value="<?php echo $strs; ?>">
<input type="text" name="valas" value="<?php echo $strss; ?>">
<?php } ?>
Any help is welcome.
So what you really want is to group all fields of all arrays together. Values with the same index shall be merged into a single object. array_map() can be used for this.
$final = array_map(function($quantity, $image, $name) {
return (object)['quantity' => $quantity, 'image' => $image, 'name' => $name];
}, $quantityArray, $imageArray, $nameArray);
The result will be:
[
{
'qunatity' => 395,
'image' => '5a3a13f237715637629.jpeg',
'name' => 'Seller Test Product'
},
{
'qunatity' => 295,
'image' => '5a3b602654cfd527057.jpeg',
'name' => 'Offline Product for Test'
}
]
You can then address them in your foreach like this:
foreach($final as $product) {
echo $product->name;
echo $product->image;
echo $product->quantity;
}
For those of you who are in a real hurry, the following call to array_map() will do the same trick, but without mapping the array fields to a specific key in the new multidimensional array:
$final = array_map(NULL, $quantityArray, $imageArray, $nameArray);
The result will be:
[
[
0 => 395,
1 => '5a3a13f237715637629.jpeg',
2 => 'Seller Test Product'
],
[
0 => 295,
1 => '5a3b602654cfd527057.jpeg',
2 => 'Offline Product for Test'
],
]
The inner arrays of the newly created mltidimensinal array will be filled in the order of which the arrays were provided to array_map().
You can loop one array and use key to get the corresponding value from the other arrays.
$a=array ( 0 => 395,1 => 295 );
$b=array ( 0 =>" 5a3a13f237715637629.jpeg" ,1 => "5a3b602654cfd527057.jpg" ) ;
$c=array ( 0 => "Seller Test Product",1 => "Offline Product for Test" );
Foreach($a as $key => $val){
$res[$key]['qty'] = $val;
$res[$key]['img'] = $b[$key];
$res[$key]['desc'] = $c[$key];
}
Var_dump($res);
Output:
array(2) {
[0]=>
array(3) {
["qty"]=> int(395)
["img"]=> string(25) " 5a3a13f237715637629.jpeg"
["desc"]=> string(19) "Seller Test Product"
}
[1]=>
array(3) {
["qty"]=> int(295)
["img"]=> string(23) "5a3b602654cfd527057.jpg"
["desc"]=> string(24) "Offline Product for Test"
}
}
https://3v4l.org/h8B0u
Example Code
<?php
$array1=array ( 0 => 395,1 => 295 );
$array2=array ( 0 =>" 5a3a13f237715637629.jpeg" ,1 => "5a3b602654cfd527057.jpg" ) ;
$array3=array ( 0 => "Seller Test Product",1 => "Offline Product for Test" );
echo "<pre>";
print_r($array1);print_r($array2);print_r($array3);
$result=array_merge($array1,$array2,$array3);
print_r($result);
?>
I am trying to get value of is_activated column key. But can not get the exact value. Have tried array_search()
Review the array
Need to check value of all is_activated key whether it's value is 1 or not.
Array
(
[0] => Array
(
[first_name] => Array
(
[0] => john
)
[is_activated] => Array
(
[0] => 0
)
)
[1] => Array
(
[first_name] => Array
(
[0] => mark
)
[is_activated] => Array
(
[0] => 1
)
)
[2] => Array
(
[first_name] => Array
(
[0] => pretik
)
[is_activated] => Array
(
[0] => 0
)
)
)
I have tried this below solution but can can not get result.
$is_user_activated = array_search(1,array_column($activity,'is_activated'));
if($is_user_activated == 1) { echo 'yes'; }
else { echo 'no'; }
I think you want to be doing this in a loop rather than using array_search. Use a foreach() to get your desired result:
foreach($yourArray as $item) {
if($item['is_activated'][0] == 1) {
echo 'yes';
} else {
echo 'no';
}
}
You can use the array_filter() for such tasks, it allows to use a callback filter function:
<?php
$data = [
[
'first_name' => ['john'] ,
'is_activated' => [0]
],
[
'first_name' => ['mark'],
'is_activated' => [1]
],
[
'first_name' => ['pretik'],
'is_activated' => [0]
]
];
$matches = array_filter($data, function($entry) {
return in_array(1, $entry['is_activated']);
});
var_dump($matches);
The output of that is:
array(1) {
[1]=>
array(2) {
["first_name"]=>
array(1) {
[0]=>
string(4) "mark"
}
["is_activated"]=>
array(1) {
[0]=>
int(1)
}
}
}
Reason why that is a bit awkward is that your initial data has a very strange structure: the values of the elements are arrays themselves holding the actual values instead of scalar values themselves. That makes searching more complex than in "normal" situations. So take a good look if maybe you can fix that strange structure instead to be able to use an easier search approach.
You can get the activated users via array_filter:
$activated = array_filter($users, function($record) {
return reset($record['is_activated']) == 1;
});
This will only keep users who are activated, you can then simply count the array to see if you have any activated users:
echo count($activated) ? 'yes' : 'no';
Example here: http://ideone.com/mMuJcO
I'm having few arrays as follows. Actually I'm having too many such arrays but for your reference I've printed only few of them:
Array
(
[0] => lineItemData
[1] => name
)
Array
(
[0] => lineItemData
[1] => startDate
)
Array
(
[0] => lineItemData
[1] => endDate
)
Array
(
[0] => lineItemData
[1] => frequencyCapping
[2] => interval
)
Array
(
[0] => lineItemData
[1] => frequencyCapping
[2] => amount
)
Array
(
[0] => orderId
)
Array
(
[0] => isExternal
)
Now you can observe in man of the above arrays key value [lineItemData] is common and it is present at oth index. Now I want to create a new array where the key would be [lineItemData] and other arrays which don't have a value [lineItemData] present within themselves should be new keys and other keys should be keys under every key. My question may confuse you. So I'm printing below the desired output array
Array
(
[lineItemData] => Array
(
[name] =>
[startDate] =>
[endDate] =>
[frequencyCapping] => Array
(
[interval] =>
[amount] =>
)
)
[orderId] =>
[isExternal] =>
)
You can do this with:
$data = [
['lineItemData', 'name'],
['lineItemData', 'startDate'],
['lineItemData', 'endDate'],
['lineItemData', 'frequencyCapping', 'interval'],
['lineItemData', 'frequencyCapping', 'amount'],
['orderId'],
['isExternal']
];
$result = [];
$pointer = &$result;
foreach($data as $keys)
{
foreach($keys as $key)
{
if(is_array($pointer) && !array_key_exists($key, $pointer))
{
$pointer[$key] = null;
}
$pointer = &$pointer[$key];
}
$pointer = &$result;
}
End result will look like:
array(3) {
["lineItemData"]=>
array(4) {
["name"]=>
NULL
["startDate"]=>
NULL
["endDate"]=>
NULL
["frequencyCapping"]=>
array(2) {
["interval"]=>
NULL
["amount"]=>
NULL
}
}
["orderId"]=>
NULL
["isExternal"]=>
NULL
}
Maybe like so?
<?php
$super['lineItemData']['name'] = NULL;
$super['lineItemData']['startDate'] = NULL;
$super['lineItemData']['endDate'] = NULL;
$super['lineItemData']['frequencyCapping']['interval'] = NULL;
$super['lineItemData']['frequencyCapping']['amount'] = NULL;
$super['orderId'] = NULL;
$super['isExternal'] = NULL; ?>
I'm sure someone will get crafty and find a way to make this happen in one array statement. I like this because it's easier for me to manage.
This is pretty basic, but my question is:
Given an array:
$a = array(
0 => array('Rate'=> array('type_id'=>1, 'name' => 'Rate_1', 'type'=>'day','value'=>10)),
1 => array('Rate'=> array('type_id'=>1, 'name' => 'Rate_2', 'type'=>'night','value'=>8)),
2 => array('Rate'=> array('type_id'=>2, 'name' => 'Rate_3', 'type'=>'day','value'=>7)),
3 => array('Rate'=> array('type_id'=>2, 'name' => 'Rate_4', 'type'=>'nigh','value'=>16)),
4 => array('Rate'=> array('type_id'=>3, 'name' => 'Rate_5', 'type'=>'day','value'=>10))
);
What is the most efficient way to change it so we have something like:
$new_array = array(
[type_id] => array(
[type] => array(
[value]
)
)
)
);
In other words, I would like to strip some data (the name, which I don't need) and reorganise the dimensions of the array. In the end I would have an array which I would be able to access the values by $new_array['type_id']['type']['value'].
Not entirely sure if this is exactly what you want, but with this you can access the values by saying
echo $new[TYPE_ID][DAY_OR_NIGHT];
$new = array();
foreach($a AS $b){
$c = $b['Rate'];
$new[$c['type_id']][$c['type']] = $c['value'];
}
Using print_r on $new would give you:
Array
(
[1] => Array
(
[day] => 10
[night] => 8
)
[2] => Array
(
[day] => 7
[night] => 16
)
[3] => Array
(
[day] => 10
)
)
Since php 5.3.0, array_reduce() allows using an array as the initial value, given your initial array $a, you can use the following code
function my_reducer ($result, $item) {
$result[$item['Rate']['type_id']][$item['Rate']['type']] = $item['Rate']['value'];
return $result;
}
$assoc_arr = array_reduce($a, 'my_reducer', array());
var_dump($assoc_arr);
This returns
array(3) { [1]=> array(2) {
["day"]=>
int(10)
["night"]=>
int(8) } [2]=> array(2) {
["day"]=>
int(7)
["nigh"]=>
int(16) } [3]=> array(1) {
["day"]=>
int(10) } }