Multidimensional Array: Extract second value if first is present - php

I have a multidimensional array:
array(3) {
["checkIn"]=> array(0) {}
["code"]=> array(0) {}
[0]=> array(2) {
["checkIn"]=> string(10) "2021-02-02"
["code"]=>string(10) "00Q4K00"
}
}
which I initialize in this way:
$array= array(
'checkIn'=>array(),
'code'=>array()
);
and so I value:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$array[] = array('checkIn'=>$row['checkIn'], 'code'=>$row['code']);
}
I can't figure out how to extract 'code' if '2021-02-02' is present:
if ( in_array('2021-02-02', array_column($array, 'checkIn')) ){
echo code??
}

Related

How to group a multidimensional array by multiple subarray values?

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.

is it possible to convert associative array to multidimensional array?

I have a form fields coming in the following format.
echo '<pre>';
print_r($_REQUEST);
echo '</pre>';exit;
[customer_id] = [0=> 4,1=>5];
[hobies]=>[0=> circket,1=>chess];
Here i want to convert into multidimensional associative array.
$output = [
4=>[
'hobies'=>'circket'
],
5=>[
'hobies'=> 'chess'
]
];
<?php
$customer_id = array(4, 5);
$hobies = array('circket', 'chess');
$output = array();
foreach($customer_id as $index=>$cid){
$output[$customer_id[$index]] = array("hobies"=>$hobies[$index]);
}
var_dump($output);
Output is
array(2) {
[4]=>
array(1) {
["hobies"]=>
string(7) "circket"
}
[5]=>
array(1) {
["hobies"]=>
string(5) "chess"
}
}

Loop through multidimensional array and write to file for each parent array

I'm looping through a multidimensional array of field groups and sub fields Using this code:
<?php
$field_groups = acf_get_field_groups();
foreach( $field_groups as $field_group ) {
$acf_groups = acf_get_fields_by_id( $field_group['ID'] );
foreach($acf_groups as $group) {
echo '<pre>';
var_dump($group);
echo '</pre>';
}
}
Which gets me this array:
array(20) {
["group"]=>
string(6) "button"
["sub_fields"]=>
array(5) {
[0]=>
array(23) {
["name"]=>
string(11) "button_text"
}
[1]=>
array(19) {
["name"]=>
string(11) "button_link"
}
}
}
array(20) {
["group"]=>
string(2) "h1"
["sub_fields"]=>
array(8) {
[0]=>
array(23) {
["name"]=>
string(9) "font_size"
}
[1]=>
array(26) {
["name"]=>
string(15) "font_size_units"
}
}
}
What I'm trying to do is print out a file with the sub_field values for each of the $group arrays ('button' and 'h1' respectively).
So for example, I want to end up with in this case 2 files:
button.php
h1.php
button.php would have:
button_text
button_link
h1.php would have:
font_size
font_size_units
I can get the two files to print out within that loop however the h1.php file includes the button sub_fields values, so:
button_text
button_link
font_size
font_size_units
How can I split the files up by the parent array and then print out a file for each group with its respective sub_fields values?
Figured it out...needed an additional grouped array to group the elements by the key in the nested array, in my case $sub['label'].
This isn't pretty or the most elegant but it works.
<?php
$field_groups = acf_get_field_groups();
foreach( $field_groups as $field_group ) {
$acf_groups = acf_get_fields_by_id( $field_group['ID'] );
$grouped = array();
foreach ($acf_groups as $group) {
$subs = $group['sub_fields'];
foreach($subs as $sub) {
$grouped[$group['label']][] = $sub['label'];
}
foreach ($grouped as $key => $items) {
// print by group here.
}
}
}

Trying to create a json but i get a php warning

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,"d‌​ata":{"name":"chimic‌​a","data":2}},{"name‌​":2,"data":{"name":"‌​fisica","data":3}},{‌​"name":3,"data":{"na‌​me":"scienze","data"‌​:1}},{"name":4,"data‌​":{"name":"inglese",‌​"data":1}},{"name":5‌​,"data":{"name":"spa‌​gnolo","data":1}},{"‌​name":6,"data":{"nam‌​e":"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}]

PHP Change multidimensional array structure

Hello I've multidimensional array that looks like that:
array(13890) {
[0]=>
array(2) {
["Icd"]=>
array(2) {
["id"]=>
int(111)
["nazwa"]=>
string(6) "DŻUMA"
}
["ProjectIcd"]=>
array(0) {
}
}
[1]=>
array(2) {
["Icd"]=>
array(2) {
["id"]=>
int(566)
["nazwa"]=>
string(7) "ŚWINKA"
}
["ProjectIcd"]=>
array(0) {
}
}
An so on.
I want to change it so it looks something like that:
array(13890) {
[0]=> array(2) {
["id"]=>
int(111)
["text"]=>
string(6) "DŻUMA"
}
How is this possible to do?
I want to add, I want to convert the array to json and feed it to select2 js in ajax.
Will that be a problem or not?
Short solution using array_map function:
// $arr is your initial array
$new_arr = array_map(function($a){
return ['id' => $a['Icd']['id'], 'text' => $a['Icd']['nazwa']];
}, $arr);
So you can simple create a new array and add there the values, which you want based on the old array. Then you convert the array to a json string with the php function json_encode():
$array = array("text"=>$old_array[0]["Icd"]["nazwa"]);
echo json_encode($array);
I hope this is something that you want.
$res = [];
$i = 0;
foreach($array as $arr) {
//get keys
if (count($arr) > 0) {
$keys = array_keys($arr);
// loop through the keys and fetch data of those keys
// put in array
foreach($keys as $key) {
if ($arr[$key]) {
$res[$i]['id'] = $arr[$key]['id'];
$res[$i]['text'] = $arr[$key]['nazwa'];
}
$i++;
}
}
}
print_r($res);
// To change array to json
echo json_encode($res);

Categories