I have the following stdClass Object contained within $response:
stdClass Object ( [domain] => stdClass Object ( [id] => d1111111 [spamscore] => 75 [rejectscore] => 200 ) [domainalias] => Array ( ) [wildcard] => Array ( ) [catchall] => Array ( ) [forward] => Array ( ) [mailbox] => Array (
[0] => stdClass Object ( [highEmailNotification] => [id] => m1111111 [lastPasswordChange] => 2020-02-19T22:41:12+00:00 [local] => mailbox1 [lowEmailNotification] => [quotaMB] => 10240 [receive] => 1 [rejectscore] => [send] => 1 [spamscore] => [usageMB] => 0 [enabled] => 1 )
[1] => stdClass Object ( [highEmailNotification] => [id] => m2222222 [lastPasswordChange] => 2020-02-17T15:46:21+00:00 [local] => mailbox2 [lowEmailNotification] => [quotaMB] => 10240 [receive] => 1 [rejectscore] => [send] => 1 [spamscore] => [usageMB] => 0 [enabled] => 1 )
[2] => stdClass Object ( [highEmailNotification] => [id] => m3333333 [lastPasswordChange] => 2020-02-19T15:00:36+00:00 [local] => mailbox3 [lowEmailNotification] => [quotaMB] => 1024 [receive] => 1 [rejectscore] => 0 [send] => 1 [spamscore] => 75 [usageMB] => 0 [enabled] => 1 ) ) [spamblacklist] => Array ( ) [spamwhitelist] => Array ( ) [responder] => Array ( ) [name] => domain.com )
I need to convert it into an array and extract particular values, i.e. [id] and [local] from it.
Speed is also an issue, as this array will grow to thousands of items, so if there is other, quicker way than 'foreach' it would be better.
I used some suggestions from here, such as:
$array = json_decode(json_encode($response), True);
foreach ($array as $var)
{
echo $var['id'] . ' - ' . $var['local'] . "<br>";
}
and got partial success with the results:
d1111111 -
-
-
-
-
i - i
(so it found the very first [id] value)
it however missed the most important values I am after.
What I need to get is:
m1111111 - mailbox1
m2222222 - mailbox2
m3333333 - mailbox3
Any suggestions are greatly appreciated.
The values you're trying to print is in a nested array within $response.
Try this.
$array = json_decode(json_encode($response['mailbox']), True);
foreach ($array as $var)
{
echo $var['id'] . ' - ' . $var['local'] . "<br>";
}
Related
-I cannot figure out how to regroup this array accourding to ids.
This array comes from a mysql table. the number 17 and 20 are foreign keys in the table.
$orginal_arr =
Array
(
[0] => Array
(
[id] => 17
[content] => string...?
)
[1] => Array
(
[id] => 20
[content] => hello
)
[2] => Array
(
[id] => 20
[content] => string...?
)
[3] => Array
(
[id] => 20
[content] => string...string...??
)
[4] => Array
(
[id] => 17
[content] => string...
)
[5] => Array
(
[id] => 17
[content] => string...
)
);
I want to convert the above $orginal_arr array to the following $desired_arr array.
$desired_arr = Array (
[0] => Array
(
[17] => Array
(
[0] => string...?
)
)
[1] => Array
(
[20] => Array
(
[0] => hello
[1] => string...?
[2] => string... string...??
)
)
[2] => Array
(
[17] => Array
(
[0] => string...
[1] => string...
)
)
);
so far I am trying the following approach:
function group_by_key($key, $data)
{
$result = array();
$j = 0;
foreach ($data as $val) {
if (array_key_exists($key, $val)) {
$result[$val[$key]][$j] = $val;
} else {
$result[""][] = $val;
}
$j++;
}
return $result;
}
$desired_arr = group_by_key("id", $orginal_arr);
$desired_arr2 = array();
foreach ($desired_arr as $index => $item) {
$desired_arr2 += $item;
}
echo "<pre>";
print_r($desired_arr2);
echo "</pre>";
the output:
Array
(
[0] => Array
(
[id] => 17
[content] => string...?
)
[4] => Array
(
[id] => 17
[content] => string...
)
[5] => Array
(
[id] => 17
[content] => string...
)
[1] => Array
(
[id] => 20
[content] => hello
)
[2] => Array
(
[id] => 20
[content] => string...?
)
[3] => Array
(
[id] => 20
[content] => string...string...??
)
)
- Thank you very much.
As I said in my comment, I'm not familiar to PHP syntax anymore so this will be pseudo code.
I would do it in 2 steps,
first I want this result
$temp_arr =
Array
(
[0] => Array
(
[id] => 17
[contents] => Array(
[0] => string...?
)
)
[1] => Array
(
[id] => 20
[contents] => Array(
[0] => hello
[1] => string...?
[2] => string...string...??
)
)
...
...
);
See, I kept the Object structure with id and contents (with a final S).
then it will be easier to check the id as it is directly accessible
$temp_array = Array();
foreach ($original_array as $element) {
$last = end($temp_array);
if ($last && $element->id == $last->id) {
// add to the last $temp_array element
$temp_array[count($temp_array)-1]->contents[] = $element->content;
}
else {
// create a new $temp_array element
$temp_array[] = Array(
"id" => $element->id,
"contents => Array(
$element->content
)
);
}
}
For the second step you just need to iterate the $temp_array and format as you like
here is another solution to this problem.
$desired_arr = array();
$prev_id = '';
$prev_key = null;
foreach ($orginal_arr as $key => $value) {
$this_id = $value['id'];
if ($this_id == $prev_id) {
array_push($desired_arr[$prev_key][$value['id']], $value['content']);
} else {
$desired_arr[$key][$value['id']] = (array)$value['content'];
$prev_key = $key;
}
$prev_id = $this_id;
}
$desired_arr = array_values($desired_arr);
After printing my $my_values['outer_group']['fieldset'] I'm getting the following output as per the data:
Array
(
[fieldset] => Array
(
[1] => Array
(
[title] => Dummy_Value1
[inner_group] => Array
(
[fieldset] => Array
(
[1] => Array
(
[id] => 11
[title] => Dummy_Value11
)
[2] => Array
(
[id] => 12
[title] => Dummy_Value12
)
[3] => Array
(
[id] => 13
[title] => Dummy_Value13
)
[actions] => Array
(
[add] => Add InnerGroup
[remove] => Remove InnerGroup
)
)
)
)
[2] => Array
(
[title] => Dummy_Value2
[inner_group] => Array
(
[fieldset] => Array
(
[1] => Array
(
[id] => 21
[title] => Dummy_Value21
)
[actions] => Array
(
[add] => Add InnerGroup
)
)
)
)
[actions] => Array
(
[add] => Add OuterGroup
[remove] => Remove OuterGroup
)
)
)
My requirement is to re-index the output data, hence I've performed the following code to re-index the same:
<?php
if (isset($my_values['outer_group']) && !empty($my_values['outer_group'])) {
$outer_types = $my_values['outer_group']['fieldset'];
$inner = [];
foreach ($outer_types as $outer_key => $outer_value) {
if (is_numeric($outer_key)) {
if (isset($outer_value['inner_group']['fieldset'])) {
foreach ($outer_value['inner_group']['fieldset'] as $k => $v) {
if (is_numeric($k)) {
$inner[] = [
'id' => $v['id'],
'title' => !empty($v['title']) ? $token->replace($v['title']) : NULL,
];
}
}
}
$my_values['outer'][$outer_key] = [
'title' => !empty($outer_value['title']) ? $token->replace($outer_value['title']) : NULL,
'inner' => $inner,
];
}
}
}
As per the output its getting re-indexed but with some errors in data. I'm getting trouble while populating the [inner] data, following is the output for the same:
Array
(
[0] => Array
(
[title] => Dummy_Value1
[inner] => Array
(
[0] => Array
(
[id] => 11
[title] => Dummy_Value11
)
[1] => Array
(
[id] => 12
[title] => Dummy_Value12
)
[2] => Array
(
[id] => 13
[title] => Dummy_Value13
)
)
)
[1] => Array
(
[title] => Dummy_Value2
[inner] => Array
(
[0] => Array
(
[id] => 11
[title] => Dummy_Value11
)
[1] => Array
(
[id] => 12
[title] => Dummy_Value12
)
[2] => Array
(
[id] => 13
[title] => Dummy_Value13
)
[3] => Array
(
[id] => 21
[title] => Dummy_Value21
)
)
)
)
Whereas, it should be:
Array
(
[0] => Array
(
[title] => Dummy_Value1
[inner] => Array
(
[0] => Array
(
[id] => 11
[title] => Dummy_Value11
)
[1] => Array
(
[id] => 12
[title] => Dummy_Value12
)
[2] => Array
(
[id] => 13
[title] => Dummy_Value13
)
)
)
[1] => Array
(
[title] => Dummy_Value2
[inner] => Array
(
[0] => Array
(
[id] => 21
[title] => Dummy_Value21
)
)
)
)
$inner = []; needs to be within the foreach loop so that it is empty before building each internal element.
Untested - but as follows:
<?php
if (isset($my_values['outer_group']) && !empty($my_values['outer_group'])) {
$outer_types = $my_values['outer_group']['fieldset'];
foreach ($outer_types as $outer_key => $outer_value) {
$inner = [];
if (is_numeric($outer_key)) {
if (isset($outer_value['inner_group']['fieldset'])) {
foreach ($outer_value['inner_group']['fieldset'] as $k => $v) {
if (is_numeric($k)) {
$inner[] = [
'id' => $v['id'],
'title' => !empty($v['title']) ? $token->replace($v['title']) : NULL,
];
}
}
}
$my_values['outer'][$outer_key] = [
'title' => !empty($outer_value['title']) ? $token->replace($outer_value['title']) : NULL,
'inner' => $inner,
];
}
}
}
I am trying to output a username and user email outside of a foreach loop. I am trying to send an email to all WordPress users that are within a certain User Role.
Here is my code:
// Get users and their roles
$user_args = array(
'role__in' => 'new_role',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$users = get_users($user_args);
foreach ( $users as $user ) :
$user_name = $user->user_email;
$user_email = $user->display_name;
endforeach;
So I can then use them in other areas of the page, ie:.
echo 'Hello, ' . $user_name;
echo 'Send to: ' .$user_email;
I've printed out the $users array which contains the following:
Array
(
[0] => WP_User Object
(
[data] => stdClass Object
(
[ID] => 46
[user_login] => huw
[user_pass] => u7CqxbuQDvApUAF6tT.
[user_nicename] => huw
[user_email] => huw#xxxx.co.uk
[user_url] =>
[user_registered] => 2017-02-06 11:13:09
[user_activation_key] => 1486379590:$P$BkisA4T5j1S/ZjRageafNYHfsdin1S0
[user_status] => 0
[display_name] => Huw Daniel Rowlands
)
[ID] => 46
[caps] => Array
(
[sssg] => 1
[new_role] => 1
[site_member] => 1
[test_role] => 1
)
[cap_key] => jciw_capabilities
[roles] => Array
(
[0] => sssg
[1] => new_role
[2] => site_member
[3] => test_role
)
[allcaps] => Array
(
[read] => 1
[sssg] => 1
[new_role] => 1
[site_member] => 1
[test_role] => 1
)
[filter] =>
)
[1] => WP_User Object
(
[data] => stdClass Object
(
[ID] => 308
[user_login] => jeremy
[user_pass] => LLOKbkPOWQsBKUIk2qL1
[user_nicename] => magnus
[user_email] => jeremy#gmail.com
[user_url] =>
[user_registered] => 2017-05-03 19:24:42
[user_activation_key] => 1493839482:$P$BI/IYldCzsXZowLEiNfxiUkIwVdDKV0
[user_status] => 0
[display_name] => Jeremy
)
[ID] => 308
[caps] => Array
(
[new_role] => 1
[sssg] => 1
[site_member] => 1
)
[cap_key] => jciw_capabilities
[roles] => Array
(
[0] => new_role
[1] => sssg
[2] => site_member
)
[allcaps] => Array
(
[read] => 1
[new_role] => 1
[sssg] => 1
[site_member] => 1
)
[filter] =>
)
)
If you want to send an email to all users with a certain role, try something like this:
// Get users and their roles
$user_args = array(
'role__in' => 'new_role',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$users = get_users($user_args);
foreach ( $users as $user ) :
$user_email = $user->user_email;
$user_name = $user->display_name;
send_email($user_name, $user_email);
endforeach;
And then write your email function:
function send_email($name, $email) {
// Do email sending stuff here with $name & $email
}
How do I scoop just the '$reference_cart_id' variable from this session_cart_array so i can query db for other matching id's that belong to this customer? When i loop back through an undefined error comes up for reference_id & reference_cart_id.
cartOutput --Array
(
[0] => Array
(
[item_id] => 133
[quantity] => 1
[size] => MED
[color] => Black
[username] => medallion9
[reference_cart_id] => DU3ZWRjNTkwNGYy
[reference_id] => 6cefe27
)
[1] => Array
(
[item_id] => 134
[quantity] => 1
[size] => LG
[color] => Camel-501
[username] => medallion9
[reference_cart_id] => DU3ZWRjNTkwNGYy
[reference_id] => 6cefe27
)
)
You can use array_map (http://php.net/manual/en/function.array-map.php):
$idArray = array_map(function($a){ return $a['reference_cart_id']; },$cartArray);
I have this array result and want to sort based on name
$arr = array
(0 => array
(
(name) => Medianas empresas
(count) => 17
[applied] =>
[url] => /es/index.php?option=com_virtuemart&view=category&Itemid=230&usage=Medianas+empresas
)
1 => array
(
[name] => Grandes empresas y gobierno
[count] => 8
[applied] =>
[url] => /es/index.php?option=com_virtuemart&view=category&Itemid=230&usage=Grandes+empresas+y+gobierno
)
2 => array
(
[name] => Microempresas
[count] => 9
[applied] =>
[url] => /es/index.php?option=com_virtuemart&view=category&Itemid=230&usage=Microempresas
)
)
The result should be like,
$arr = array
(
1 => array
(
[name] => Grandes empresas y gobierno
[count] => 8
[applied] =>
[url] => /es/index.php?option=com_virtuemart&view=category&Itemid=230&usage=Grandes+empresas+y+gobierno
)
0 => array
(
(name) => Medianas empresas
(count) => 17
[applied] =>
[url] => /es/index.php?option=com_virtuemart&view=category&Itemid=230&usage=Medianas+empresas
)
2 => array
(
[name] => Microempresas
[count] => 9
[applied] =>
[url] => /es/index.php?option=com_virtuemart&view=category&Itemid=230&usage=Microempresas
)
)
The desired result is in fact identical to the one you have, so, there is nothing to do.
If you confounded indices 0 and 1 in your desired result, just
sort($arr);
would do the job.