I have an array $data:
Array
(
[0] => Array
(
[quarter] => Q1
[category] => DEODORANTS,FACE CARE
[fund_type] => EOT
)
[1] => Array
(
[quarter] => Q2
[category] => BODY CARE
[fund_type] => A&P
)
[2] => Array
(
[quarter] => Q2
[category] => ORAL CARE,NOCATEGORY
[fund_type] => A&P
)
)
and other array $categories:
Array
(
[0] => Array
(
[id] => 1
[descriptions] => DEODORANTS
)
[1] => Array
(
[id] => 2
[descriptions] => BODY CARE
)
[2] => Array
(
[id] => 3
[descriptions] => FACE CARE
)
[3] => Array
(
[id] => 4
[descriptions] => ORAL CARE
)
)
I need to change value of category in $data with id from $categories:
code:
function lookup($data = array(), $categories = array())
{
if(is_array($data))
{
foreach ($data as $k => $v)
{
$valCat = explode(',', $v['category']);
if(!empty($valCat))
{
foreach ($valCat as $vc)
{
foreach ($categories as $c)
{
if($c['descriptions'] === $vc)
{
$CatID[] = $c['id'];
}
}
}
}
$data[$k]['category'] = $CatID;
}
return $data;
}
}
result lookup($data, $categories):
Array
(
[0] => Array
(
[quarter] => Q1
[category] => Array
(
[0] => 1
[1] => 3
)
[fund_type] => EOT
)
[1] => Array
(
[quarter] => Q2
[category] => Array
(
[0] => 1
[1] => 3
[2] => 2
)
[fund_type] => A&P
)
[2] => Array
(
[quarter] => Q2
(
[0] => 1
[1] => 3
[2] => 2
[3] => 4
)
[fund_type] => A&P
)
)
correct result should be:
Array
(
[0] => Array
(
[quarter] => Q1
[category] => Array
(
[0] => 1
[1] => 3
)
[fund_type] => EOT
)
[1] => Array
(
[quarter] => Q2
[category] => Array
(
[0] => 2
)
[fund_type] => A&P
)
[2] => Array
(
[quarter] => Q2
(
[0] => 4
)
[fund_type] => A&P
)
)
You're not resetting the value of $CatID. After this line
foreach ($data as $k => $v)
{
add
$CatID = array();
Related
I'm getting all the HTML post values in the $postdata variable.
$postdata = $this->input->post();
unset($postdata['submit']);
unset($postdata['valve_no']);
Output of $postdata:
Array (
[1] => Array (
[0] => BH123
[1] => H89
)
[2] => Array (
[0] => BH123
[1] => H89
)
[3] => Array (
[0] => BH123
[1] => H89
)
[4] => Array (
[0] => BH123
)
)
$valve_no=$this->input->post('valve_no');
Output of $valve_no:
Array (
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
Next is I'm trying to merge both arrays
foreach($postdata as $key => $val)
{
$dataSet[] = array ('valve_no'=>$valve_no[$key-1],$postdata[$key]);
}
print_r($dataSet);
Output of $dataSet:
Array (
[0] => Array (
[valve_no] => 1
[0] => Array (
[0] => BH123
[1] => H89
)
)
[1] => Array (
[valve_no] => 2
[0] => Array (
[0] => BH123
[1] => H89
)
)
[2] => Array (
[valve_no] => 3
[0] => Array (
[0] => BH123
[1] => H89
)
)
[3] => Array (
[valve_no] => 4
[0] => Array (
[0] => BH123
)
)
)
The output I'm expecting is below:
Array (
[0] => Array (
[valve_no] => 1
[1] => BH123
[2] => H89
)
[1] => Array (
[valve_no] => 2
[1] => BH123
[2] => H89
)
[2] => Array (
[valve_no] => 3
[1] => BH123
[2] => H89
)
[3] => Array (
[valve_no] => 4
[1] => BH123
)
)
)
As you can see in the expected output I want to extract the sub-array and need to start with [1] instead of [0].
Thanks in advance.
Just change foreach() code like this: (As per your comment)
foreach($postdata as $key => $val)
{
$dataSet[$key-1]['valve_no'] = $valve_no[$key-1];
foreach($val as $k=>$v){
$dataSet[$key-1][$k+1] =$v;
}
}
print_r($dataSet);
Output: https://3v4l.org/P8NKs
Note: In case $postdata sub-array indexes not start with 0,1,2... and still you want them to start with 1,2,... in your result, then do like below:
foreach($postdata as $key => $val)
{
$dataSet[$key-1]['valve_no'] = $valve_no[$key-1];
$srNo = 1;
foreach($val as $v){
$dataSet[$key-1][$srNo] =$v;
$srNo++;
}
}
Output: https://3v4l.org/sLVv5
I have this array $all_zones that comes sometimes with missing keys and values and I would like to fill the array with empty values for the messing keys, here's the array:
Array
(
[0] => Array
(
[id_zone] => 1
[name] => Europe
[price] => Array
(
[0] => 3.00
[1] => 6.00
)
[id_delivery] => Array
(
[0] => 1
[1] => 2
)
)
[1] => Array
(
[id_zone] => 3
[name] => Asia
)
[2] => Array
(
[id_zone] => 4
[name] => Africa
[price] => Array
(
[0] => 3.00
[1] => 6.00
)
[id_delivery] => Array
(
[0] => 3
[1] => 4
)
)
[3] => Array
(
[id_zone] => 5
[name] => Oceania
)
)
The thing is the $all_zones[$key]['price'] depend on how many ranges there's for each Zone, inthis case $range_count = count($all_ranges); will display 2, so I'd like to fill the missing keys for 2 times : Here's the output:
Array
(
[0] => Array
(
[id_zone] => 1
[name] => Europe
[price] => Array
(
[0] => 3.00
[1] => 6.00
)
[id_delivery] => Array
(
[0] => 1
[1] => 2
)
)
[1] => Array
(
[id_zone] => 3
[name] => Asia
[price] => Array
(
[0] =>
[1] =>
)
[id_delivery] => Array
(
[0] =>
[1] =>
)
)
[2] => Array
(
[id_zone] => 4
[name] => Africa
[price] => Array
(
[0] => 3.00
[1] => 6.00
)
[id_delivery] => Array
(
[0] => 3
[1] => 4
)
)
[3] => Array
(
[id_zone] => 5
[name] => Oceania
[price] => Array
(
[0] =>
[1] =>
)
[id_delivery] => Array
(
[0] =>
[1] =>
)
)
)
Here's what I've tried so far and didn't succeed:
$range_count = count($all_ranges);
$i=0;
foreach ($all_zones as $key => $value) {
if(isset($value['id_zone']) && isset($value['name']) && (!isset($value['price']) || !isset($value['id_delivery']))){
if($range_count>$i){
$disabled[]=$key;
$all_zones[$key]['price'][] = '';
$all_zones[$key]['id_delivery'][] = '';
}
$i++;
}
}
Any help with this? Much appreciated.
try this
$range_count = count($all_ranges);
foreach ($all_zones as $key => $value) {
if(isset($value['id_zone']) && isset($value['name']) && (!isset($value['price']) || !isset($value['id_delivery']))){
$disabled[]=$key;
if((!isset($value['price']))
{
for($i=0; $i<$range_count<$i++)
{
$all_zones[$key]['id_delivery'][] = '';
}
}
if((!isset($value['id_delivery']))
{
for($i=0; $i<$range_count<$i++)
{
$all_zones[$key]['id_delivery'][] = '';
}
}
}
}
You might have operator precedence problem.
( (!isset($value['price']) || !isset($value['id_delivery'])) )
The way to do this is to loop through the array, with an array_merge() on each array within the parent array to set your 'defaults'.
$zone_template = array(
'id_zone' => '',
'name' => '',
'price' => array(
0 => '',
1 => ''
),
'id_delivery' = array(
0 => '',
1 => ''
)
);
foreach ($all_zones as $zone) {
array_merge($zone_template, $zone);
}
Can also be done with array_walk()
This is my array which is coming from a foreach loop:
Array
(
[count] => 1
[0] => Array
(
[distinguishedname] => Array
(
[count] => 1
[0] => CN=Administratie,OU=Test,DC=stefan,DC=nl
)
[0] => distinguishedname
[count] => 1
[dn] => CN=Administratie,OU=Test,DC=stefan,DC=nl
)
)
Array
(
[count] => 1
[0] => Array
(
[distinguishedname] => Array
(
[count] => 1
[0] => CN=Anderemailgroep,OU=Test,DC=stefan,DC=nl
)
[0] => distinguishedname
[count] => 1
[dn] => CN=Anderemailgroep,OU=Test,DC=stefan,DC=nl
)
)
Array
(
[count] => 1
[0] => Array
(
[distinguishedname] => Array
(
[count] => 1
[0] => CN=Beheergroep,OU=Test,DC=stefan,DC=nl
)
[0] => distinguishedname
[count] => 1
[dn] => CN=Beheergroep,OU=Test,DC=stefan,DC=nl
)
)
Array
(
[count] => 1
[0] => Array
(
[distinguishedname] => Array
(
[count] => 1
[0] => CN=Belangrijke Groep,OU=Test,DC=stefan,DC=nl
)
[0] => distinguishedname
[count] => 1
[dn] => CN=Belangrijke Groep,OU=Test,DC=stefan,DC=nl
)
)
Array
(
[count] => 1
[0] => Array
(
[distinguishedname] => Array
(
[count] => 1
[0] => CN=Hoofdgroep,OU=Test,DC=stefan,DC=nl
)
[0] => distinguishedname
[count] => 1
[dn] => CN=Hoofdgroep,OU=Test,DC=stefan,DC=nl
)
)
Array
(
[count] => 1
[0] => Array
(
[distinguishedname] => Array
(
[count] => 1
[0] => CN=Mailgroep,OU=Test,DC=stefan,DC=nl
)
[0] => distinguishedname
[count] => 1
[dn] => CN=Mailgroep,OU=Test,DC=stefan,DC=nl
)
)
Array
(
[count] => 1
[0] => Array
(
[distinguishedname] => Array
(
[count] => 1
[0] => CN=Testgroep2,OU=Test,DC=stefan,DC=nl
)
[0] => distinguishedname
[count] => 1
[dn] => CN=Testgroep2,OU=Test,DC=stefan,DC=nl
)
)
The question is, how do I get all the CN="GroupName" values from all the arrays in a list or something? It only needs to grab that value from every single array and display it in a list.
For example:
I only want this value from every array.
And the output should be like this:
Administratie
Anderemailgroep
Beheergroep
Belangrijke Groep
Hoofdgroep
Mailgroep
Testgroep2
EDIT
The array is coming from this piece of code:
$result = $adldap->user()->groups('test.user');
for ($i=0;$i<count($result);$i++) {
sort($result);
}
print_r($result);
foreach ($result as $key => $value) {
$check = $adldap->group()->info($value, array(
'distinguishedname'
));
if (strpos($check[0]['distinguishedname'][0], 'OU=Test') !== false) {
unset($result[$key]);
print_r($check);
}
}
Lets say your array is $arrs which contains of your array values given.
You can do like this.
<?php
foreach($arrs as $arr) {
foreach($arr as $ar) {
$sep = explode(',',$ar['dn']);
echo explode('=',$sep[0])[1];
}
}
?>
I have an array,
$arr=(
[0] => Array
(
[groupid] => 1
[groupname] => Red
[members] => Array
(
[0] => Array
(
[mid] => 9
[name] => Anith
)
[1] => Array
(
[mid] => 11
[name] => Aravind
)
[2] => Array
(
[mid] => 10
[name] => Lekshmi
)
)
)
[1] => Array
(
[groupid] => 2
[groupname] => Blue
[members] => Array
(
[0] => Array
(
[mid] => 6
[name] => Yamuna
)
[1] => Array
(
[mid] => 2
[name] => Kamala K
)
[2] => Array
(
[mid] => 13
[name] => Sooraj K
)
)
)
I want to check [mid] => 2 is in the array..If it exists
I want to delete it(ie. unset the array )-----
[1] => Array
(
[mid] => 2
[name] => Kamala K
)
;;;
eg:--unset($arr[1]['members'][2];
This should do the trick
foreach ($arr as $group => $subarray) {
foreach ($subarray['members'] as $k => $v) {
if ($v['mid'] == 2) {
unset($arr[$group]['members'][$k]);
break;
}
}
}
var_dump($arr);
If you feel like getting crafty, you could do something like this:
// note: requires PHP >= 5.3
foreach ($arr as $key => &$value) {
$value['members'] = array_filter(
$value['members'],
function($member) {
return $member['mid'] != 2;
}
);
}
var_dump($arr);
I have this array lets call it array 1
Array
(
[0] => Array
(
[Machine] => Array
(
[id] => 7
[name] => XYZ
[priority] => 1
)
[Software] => Array
(
[id] => 472
)
)
[1] => Array
(
[Machine] => Array
(
[id] => 6
[name] => ABC
[priority] => 0
)
[Software] => Array
(
[id] => 470
)
)
[2] => Array
(
[Machine] => Array
(
[id] => 1
[name] => IEU
[priority] => 3
)
[Software] => Array
(
[id] => 471
)
)
)
Then I have another array lets call it array 2
Array
(
[0] => 7
[1] => 5
[2] => 4
[3] => 3
[4] => 6
)
If array 2 doesnt have [Machine][id] then I want it to be removed from array 1. Like in above example 1 will removed
[2] => Array
(
[Machine] => Array
(
[id] => 1
[name] => IEU
[priority] => 3
)
[Software] => Array
(
[id] => 471
)
)
any idea on how to achieve that. Thanks
Perhaps..
foreach ($array1 AS $key => $array) {
if (!in_array($array['Machine']['id'], $array2))
unset($array1[$key]);
}
try something like :
$new_array = array();
foreach ($array1 as $platform)
{
if (in_array($platform["Machine"]["id"], $array2))
{
$new_array[] = $platform;
}
}
return $new_array;