Why array value is giving 0? - php

I am reading a whole bunch of data from my users and adding them as associative arrays to be able to join the data. the following gives me the correct arrays values but then I get 0 as a result.
<?php
$userID = array();
$blogusers = get_users( 'orderby=nicename&role=author' );
foreach ( $blogusers as $user ) {
$gender = 'sesso';
$single = true;
$the_user_id = $user->ID;
array_push($userID, $the_user_id);
$myUsers = $user->user_description;
$user_sex = get_user_meta( $the_user_id, $gender, $single );
$users[$user->ID] = array( "description" => $myUsers , "gender" => $user_sex );
}
$roles = array();
foreach($users as $id => $values){
$temp_roles = explode(',', $values['description']);
foreach($temp_roles as $k => $v){
$roles[trim($v)][$values['gender']][] = true;
}
}
?>
<ul class="margin-top-20">
<?php
foreach($roles as $skill => $genderB) {
$males = count($genderB['male']);
$females = count($genderB['female']);
$total = $males + $females;
echo $females;
echo "<li>We have ".$total." ".$skill." teachers, ".$males." males, ".$females." females</li>";
}
?>
</ul>
I get 0 if I do echo $females; and the strings all get 0 as values
We have 0 francese teachers, 0 males, 0 females
if I do print_r($users[$user->ID] ); right at the end of the first foreach, i get the right result tho
Array ( [description] => francese, chimica, fisica, scienze [gender] => Array ( [0] => maschio ) ) Array ( [description] => inglese, fisica, chimica, spagnolo [gender] => Array ( [0] => maschio ) ) Array ( [description] => francese, fisica, italiano [gender] => Array ( [0] => femmina ) )
And if I do print_r($roles); at the end of the second foreach i get only the roles values but not the gender
Array ( [francese] => Array ( ) [chimica] => Array ( ) [fisica] => Array ( ) [scienze] => Array ( ) ) Array ( [francese] => Array ( ) [chimica] => Array ( ) [fisica] => Array ( ) [scienze] => Array ( ) [inglese] => Array ( ) [spagnolo] => Array ( ) ) Array ( [francese] => Array ( ) [chimica] => Array ( ) [fisica] => Array ( ) [scienze] => Array ( ) [inglese] => Array ( ) [spagnolo] => Array ( ) [italiano] => Array ( )
The again if I do print_r($temp_roles); at the end of the second foreach i get
Array ( [0] => francese [1] => chimica [2] => fisica [3] => scienze ) Array ( [0] => inglese [1] => fisica [2] => chimica [3] => spagnolo ) Array ( [0] => francese [1] => fisica [2] => italiano )
I don't understand why I am getting 0 values for all of them at the end

In your data the gender is defined by femmina and maschio. In your code you check for female and male.
The other problem I detected is that you do not have to have both male and female teachers. If you turn your error reporting on, with this code you will get
Undefined index: female (or male)
if there are no females. You can work arround it by $females = isset($genderB['female']) ? count(genderB['female']): 0;. Or by "full" if statement of course.

Related

how to take the value from the array if the value is separated by the semicolon

i have an array which have two values which are separated by the semicolon out of that i only want the the value which is after the semicolon.
Array ( [0] => animals;1 [1] => animals;2 [2] => animals;3 [3] => birds;1 [4] => birds;2 )
i am getting the output
Array ( [animals] => 1 )
Array ( [animals] => 2 )
Array ( [animals] => 3 )
Array ( [animals] => 3 [birds] => 1 )
Array ( [animals] => 3 [birds] => 2 )
i want the output
Array ( [animals] => 1 )
Array ( [animals] => 2 )
Array ( [animals] => 3 )
Array ( [birds] => 1 )
Array ( [birds] => 2 )
i have tried
$filter_param = array();
foreach ($animals as $options)
{
$exp_data = explode(";",$options);
//echo "<br>";
//print_r($exp_data);
$filter_string = '';
foreach ($exp_data as $dta)
{
$filter_string .= $dta[].',';
}
$filter_string = $exp_data[1];
$filter_string = rtrim($filter_string,",");
$filter_param[$exp_data[0]] = $filter_string;
echo "<br>";
print_r($filter_param);
}
$arr = Array ( 0 => "animals;1", 1 => "animals;2", 2 => "animals;3", 3 => "birds;1", 4 => "birds;2" );
foreach($arr as $key=>$row)
{
$tmparr = explode(";", $row);
$newArr[][$tmparr[0]] = $tmparr[1];
}
foreach($newArr as $new)
{
print_r($new);
echo "<br>";
}
Output
Array ( [animals] => 1 )
Array ( [animals] => 2 )
Array ( [animals] => 3 )
Array ( [birds] => 1 )
Array ( [birds] => 2 )
Demo: Click Here

array manipulation and rearranging

Here's my deal.
I have this array:
Array // called $data in my code
(
[0] => Array
(
[name] => quantity
[value] => 0
)
[1] => Array
(
[name] => var_id
[value] => 4
)
[2] => Array
(
[name] => quantity
[value] => 0
)
[3] => Array
(
[name] => var_id
[value] => 5
)
)
which I need it to be like:
Array // called $temp in my code
(
[0] => Array
(
[0] => Array
(
[name] => quantity
[value] => 0
)
[1] => Array
(
[name] => var_id
[value] => 4
)
)
[2] => Array
(
[0] => Array
(
[name] => quantity
[value] => 0
)
[1] => Array
(
[name] => var_id
[value] => 5
)
)
)
and I did it using this code I made:
$data = $_POST['data'];
$temp = array();
foreach($data as $key => $datum)
{
if($key%2 == 0)
{
$temp[$key] = array();
array_push($temp[$key], $datum, $data[$key+1]);
}
}
But I think that my code is some kinda stupid, specially if I have a huge data.
eventually what I want to do is just have each two indexes combined in one array, and I know that there should be something better than my code to do it, any suggestions?
Discover array_chunk()
$temp = array_chunk($data, 2);
$cnt = count($data);
$temp = array();
for ($i = 0; $i < $cnt; $i = $i + 2)
{
$temp[] = array($data[$i], $data[$i+1]);
}
Take a look at array_chunk.
<?php
$array = array(
array(1),
array(2),
array(3),
array(4),
);
print_r(
array_chunk($array, 2, false)
);
/*
Array
(
[0] => Array
(
[0] => Array
(
[0] => 1
)
[1] => Array
(
[0] => 2
)
)
[1] => Array
(
[0] => Array
(
[0] => 3
)
[1] => Array
(
[0] => 4
)
)
)
*/

compare two associative array and display the difference

I have the following Two array results coming from MySQL query result.
Array One (Orignal-Data):
Array
(
[w_a] => Array
(
[0] => Array
(
[cod] => CRR
[pr] => LL
[aid] => VM2254
[gender] => m
[title] =>
)
)
[w_a_ml] => Array
(
)
[w_a_ol] => Array
(
)
[w_a_rl] => Array
(
[0] => Array
(
[rol] => 1
)
)
)
Array Two (Changed-Data)
Array
(
[w_a] => Array
(
[0] => Array
(
[cod] => CRR
[pr] => LL
[aid] => VM2254
[gender] => f
[title] => Mr
)
)
[w_a_ml] => Array
(
[0] => Array
(
[wl] => 255
[care] => Sahan
[heigh] =>
[adam] =>
[instance] => Look
)
)
[w_a_ol] => Array
(
)
[w_a_rl] => Array
(
[0] => Array
(
[rol] => 1
)
)
)
What I wan to achieve from the above two array is to compare and show the result in table format or Inside Form To each Field (Original and New-Change) like below.
FiledsN Original New Change
title Empty Mr
Wl Empty 255
Care Empty Sahan
gender m f
instance Empty Look
I've tried making the array a single array using this function:
function array_single($arr) {
if (!is_array($arr)) {
return FALSE;
}
$res = array();
foreach ($arr as $keys => $values) {
if (is_array($values)) {
$res = array_merge($res, array_single($values));
} else {
$res[$keys] = $values;
}
}
return $res;
}
And then comparing using this:
function diff($new,$old) {
$del=array_diff_assoc($old,$new);
$add=array_diff_assoc($new,$old);
return $diff=array("old"=>$del, "new"=>$add);
}

Get a part from the array

The following code is converting XML to array. I want to get a sting by the value of [name]. So for example: $..[offerid].. should give 8b5f7fd3806a42ccb0ade9f8309c5587
Who can help me?? :)
$xmldata = file_get_contents($XMLURL);
$arr= xml2ary($xmldata);
foreach($arr['m4n']['_c']['data']['_c']['record'] as $result){
echo "<pre>"; print_r($result);
}
The echo result is:
Array
(
[recordHash] => Array
(
[_v] => -652572603
)
[column] => Array
(
[0] => Array
(
[_a] => Array
(
[name] => url
)
[_v] => http://ad.dd.com/ppc/?20910868C187884459&zpar6=DF_1&ULP=[[http%3A%2F%2F]]
)
[1] => Array
(
[_a] => Array
(
[name] => title
)
[_v] => This is the title
)
[2] => Array
(
[_a] => Array
(
[name] => description
)
[_v] => Aanbod
)
[3] => Array
(
[_a] => Array
(
[name] => offerid
)
[_v] => 8b5f7fd3806a42ccb0ade9f8309c5587
)
)
)
Try looping through all of the items using a php foreach loop.
$name_to_find = 'offerid';
$value = FALSE;
foreach ($result['column'] as $item) {
if ($item['_a']['name'] == $name_to_find) {
$value = $item['_v'];
break;
}
}
// Now $value has the value of the entry with the name $name_to_find
If you end up finding an entry with the given $name_to_find, then $value will not be FALSE (granted that, you don't have any FALSE values :D).

php array sum for same value

I have this array:
Array ( [0] =>
Array (
[0] => Array (
[07] => Array (
[2] => 352.9
[3] => 375737
[4] => 1000000002
) )
[1] => Array (
[07] => Array (
[2] => 362.1
[3] => 375797
[4] => 1000000002
) )
)
Array ( [1] =>
[0] => Array (
[08] => Array (
[2] => 305.7
[3] => 375857
[4] => 1000000002
) )
)
)
i need a final array sum for the key 07 ( is the month ) like this:
Array ( [0] =>
Array (
[0] => Array (
[07] => Array (
[2] => 3254.9 ( the sum of each 07 )
[3] => 6521545 ( the sum )
[4] => 98474916521621 ( the sum )
) )
)
Array ( [1] =>
[0] => Array (
[08] => Array (
[2] => 305.7 ( not summed cause month 08 is only one )
[3] => 375857 ""
[4] => 1000000002 ""
) )
)
)
Any help?
Here, try this - I'm sure it's neither a perfect nor optimal solution ( 3 foreach-es ), but it works on a reasonably large data set...
$inputArray is the multidimensional array with the data you provided, btw...
EDIT: Fixed version:
$result = array();
foreach( $inputArray as $subArray ) {
foreach ( $subArray as $subKey => $member ) {
if ( empty( $result[$subKey]) ) {
$result[$subKey] = $member;
} else {
foreach ( $member as $id => $subMember ) {
if ( empty( $result[$subKey][$id]) ) {
$result[$subKey][$id] = $subMember;
} else {
$result[$subKey][$id] += $subMember;
}
}
}
}
}
EDIT2: Since you changed the format of arrays, the solution is different:
Note: $array1 and $array2 are your "global" - predefined arrays.
$arrayWrapper = array_merge( ( array ) $array1, ( array ) $array2 );
$result = array();
foreach ( $arrayWrapper as $inputArray ) {
foreach( $inputArray as $subArray ) {
foreach ( $subArray as $subKey => $member ) {
if ( empty( $result[$subKey]) ) {
$result[$subKey] = $member;
} else {
foreach ( $member as $id => $subMember ) {
if ( empty( $result[$subKey][$id]) ) {
$result[$subKey][$id] = $subMember;
} else {
$result[$subKey][$id] += $subMember;
}
}
}
}
}
}
Tested it with your data, should work.
Cheers.

Categories