I have the following multidimensional array:
Array
(
[0] => Array
(
)
[1] => Array
(
[0] => Array
(
[id] => 74
[RecordGUID] => 9BD28E1E-99EB-D4E7-CC2C-AB6F5905BCDA
[Type] => DENTAL
[App_Service] => a:4:{i:151;s:6:"AAMCAS";i:152;s:3:"DDS";i:154;s:11:"APP SVC TWO";i:155;s:6:"AADSAS";}
)
[1] => Array
(
[id] => 73
[RecordGUID] => A5146CFF-5D17-FB1A-D831-E6835D0A04DD
[Type] => MED
[App_Service] => a:1:{i:151;s:6:"AAMCAS";}
)
[2] => Array
(
[id] => 75
[RecordGUID] => 0C253109-07E7-151A-0277-19EAC025C2E6
[Type] => PHYSICAL THERAPY
[App_Service] => a:1:{i:153;s:8:"PHYSTHER";}
)
)
[2] => Array
(
[0] => Array
(
[id] => 155
[RecordGUID] => 5DF76F3E-2F0C-FD63-B58F-027A61E9BC11
[AppService] => AADSAS
[AppService_types] =>
)
[1] => Array
(
[id] => 151
[RecordGUID] => 3B503CFC-AB80-C06B-C4C4-8EE548FFC7BF
[AppService] => AAMCAS
[AppService_types] =>
)
[2] => Array
(
[id] => 154
[RecordGUID] => 753D95F2-6733-AE27-8F2E-48685DC796C0
[AppService] => APP SVC TWO
[AppService_types] =>
)
[3] => Array
(
[id] => 152
[RecordGUID] => 0D3C9435-64DD-9079-C0F4-D543DFFA0E10
[AppService] => DDS
[AppService_types] =>
)
[4] => Array
(
[id] => 153
[RecordGUID] => 0D196967-21AF-ADDA-920E-F12938DACADB
[AppService] => PHYSTHER
[AppService_types] =>
)
)
)
I want to find the Type key that equals MED and then grab the App_Service value right below it.
I'm a bit stuck. Any help would be greatly appreciated. Thanks in advance.
That is not multi-dimensional, it is a nested structure. Arrays are associative i.e. maps in PHP.
To find that value you can do the following (where $x is your array)
foreach($x as $entry) {
foreach($entry as $subentry) {
if ($subentry['Type'] == "MED") {
echo $subentry['App_service'];
}
}
}
Try this out:
foreach($MDArray as $array) {
if(array_key_exists("Type",$array) {
foreach($array as $k => $v) {
if($k == "MED") {
return $array['AppService'];
}
}
}
}
Replace $MDArray with the name of your multi-dimensional array.
If your data can have several arrays with type MED, try the following recursive function:
function findByType($array, $val, &$result) {
foreach ($array AS $k => $v) {
if (is_array($v)) {
if (isset($v['Type']) && $v['Type'] == $val) {
$result[] = $v['App_Service'];
} else {
findByType($v, $val, $result);
}
}
}
}
$result = array();
findByType($data, 'MED', $result);
var_dump($result);
Something simple usually gets the job done.
foreach( $array[1] as $find )
{
if( array_search( 'MED', $find ) )
{
echo $find['App_Service'];
}
}
Or, if you prefer a recursive function to check through the entire array:
function findRecursive( $type, $array )
{
foreach( $array as &$section )
{
if( is_array( $section ) )
{
if( in_array( $type, $section ) )
{
return $section['App_Service'];
}
elseif( ( $test = findRecursive( $type, $section ) ) !== FALSE )
{
return $test;
}
}
}
return false;
}
The function would be useful to reuse for different types and if your array may not have the same format.
Edit
Fixed my function. Doesn't require array_search anyway.
Related
I have a multidimensional associative array which has a set of array. I want to change my array index value from some array value.
I already tried some array functions but my array also contains some null array so laravel function keyBy not give me wanted result.
$arr1=array(0 =>array(),1=>array(0=>array('quan'=>10,'handle' => 'baroque'),1 =>array('quan'=>20,'handle' => 'baroque')),
2 =>array (0 =>array('quan' => 5,'handle' => 'adidas')));
My expected result array must be like this
$arr2=array(0 =>array(),'baroque'=>array(0=>array('quan'=>10,'handle' => 'baroque'),1 =>array('quan'=>20,'handle' => 'baroque')),
'adidas' =>array (0 =>array('quan' => 5,'handle' => 'adidas')));
You can use the classic foreach. Check if the handle on element 0 exists using isset, if it does, use that as the key.
$arr1 = //...
$result = array();
foreach($arr1 as $key => $val) {
if (is_array($val) && isset($val[0]["handle"])) $result[ $val[0]["handle"] ] = $val;
else $result[$key] = $val;
}
$result will be:
Array
(
[0] => Array
(
)
[baroque] => Array
(
[0] => Array
(
[quan] => 10
[handle] => baroque
)
[1] => Array
(
[quan] => 20
[handle] => baroque
)
)
[adidas] => Array
(
[0] => Array
(
[quan] => 5
[handle] => adidas
)
)
)
You can use without condition by grouping at the handle as key directly.
$result = [];
foreach ($arr as $key => $value) {
if (!empty($value)) {
foreach ($value as $key1 => $value1) {
$result[$value1['handle']][] = $value1;
}
} else {
$result[] = $value;
}
}
Demo
Output:-
Array
(
[0] => Array
(
)
[baroque] => Array
(
[0] => Array
(
[quan] => 10
[handle] => baroque
)
[1] => Array
(
[quan] => 20
[handle] => baroque
)
)
[adidas] => Array
(
[0] => Array
(
[quan] => 5
[handle] => adidas
)
)
)
Try this..
$res = [];
foreach($x as $key => $value)
{
if(empty($value))
{
$res[] = $value;
}
else
{
foreach($value as $v => $k)
{
if(array_key_exists($k['handle'],$res))
{
$res[$k['handle']][] = ['quan' => $k['quan'],'handle' => $k['handle']];
}
else
{
$res[$k['handle']][0] = ['quan' => $k['quan'],'handle' => $k['handle']];
}
}
}
}
The result is going to be like this.
Array
(
[0] => Array
(
)
[baroque] => Array
(
[0] => Array
(
[quan] => 10
[handle] => baroque
)
[1] => Array
(
[quan] => 20
[handle] => baroque
)
)
[adidas] => Array
(
[0] => Array
(
[quan] => 5
[handle] => adidas
)
)
)
I have this array:
Array
(
[8008] => Array
(
[main_mob_vnum] => 8008
[0] => Array
(
[drop_mob_vnum] => 50300
[drop_mob_count] => 2
[drop_mob_percent] => 110.00
)
[1] => Array
(
[drop_mob_vnum] => 39030
[drop_mob_count] => 2
[drop_mob_percent] => 85.00
)
)
[8009] => Array
(
[main_mob_vnum] => 8009
[0] => Array
(
[drop_mob_vnum] => 50300
[drop_mob_count] => 4
[drop_mob_percent] => 110.00
)
[1] => Array
(
[drop_mob_vnum] => 50125
[drop_mob_count] => 1
[drop_mob_percent] => 75.00
)
)
[8010] => Array
(
[main_mob_vnum] => 8009
[0] => Array
(
[drop_mob_vnum] => 50125
[drop_mob_count] => 4
[drop_mob_percent] => 110.00
)
)
)
I want to make a function like
function searchArray($array,$vnum)
if $vnum is == with drop_mob_vnum take the previously array and put all this array keys on new array
For example, searchArray($array,50300) should return an array like: array(8008,8009) because 8010 doesn't have a drop_mob_vnum == 50300
I know StackOverflow requires that I post my problem with code, but I don't know where to start. If someone could help me, I would appreciate it. Thank you!
This function should do the trick:
function find_vnum($array,$value)
{
$result = array();
foreach ($array AS $k1 => $v1)
{
foreach ($v1 AS $k2 => $v2)
{
if ($k2 === 'main_mob_vnum') {
continue;
}
foreach ($v2 AS $k3 => $v3)
{
if ($k3 !== 'drop_mob_vnum') {
continue;
}
if (($v3 === $value) && !in_array($k1, $result)) {
$result[] = $k1;
}
}
}
}
return $result;
}
Given your sample data:
print_r(find_vnum($data,50300));
// Array
// (
// [0] => 8008
// [1] => 8009
// )
You can see a working demo here.
I'm hoping there is a better way of returning the values from each of cy_GB['value] and en_GB['value] from the array below:
MultilingualSelectAttributeTypeOptionList Object (
[options:MultilingualSelectAttributeTypeOptionList:private] => Array
(
[0] => MultilingualSelectAttributeTypeOption Object
(
[error] =>
[id] => 7
[values] => Array
(
[cy_GB] => Array
(
[id] => 13
[value] => Audio described
)
[en_GB] => Array
(
[id] => 14
[value] => Audio described
)
)
[th] => TextHelper Object
(
)
)
[1] => MultilingualSelectAttributeTypeOption Object
(
[error] =>
[id] => 3
[values] => Array
(
[cy_GB] => Array
(
[id] => 5
[value] => BSL signed
)
[en_GB] => Array
(
[id] => 6
[value] => BSL signed
)
)
[th] => TextHelper Object
(
)
)
)
[error] =>
)
This is what I've tried. I should also use more meaningful names.:
foreach ($selectedOptions as $row) {
foreach ($row as $key) {
foreach ($key as $k => $v) {
if($k == 'cy_GB') {
echo $v['value'];
}
if($k == 'en_GB') {
echo $v['value'];
}
}
}
}
I know this kind of thing has been asked many times before so I apologise for that. any help would be most appreciated.
Something like this could work:
function findKeyRec($obj, $search) {
if( !is_array( $obj ) && !$obj instanceof Traversable ) return;
foreach($obj as $key => $value) {
if($key == $search) {
echo $value['value'];
} else {
findKeyRec($value, $search);
}
}
}
findKeyRec($ar, 'cy_GB');
findKeyRec($ar, 'en_GB');
It's not shorter, but in my opinion more elegant, and it should work with any object/array structure.
Untested.
i have an array $mainArray of arrays and i would like to remove / undset the arrays that vave keys with no value.
here is my array:
Array
(
[0] => Array
(
[msg_id] => 203
[comment] => Array
(
[0] => Array
(
[com_id] =>
)
)
)
[1] => Array
(
[msg_id] => 202
[comment] => Array
(
[0] => Array
(
[com_id] => 196
)
[1] => Array
(
[com_id] => 197
)
[2] => Array
(
[com_id] =>
)
)
)
[2] => Array
(
[msg_id] => 201
[comment] => Array
(
[0] => Array
(
[com_id] => 198
)
[1] => Array
(
[com_id] =>
)
)
)
)
In this case i would like to look inside comment array arrays and see if there are any of them that have empty values. The best case scenario would be to remove the comment array entirely if all sub arrays are empty.
nut im of with leaving the comment hey there just null
this array should become:
Array
(
[0] => Array
(
[msg_id] => 203
)
[1] => Array
(
[msg_id] => 202
[comment] => Array
(
[0] => Array
(
[com_id] => 196
)
[1] => Array
(
[com_id] => 197
)
)
)
[2] => Array
(
[msg_id] => 201
)
)
any ideas on how to proceed?
thanks.
array_filter() is what you're after. Particularly a recursive version. The following was taken from a comment on the PHP Doc:.
function array_filter_recursive($array, $callback = null) {
foreach ($array as $key => & $value) {
if (is_array($value)) {
$value = array_filter_recursive($value, $callback);
}
else {
if ( ! is_null($callback)) {
if ( ! $callback($value)) {
unset($array[$key]);
}
}
else {
if ( ! (bool) $value) {
unset($array[$key]);
}
}
}
}
unset($value);
return $array;
}
Use php's unset() to unset any array key/value.
More info on this link http://in3.php.net/unset
in your case the code can be, (i have not tested it. but let me know if you have any problem and i can fix it)
function unsetCommentFromArray($mainArray) {
foreach($mainArray as $key => $value) {
foreach($value['comment'] as $k => $v) {
if(empty($v['com_id'])) {
unset($mainArray[$key]['comment'][$k]);
}
}
}
return $mainArray;
}
$array = array_map(function ($i) {
$i['comment'] = array_filter($i['comment'], function ($c) { return $c['com_id']; });
return array_filter($i);
}, $array);
Requires PHP 5.3 or higher.
I have a multidimensional array that could be any size or depth. I'm basically trying replace empty values with a value but only in certain cases. Here is an example of the array, it's quite large but I want to illustrate my point well:
[field_ter] =>
[field_title] => Array
(
[0] => Array
(
[value] =>
)
)
[field_firstnames] => Array
(
[0] => Array
(
[value] => test9
)
)
[field_birth] => Array
(
[0] => Array
(
[value] =>
)
)
[field_postal] => Array
(
[0] => Array
(
[value] =>
)
)
[group_certificates] => Array
(
[0] => Array
(
[_delta] => 0
[field_cert_details] => Array
(
[value] =>
)
[field_cert_issuedate] => Array
(
[value] => Array
(
[date] =>
)
)
[field_cert_expiry] => Array
(
[value] => Array
(
[date] =>
)
)
[field_cert_issue_country] => Array
(
[value] =>
)
[field_cert_limits] => Array
(
[value] =>
)
)
[1] => Array
(
[_delta] => 1
[field_cert_details] => Array
(
[value] =>
)
[field_cert_issuedate] => Array
(
[value] => Array
(
[date] =>
)
)
[field_cert_expiry] => Array
(
[value] => Array
(
[date] =>
)
)
[field_cert_issue_country] => Array
(
[value] =>
)
[field_cert_limits] => Array
(
[value] =>
)
)
)
What I'm trying to do is find any element in the array that is empty, then replace the empty value with a value. I have an array of exceptions where the empty element is not replaced. This is the function I'm working on at the moment:
function check_empty(&$array) {
$exceptions = array('changed', 'form_build_id','date', 'status', 'op');
// This is the array we will return at the end of the function
$new_array = array();
foreach($array as $key => $value) {
if(is_array($value)) {
check_empty($value);
}
elseif ($value == '' && !in_array($key, $exceptions)) {
$new_array[$key] = '$$$';
}
else {
$new_array[$key] = $value;
}
}
return $new_array;
}
Could someone help me tweak my function or point me in the direction of a better way? I tried array_walk_recursive but it doesn't work with my arrays.
You need to assign the return value of the recursive check_empty:
function check_empty($array) {
$exceptions = array('changed', 'form_build_id','date', 'status', 'op');
// This is the array we will return at the end of the function
$new_array = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$new_array[$key] = check_empty($value);
}
elseif ($value == '' && !in_array($key, $exceptions)) {
$new_array[$key] = '$$$';
}
else {
$new_array[$key] = $value;
}
}
return $new_array;
}
if(is_array($value)) {
check_empty($value);
}
Here you don't return resulting value to anywhere. Probably that's the problem