I seem to be having some issues looping through an array of data it works one way but not the way I want it to.
I should preface that the array is characterized by two variables $departments and $_UNIQUECONSTANT['departments']. Any help getting the constant working would be great.
The Array
Array (
[departments] => Array (
[0] => Array (
[id] => 2
[name] => Support
[description] => Support Department
)
[1] => Array (
[id] => 3
[name] => Accounting
[description] => Accounting Department
)
)
)
The working function.
foreach($departments as $department){
$department['id'];
}
The function I need to be working.
foreach($_UNIQUECONSTANT['departments'] as $department){
$department['id'];
}
Here are 2 methods that should work:
Note: I didn't test them
define('_UNIQUECONSTANT', serialize(array()));
$d = unserialize(_UNIQUECONSTANT);
foreach($d["departments"] as $department){
$department['id'];
}
// Newer PHP versions
foreach(unserialize(_UNIQUECONSTANT)["departments"] as $department){
$department['id'];
}
Related
so this is an odd one. I'm building a multi-email checker based on the 'Have I been pwned?' service. I have already built the function that loops through each email address, however in the output it's turned into a multidimensional array which runs really deep, like so:
Array
(
[0] => Array
(
["foo#bar.com"] => Array
(
[0] => Array
(
[Name] => 000webhost
)
[1] => Array
(
[Name] => Adobe
)
[2] => Array
(
[Name] => Patreon
)
[3] => Array
(
[Name] => Plex
)
)
)
[1] => Array
(
["foo2#bar.com"] => Array
(
[0] => Array
(
[Name] => 000webhost
)
)
)
)
I'd love to know how to strip this down so I can output it into a proper report for each email address that will list each breach according to the respective email address. I've tried a loop within a loop within a loop but that doesn't work. Like I say this array runs very deep. If somebody could help me that would be great! Thanks!
I'm not sure what you want to do there but if you want to get the mails and the number of breaches from the arrays, you can do it like that:
$results = array(
"foo#bar.com"=>array(
array("000webhost"),
array("Adobe"),
array("Patreon"),
array("Plex")
),
"foo2#bar.com"=>array(
array("000webhost2")
)
);
foreach ($results as $mail=>$value) {
echo "Mail: ".$mail." Breaches: ".count($value)."<br>";
}
Result:
Mail: foo#bar.com Breaches: 4
Mail: foo2#bar.com Breaches: 1
I'm having a real headache trying to iterate through an array and output elements. Using the array structure below I want to be able to output each instance of partname.
The following loop outputs the first instance of partname. I can't seem to adapt it to loop through all instances within the array. I'm sure I'm missing something basic.
foreach($ItemsArray['assignments'] as $item) {
$partname = $item['grades'][0]['partname'];
}
Array
(
[assignments] => Array
(
[0] => Array
(
[assigntmentid] => 5101
[grades] => Array
(
[0] => Array
(
[id] => 5101
[name] => Advanced AutoCad
[partid] => 6601
[partname] => Draft
[userid] => 82069
[grade] => 53
[courseid] => 6265
[fullname] => Computer Aided Design
)
)
)
[1] => Array
(
[assigntmentid] => 5101
[grades] => Array
(
[0] => Array
(
[id] => 5101
[name] => Advanced AutoCad
[partid] => 6602
[partname] => Final
[userid] => 82069
[grade] => 35
[courseid] => 6265
[fullname] => Computer Aided Design
)
)
)
)
)
Instead of just coding by slapping the keyboard. Write down what your function needs to do. In english (or whatever language you prefer). This would be something like:
Foreach assignment, loop over all grades and store the partname of
that grade into an array.
And then code it:
function getPartnames($assignments) {
$partNames = array();
foreach ($assignments as $assignment) {
foreach($assignment['grades'] as $grade) {
$partNames[] = $grade['partname'];
}
}
return $partNames;
}
So what did I do? I simply translated english to code.
Some few more tips: Use variables names that make sense. $item; $ItemArray; ... don't make sense. They tell me nothing
use an extra foreach in your loop:
foreach($ItemsArray['assignments'] as $item) {
foreach($item['grades'] as $grade) {
echo $grade['partname'];
}
}
My input array :
Array
(
[0] => Array
(
[id] => 1
[status_name] => Released
)
[1] => Array
(
[id] => 2
[status_name] => Under Construction
)
)
I want the output result :
Array (
[1] => Released
[2] => Under Construction
)
USe sub array id as output array key value and status_name as value array.
This is built into php as array_column. You would have:
$status_names = array_column($data, 'status_name', 'id');
print_r($status_name);
Bonus points on question as I had no idea this existed until looking for an answer for you.
Try the following:
function reOrderArray($input_array)
{
$result = array();
foreach ($input_array as $sub_array)
{
$result[$sub_array['id']] = $sub_array['status_name'];
}
return $result;
}
There might be a built-in php function to do this, array functions in php are quite powerful. I am, however, woefully unaware of one.
I have a multi-dimensional array (There is more than one item in "data" but i'm just showing one for this question):
Array
(
[data] => Array
(
[0] => Array
(
[to] => Array
(
[data] => Array
(
[0] => Array
(
[name] => fake name
[id] => 668071477234
)
[1] => Array
(
[name] => fake name
[id] => 1345556711
)
)
)
[updated_time] => 2012-12-24T23:46:26+0000
[id] => 327424994013537
)
)
)
I am trying to loop thru the array and determine if the id matches a variable sent from $_REQUEST, and if it does, I only want to return the "updated_time" value of the iteration.
Here's what I have but the date is always wrong, and doesn't match the proper iteration:
foreach($userOutbox['data'] as $outbox){
foreach($outbox['to']['data'] as $user){
if($user['id'] == $_REQUEST['facebook_id']){
$last_message_date = $outbox['updated_time'];
}
}
}
It's late and my eyes and brain are not helping me. Can anyone give me any direction?
Here's the solution that worked for me, just added break 2; Thanks for your help:
foreach($userOutbox['data'] as $outbox){
foreach($outbox['to']['data'] as $user){
if($user['id'] == $_REQUEST['facebook_id']){
$last_message_date = $outbox['updated_time'];
break 2;
}
}
}
I have an array which looks like this:
Array
(
[0] => Array
(
[1] => Array
(
[name] => vrij
// ...
)
[2] => Array
(
[name] => zat
// ...
)
)
)
I build this array using a for loop; however, I need to push 4 more 'records' to the array, which I can't do in this for loop.
I want the array to look like this, after the pushes:
Array
(
[0] => Array
(
[1] => Array
(
[name] => vrij
// ...
)
[2] => Array
(
[name] => zat
// ...
)
// ...
)
[1] => Array
(
[1] => Array
(
[name] => zon
//...
)
[2] // etc
)
)
The four new records should be pushed to array[1], so I get something like
$array[1][0], $array[1][1], etc. 0 1 2 3 contains the new data.
I tried quite a lot of stuff, to be honest. I need to do four of these pushes, so I was trying a for loop:
for($i = 0; $i < 4; $i++)
{
$day_info = $this->get_day_info($i, $data['init']['next_month'], $data['init']['current_year']);
$push['name'] = $day_info['day_name'];
array_push($data['dates'], $push);
}
and all other kinds of things with [], [1][$i], etc. Sometimes it even adds five arrays! I'm confused as to why it won't just add the [1][1], [1][2],.. I'm probably missing out on something here. Thanks a lot.
If this isn't clear, please do tell and I'll add more code to explain the problem better.
$extradates = array(1 => 'zon', 2 => 'maa');
$data['dates'][] = $extradates;
Will add 2 extra dates to the array using a new index.
Although if I see what you trying to accomplish, I think there might be a better way.
This above works though :)