Returning values from a multidimensional array php - php

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.

Related

Remove duplicates from an object array

I have an object array that looks like this:
Array
(
[0] =>Object
(
[ClassScheduleID] => 2263
[Name] => Workout 1
[Location] => Object
(
[BusinessID] => 1
)
)
[1] =>Object
(
[ClassScheduleID] => 2263
[Name] => Workout 1
[Location] => Object
(
[BusinessID] => 13
)
)
[2] =>Object
(
[ClassScheduleID] => 2264
[Name] => Workout 2
[Location] => Object
(
[BusinessID] => 22
)
)
I am looking to identify that the ClassScheduleID of 2263 is a duplicate, and remove the duplicate entry's entire object from the array. So that I get:
Array
(
[0] =>Object
(
[ClassScheduleID] => 2263
[Name] => Workout 1
[Location] => Object
(
[BusinessID] => 1
)
)
[1] =>Object
(
[ClassScheduleID] => 2264
[Name] => Workout 2
[Location] => Object
(
[BusinessID] => 22
)
)
I tried the solution proposed here
How to remove duplicate values from a multi-dimensional array in PHP
but the count() remained the same
$count = 0;
foreach($arrayObj as $key => $value) {
if ($value['ClassScheduleID'] == 2263) {
$count++;
}
if ($count > 1){
unset($arrayObj[$key]);
$count--;
}
}
It works: http://ideone.com/fork/zrdDtu
Edit: Modified to delete any duplicates:
foreach($arrayObj as $key => $value) {
$count = 0;
foreach($arrayObj as $nkey => $nvalue) {
if ($value['ClassScheduleID'] == $nvalue['ClassScheduleID']) {
$count++;
}
if ($count > 1){
unset($arrayObj[$key]);
$count--;
}
}
}
var_dump($arrayObj);
See it here: http://ideone.com/fork/85RCst
function get_unique_array($array)
{
$result = array_map("unserialize", array_unique(array_map("serialize", $array)));
foreach ($result as $key => $value)
{
if ( is_array($value) )
{
$result[$key] = get_unique_array($value);
}
}
return $result;
}
Demo: http://3v4l.org/WOTHi#v430

Looping through multidimensional array in php

Please refer to array below. What I would like to do is to get the total of the values of index zero under 21 and 23 divided by the number of zeros. It is like getting their average.
Array
(
[21] => Array
(
[0] => 3.5
[65] => Array
(
[0] => 44.125
)
[150] => Array
(
[0] => 15.25
)
[151] => Array
(
[0] => 17.333333333333
)
)
[23] => Array
(
[0] => 0
[166] => Array
(
[0] => 26
)
[172] => Array
(
[0] =>
)
[182] => Array
(
[0] => 20.333333333333
)
[183] => Array
(
[0] => 24.125
)
)
)
Then format it to this
Array
(
[21] => Array
(
[0] => Average for 21
[65] => Array
(
[0] => 44.125
)
[150] => Array
(
[0] => 15.25
)
[151] => Array
(
[0] => 17.333333333333
)
)
[23] => Array
(
[0] => Average for 23
[166] => Array
(
[0] => 26
)
[172] => Array
(
[0] =>
)
[182] => Array
(
[0] => 20.333333333333
)
[183] => Array
(
[0] => 24.125
)
)
)
Thanks in advance to those who can help! :)
Note: This is just a sample structure of the array. It's possible that the children of 21 and 23 can have another children, meaning, another nodes. Example:
[65] => Array
(
[0] => 44.125
[x] => Array
(
[0]=> 121.11
)
)
I believe a recursive function is needed on this one.
try this, I think this is what you want
$count1 = 0;$sum=0;
$count2 = 0;$sum2=0;
foreach ($array as $key3 => $value3)
{
if($key3 == "21")
{
foreach ($value3 as $key => $value)
{
if (strpos($key, '0') === 0) {
$count1++;$sum=$sum+$value;
}
if(is_array($value))
{
foreach($value as $key2=>$value2)
{
if(strpos($key2,'0') === 0)
{
$count1++ ;$sum=$sum+$value2;
}
}
}
}
}
if($key3 == "23")
{
foreach ($value3 as $key => $value)
{
if (strpos($key, '0') === 0) {
$count2++;$sum2=$sum2+$value;
}
if(is_array($value))
{
foreach($value as $key2=>$value2)
{
if(strpos($key2,'0') === 0)
{
$count2++; $sum2=$sum2+$value2;
}
}
}
}
}
}
$array[21][0] = ($sum/$count1);
$array[23][0] = ($sum2/$count2);
Demo

PHP find value in array

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.

how to unset empty arrays in php?

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.

Recursive PHP function that copies multidimensional array but replaces empty values

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

Categories