When I run this:
<?php
$array = array_count_values($roles);
var_dump($roles);
$result = array();
foreach($array as $key => $value) {
$result[]=array("name"=>$key,"data"=>$value);
}
?>
I get this
Warning: array_count_values(): Can only count STRING and INTEGER
values!
The var_dump gives me
array(7) { ["francese"]=> array(2) { ["maschio"]=> array(1) { [0]=> bool(true) } ["femmina"]=> array(1) { [0]=> bool(true) } } ["chimica"]=> array(1) { ["maschio"]=> array(2) { [0]=> bool(true) [1]=> bool(true) } } ["fisica"]=> array(2) { ["maschio"]=> array(2) { [0]=> bool(true) [1]=> bool(true) } ["femmina"]=> array(1) { [0]=> bool(true) } } ["scienze"]=> array(1) { ["maschio"]=> array(1) { [0]=> bool(true) } } ["inglese"]=> array(1) { ["maschio"]=> array(1) { [0]=> bool(true) } } ["spagnolo"]=> array(1) { ["maschio"]=> array(1) { [0]=> bool(true) } } ["italiano"]=> array(1) { ["femmina"]=> array(1) { [0]=> bool(true) } } }
When I run this I get the correct result:
<?php
foreach($roles as $skill => $genderB) {
$males = isset($genderB['maschio']) ? count($genderB['maschio']): 0;
$females = isset($genderB['femmina']) ? count($genderB['femmina']): 0;
$total = $males + $females;
$data[] = $total;
echo "<li>We have ".$total." teachers of ".$skill.", ".$males." male, ".$females." female</li>";
}
?>
Result
We have 2 teachers of francese, 1 male, 1 female
We have 2 teachers of chimica, 2 male, 0 male
And what I would like to achieve is:
[{"name":"francese","data":2},{"name":"inglese","data":4}]
I am getting confused on which array I should be getting
UPDATE
This is the json_encode($roles) as request in the comment
{"francese":{"maschio":[true],"femmina":[true]},"chimica":{"maschio":[true,true]},"fisica":{"maschio":[true,true],"femmina":[true]},"scienze":{"maschio":[true]},"inglese":{"maschio":[true]},"spagnolo":{"maschio":[true]},"italiano":{"femmina":[true]}}
UPDATE 2
With the answer I got I am still not getting the correct json
[{"name":0,"data":{"name":"francese","data":2}},{"name":1,"data":{"name":"chimica","data":2}},{"name":2,"data":{"name":"fisica","data":3}},{"name":3,"data":{"name":"scienze","data":1}},{"name":4,"data":{"name":"inglese","data":1}},{"name":5,"data":{"name":"spagnolo","data":1}},{"name":6,"data":{"name":"italiano","data":1}}]
when I run
$result = array();
foreach($final_array as $key => $value) {
$result[]=array("name"=>$key,"data"=>$value);
}
echo json_encode($result);
It should be
[{"name":"francese","data":2},{"name":"inglese","data":4}]
you just need to make array and push it into final array like otherwise your code perfect
// $data[] = $total;
$final_array[]=array("name"=>$skill,"data"=>$total);
UPDATE 1:
<?php
$roles ='{"francese":{"maschio":[true],"femmina":[true]},"chimica":{"maschio":[true,true]},"fisica":{"maschio":[true,true],"femmina":[true]},"scienze":{"maschio":[true]},"inglese":{"maschio":[true]},"spagnolo":{"maschio":[true]},"italiano":{"femmina":[true]}}';
$roles_new = json_decode($roles,true);
echo "<pre>";
print_r($roles_new);
$final_array =array();
foreach($roles as $skill => $genderB) {
$males = isset($genderB['maschio']) ? count($genderB['maschio']): 0;
$females = isset($genderB['femmina']) ? count($genderB['femmina']): 0;
$total = $males + $females;
$final_array[]=array("name"=>$skill,"data"=>$total);
}
print_r($final_array);
echo json_encode($final_array);
?>
OUTPUT :
[{"name":"francese","data":2},{"name":"chimica","data":2},{"name":"fisica","data":3},{"name":"scienze","data":1},{"name":"inglese","data":1},{"name":"spagnolo","data":1},{"name":"italiano","data":1}]
Related
I checked this question and answers:
How to group a multidimensional array by a particular subarray value?
He wanted to group results by 'level'. But how would you do it to group it by 'level' first and then by 'type'?
Its pretty straight forward. Loop through $items array. Get each item's level and type and if they are not set yet, initialize them with an empty array. Then just push the "cust" value into the array.
I have given the code below.
I am assuming "$items" is an array which contains the input.
$g = [];
foreach($items as $k => $v) {
$l = $v["level"];
$t = $v["type"];
$c = $v["cust"];
if(!isset($g[$l])) {
$g[$l] = [];
}
if(!isset($g[$l][$t])) {
$g[$l][$t] = [];
}
$g[$l][$t][] = [
"cust" => $c
];
}
var_dump($g);
The output of this code would be like below:
array(3) {
[1]=>
array(1) {
["standard"]=>
array(2) {
[0]=>
array(1) {
["cust"]=>
string(6) "XT8900"
}
[1]=>
array(1) {
["cust"]=>
string(6) "XT8944"
}
}
}
[3]=>
array(1) {
["premier"]=>
array(2) {
[0]=>
array(1) {
["cust"]=>
string(6) "XT8922"
}
[1]=>
array(1) {
["cust"]=>
string(6) "XT8816"
}
}
}
[7]=>
array(1) {
["standard"]=>
array(1) {
[0]=>
array(1) {
["cust"]=>
string(6) "XT7434"
}
}
}
}
[P.S.]: You can also use sort to solve this problem easily. That's another way of solving this problem.
I have the following code:-
if( $featured_query->have_posts() ): $property_increment = 0;
while( $featured_query->have_posts() ) : $featured_query->the_post();
$town = get_field('house_town');
$a = array($town);
$b = array_unique($a);
sort($b);
var_dump($b);
$property_increment++; endwhile; ?>
<?php endif; wp_reset_query();
var_dump(b) shows:-
array(1) { [0]=> string(10) "Nottingham" } array(1) { [0]=> string(9) "Leicester" } array(1) { [0]=> string(9) "Leicester" } array(1) { [0]=> string(11) "Mountsorrel" } array(1) { [0]=> string(12) "Loughborough" } array(1) { [0]=> string(12) "Loughborough" }
var_dump($town) shows:-
string(10) "Nottingham" string(9) "Leicester" string(9) "Leicester" string(11) "Mountsorrel" string(12) "Loughborough" string(12) "Loughborough"
var_dump($a) shows:-
array(1) { [0]=> string(10) "Nottingham" } array(1) { [0]=> string(9) "Leicester" } array(1) { [0]=> string(9) "Leicester" } array(1) { [0]=> string(11) "Mountsorrel" } array(1) { [0]=> string(12) "Loughborough" } array(1) { [0]=> string(12) "Loughborough" }
What I want to do is get the unique vales of $town and output them into a select option:-
<select>
<option value="Leicester">Leicester</option>';
<option value="Loughborough">Loughborough</option>';
<option value="Mountsorrel">Mountsorrel</option>';
</select>';
In alpha as above, any help would be much appreciated.
#collect all get_field('house_town') in while
$collect[] = get_field('house_town');
#then do the work
$html = implode('',
array_map(
function($a){
return "<option value='{$a}'>{$a}</option>";
},
array_unique($collect)
)
);
Your array needs to be un-nested with array_column before you sort it and make it unique. So after you initialised $a, continue like this:
$b = array_unique(array_column($a, 0));
sort($b);
and then make the HTML:
$html = "";
foreach($b as $town) {
$html .= "<option value='$town'>$town</option>";
}
echo "<select>$html</select>";
If you don't have array_column, then you can use this replacement:
function array_column($arr, $column) {
$res = array();
foreach ($arr as $el) {
$res[] = $el[$column];
}
return $res;
}
Here's a summary of Chris G's comment and trincot's code snippet for generating the HTML code.
Note: for testing purposes I have created the $town array manually here. Replace it by your statement $town = get_field('house_town');
<?php
$town = array(
"Nottingham",
"Leicester",
"Leicester",
"Mountsorrel",
"Loughborough",
"Loughborough"
);
// $town = get_field('house_town');
$html = "";
$town = array_unique($town);
sort($town);
foreach($town as $xtown) {
$html .= "<option value='$xtown'>$xtown</option>";
}
echo "<select>$html</select>";
?>
Basic/ General unique usage in while/ foreach loop
// refer to
$a = array($town); // $a in while/ foreach loop
if(current($a) != next($a)) {
// do your query here // get required unique here
}
// Note: caring and sharing
I want to buld a Json object to feed my graphs. I have got the following code to change my PHP object.
$rows = $this->Website_model->getGraphData();
$_rows = array();
$i = 0;
foreach ($rows as $key => $row) {
foreach ($row as $column => $value) {
$_rows[$i][] = $value;
}
$i++;
}
$rows = $_rows;
echo json_encode(array("sEcho" => intval($sEcho), "data" => $rows));
die();
My current ouput looks like this:
array(24) {
[0]=>
array(3) {
[0]=>
string(7) "3283581"
[1]=>
string(10) "2013-10-16"
}
It should look something like this:
{"y":15,"x":"2012-11-19"},{"y":18,"x":"2012-11-19"} etc etc
How can I add the Y and X to my data and take care i will get the right output to feed my graph?
/////////////////////////////////////////////////////
I tried the following:
Now i'm using the following code:
$rows = $this->Website_model->getGraphDataPositives();
$_rows = array();
$i = 0;
foreach ($rows as $key => $row) {
foreach ($row as $column => $value) {
$_rows[$i]['x'] = $value;
$_rows[$i]['y'] = $value;
$i++;
}
}
This results in the following response:
array(48) {
[0]=>
array(2) {
["x"]=>
string(7) "3283581"
["y"]=>
string(7) "3283581"
}
[1]=>
array(2) {
["x"]=>
string(10) "2013-10-16"
["y"]=>
string(10) "2013-10-16"
}
So it isn't okay yet.. it should say:
array(48) {
[0]=>
array(2) {
["x"]=>
string(7) "3283581"
["y"]=>
string(7) "2013-10-16"
}
[1]=>
array(2) {
["x"]=>
string(10) "1512116"
["y"]=>
string(10) "2013-10-17"
}
This would create an array like you wish :
$rows = $this->Website_model->getGraphData();
$_rows = array();
$i = 0;
foreach ($rows as $key => $row) {
$_rows[$i]['y'] = $rows[0];
$_rows[$i]['x'] = $rows[1];
$i++;
}
Your example data is not correct. You have array(3), but you show only 2 elements. Also in the output data you have 3283581 and the y values are 15 and 18. So there is no way I can guess how to convert those values.
I need to delete all elements where FacetValueCount is lower than 3.
How can I do this?
This is my array: Array name is $farben
array(8) {
[0]=>
array(2) {
["FacetValueName"]=>
string(4) "Blau"
["FacetValueCount"]=>
int(5)
}
[1]=>
array(2) {
["FacetValueName"]=>
string(7) "Schwarz"
["FacetValueCount"]=>
int(3)
}
[2]=>
array(2) {
["FacetValueName"]=>
string(4) "blue"
["FacetValueCount"]=>
int(2)
}
[3]=>
array(2) {
["FacetValueName"]=>
string(4) "Grau"
["FacetValueCount"]=>
int(1)
}
}
<?php
$farben = ARRAY();
$farben[] = array('FacetValueName'=>'Blau', 'FacetValueCount' => 5);
$farben[] = array('FacetValueName'=>'Schwarz', 'FacetValueCount' => 3);
$farben[] = array('FacetValueName'=>'blue', 'FacetValueCount' => 2);
$farben[] = array('FacetValueName'=>'Grau', 'FacetValueCount' => 1);
print '<pre>'; var_dump($farben); print '</pre>';
foreach ($farben AS $key => $row) {
if ($row['FacetValueCount'] < 3) { unset($farben[$key]); }
}
print '<pre>'; var_dump($farben); print '</pre>';
?>
try this...
$farben = array_filter($farben, function($row) {
if($row["FacetValueCount"] > 3) {
return $row;
}
});
I'm trying to get this array ($resdata) with object(SimpleXMLElement) into a php array:
$resdata =
array(59) {
[0]=> ...
[10]=> object(SimpleXMLElement)#294 (28) {
["reservation_id"]=> string(7) "8210614"
["event_id"]=> string(6) "279215"
["space_reservation"]=> array(2) {
[0]=> object(SimpleXMLElement)#344 (9) {
["space_id"]=> string(4) "3760"
["space_name"]=> string(9) "205"
["formal_name"]=> string(33) "Center" }
[1]=> object(SimpleXMLElement)#350 (9) {
["space_id"]=> string(4) "3769"
["space_name"]=> string(9) "207"
["formal_name"]=> string(32) "Right" } } }
}
I've tried:
$res = (array)$resdata;
$reservation = $res['reservation'];
$result = array();
foreach ($reservation as $key => $value){
$res = array($value);
$spid = $res[0]->space_reservation->space_id;
echo $value->event_id."<br />";
echo $spid."<br />";
}
This only outputs the first space_id and I need to get all the space_ids within "space_reservation" array. Not all records will have multiple space_ids. Any help pointing me in the right direction is appreciated. Not sure if I should use xpath but I need to re-write my foreach statement regardless.
I was hoping to be able to literally convert all references to "object(SimpleXMLElement)#_ (#)" to "array(#)"
[10]=> array (28) {
["reservation_id"]=> string(7) "8210614"
["event_id"]=> string(6) "279215"
["space_reservation"]=> array(2) {
[0]=> array (9) {
["space_id"]=> string(4) "3760"
["space_name"]=> string(9) "205"
["formal_name"]=> string(33) "Center" }
[1]=> array (9) {
["space_id"]=> string(4) "3769"
["space_name"]=> string(9) "207"
["formal_name"]=> string(32) "Right" } } }
}
the function in my cakephp 1.3 controller is this:
$xml = simplexml_load_string($string);
$this->data['events']= $xml->children();
$resdata = $this->data['events'];
$this->set('resdata',$resdata);
I think this should do what you are looking for:
foreach ($resdata as $res) {
echo $res->event_id . '<br />';
foreach ($res->space_reservation as $reservation) {
echo $reservation->space_id . '<br />';
}
}
Googled it and found a general solution for any SimpleXMLElement to array conversion:
function xml2array($xml) {
$arr = array();
foreach ($xml as $element) {
$tag = $element->getName();
$e = get_object_vars($element);
if (!empty($e)) {
$arr[$tag] = $element instanceof SimpleXMLElement ? xml2array($element) : $e;
}
else {
$arr[$tag] = trim($element);
}
}
return $arr;
}