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);
Related
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();
So I got a multidimensional array like this, it is a result of some calculation:
Array
(
[0] => Array
(
[0] => 0.0415
[1] => 0.083
[2] => 0.083
)
[1] => Array
(
[0] => 0.0325
[1] => 0.041
[2] => 0.025
)
[1] => Array
(
[0] => 0.076
[1] => 0.005
[2] => 0.031
)
)
Is it possible to give a name of each index? The result that I expected is like this:
Array
(
[A1] => Array
(
[C1] => 0.0415
[C2] => 0.083
[C3] => 0.083
)
[A2] => Array
(
[C1] => 0.0325
[C2] => 0.041
[C3] => 0.025
)
[A3] => Array
(
[C1] => 0.076
[C2] => 0.005
[C3] => 0.031
)
)
Yes it is possible with foreach loop.
$b=array();
foreach ($a as $k => $v) {
$new_key='A'.$k;
$new_array= array();
foreach ($v as $k2 => $val) {
$kkk = 'C'.$k2;
$new_array[$kkk]=$val;
}
$b[$new_key]=$new_array;
}
I have an PHP array like this:
Array
(
[0] => Array
(
[option_id] => 21
[header_image] => logo.png
)
[1] => Array
(
[option_id] => 21
[menu1] => About
)
[2] => Array
(
[option_id] => 22
[menu2] => Speaker
)
[3] => Array
(
[option_id] => 22
[menu3] => Agenda
)
[4] => Array
(
[option_id] => 22
[menu4] => Venue
)
[5] => Array
(
[option_id] => 23
[menu5] => Hotel
)
[6] => Array
(
[option_id] => 23
[menu6] => Sponsors
)
)
I want array like this:
Array
(
[0] => Array
(
[option_id] => 21
[header_image] => logo.png
[menu1] => About
)
[1] => Array
(
[option_id] => 22
[menu2] => Speaker
[menu3] => Agenda
[menu4] => Venue
)
[2] => Array
(
[option_id] => 23
[menu5] => Hotel
[menu6] => Sponsors
)
)
I want to bind array having same option_id.
How can I achieve this?
You need to loop over the array with foreach
<?php
$output = array();
if (! empty($arr)) {
foreach ($arr as $elem) {
if (! empty($elem)) {
foreach ($elem as $k => $v) {
$output[$elem['option_id']][$k] = $v;
}
}
}
}
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()
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;