I have a multidimensionel array of different departments, and a want to loop through it using a foreach loop, but for some reason, the foreach loop grabs the values under the first key through every iteration.
The array looks like this:
$departmentArray =
Array
(
[0] => Array
(
[dpt_id] => 5
[dpt_name] => Administration
[dpt_employees] => Array
(
[0] => Array
(
[started] => 2000-06-01
[stopped] => 9999-99-99
[empl_id] => 21
)
[1] => Array
(
[started] => 2000-06-01
[stopped] => 2010-01-01
[empl_id] => 23
)
)
)
[1] => Array
(
[dpt_id] => 6
[dpt_name] => Warehouse
[dpt_employees] => Array
(
[0] => Array
(
[started] => 2000-10-01
[stopped] => 2012-01-01
[empl_id] => 30
)
[1] => Array
(
[started] => 2007-10-17
[stopped] => 9999-99-99
[empl_id] => 197
)
)
)
)
And the foreach loop looks like this:
foreach($departmentArray as $key => $value) {
print_r($key);
print_r($value['dpt_name']);
}
And this prints:
0 Administration 1 Administration.
Does anyone know, why the loop does not move forward in the array and grab the value (Warehouse) under key/index 1 during its second iteration?
Total stab into the dark:
You have used $value in a foreach loop before as reference, like so:
foreach ($foo as &$value) { ... }
foreach ($departmentArray as $key => $value) { ... }
This is a well known side-effect of references. unset($value) after the first loop.
Related
I've been trying to figure out how I would be able to loop through two arrays and match values from each array which should only return the value that matches of the first array.
Array 1:
[0] => Array
(
[contact] => 68
[field] => 11
[value] => DBSA
[cdate] => 2019-11-14T11:21:08-06:00
[udate] => 2021-03-30T07:54:00-05:00
)
[1] => Array
(
[contact] => 68
[field] => 131
[value] => ABC
[cdate] => 2019-11-22T08:34:03-06:00
[udate] => 2021-03-30T07:54:00-05:00
)
Array 2:
[0] => Array
(
[source] => analysis_utm_source
[id] => 131
)
[1] => Array
(
[source] => analysis_utm_medium
[destination] => UTM medium
[id] => 132
)
So in this example I would like to retrieve the values from the first array but only where id/field = 131
I've tried to work with 'array_intersect' but this doesn't seem to give the right output.
I'd appreciate it if someone would be able to push me in the right direction.
EDIT: I've been able to solve it by using array_filter, final code like so;
foreach ($fieldValuesDB as $arr) {
$options[] = $arr['id'];
}
$result = array_filter($fieldValuesAC, function($v) use ($options) {
return in_array($v['field'], $options);
});
foreach($array2 as $k => $v)
{
$key = array_search($v['id'], array_column($array1, 'field'));
if($key){
echo print_r($array1[$key]);
}
}
you will get the index of the element in your first array stored in $key variable
This question already has answers here:
Push elements from one array into rows of another array (one element per row)
(4 answers)
Closed 5 months ago.
Problem: I want to add the values of Array2 one-after-another to a multidimensional array (array1).
Array 1:
Array
(
[0] => Array
(
[date] => 2020-01-01
[itemsSold] => 25.00000000
)
[1] => Array
(
[date] => 2020-01-02
[itemsSold] => 50.00000000
)
[2] => Array
(
[date] => 2020-01-03
[itemsSold] => 25.00000000
)
)
Array2:
Array
(
[0] => 10
[1] => 15
[2] => 25
)
Goal:
Array
(
[0] => Array
(
[date] => 2020-01-01
[itemsSold] => 25.00000000
[0] => 10
)
[1] => Array
(
[date] => 2020-01-02
[itemsSold] => 50.00000000
[0] => 15
)
[2] => Array
(
[date] => 2020-01-03
[itemsSold] => 25.00000000
[0] => 25
)
)
I am just learning programming since january 2021 so my best solution I got was this:
foreach ($array1 as $main => $value) {
foreach ($array2 as $sec => $entry) {
$array1[$main][] = $array2[$sec];
}
}
print_r($array1);
Output:
Array
(
[0] => Array
(
[date] => 2020-01-01
[itemsSold] => 25.00000000
[0] => 10
[1] => 15
[2] => 25
)
[1] => Array
(
[date] => 2020-01-02
[itemsSold] => 50.00000000
[0] => 10
[1] => 15
[2] => 25
)
[2] => Array
(
[date] => 2020-01-03
[itemsSold] => 25.00000000
[0] => 10
[1] => 15
[2] => 25
)
)
What am I doing wrong in my loop?? How can I get the values of array2 one-by-one into array1??
Thanks for any help ;)
The issue with your code is that you have nested a loop within another when you do not need to. Instead, use the loop format foreach ($array1 as $key => $value) so you can capture its index in $key, then use that as an index to $array2.
foreach ($array1 as $key => $value) {
// Append from $array2 by its index $key
// Using [] will append at index [0]
// You can modify $array1 inside the loop if you
// target it by $key. Notice that we are not using
// $value at all in this loop - only the $key matters.
$array1[$key][] = $array2[$key];
}
print_r($array1);
foreach also allows you to modify the value directly if you use a & reference. This is a little bit shorter:
foreach ($array1 as $key => &$value) {
// Looping over references to $array1 sub-arrays
// means that $value can be modified directly
$value[] = $array2[$key];
}
print_r($array1);
Iteration by reference is described in the PHP foreach manual
I have this arbitrary multi dimensional array.
Array (
[0] => Array
(
[0] => 0
[1] => 1
[2] => 2
)
[5] => Array
(
[0] => 0
[1] => 1
[2] => 2
)
[10] => Array
(
[0] => 0
[1] => 1
[2] => 2
)
[15] => Array
(
[0] => 0
[1] => 1
[2] => 2
)
[1] => Array
(
[0] => 2
[1] => 1
[2] => 2
)
[2] => Array
(
[0] => 2
[1] => 1
[2] => 2
)
)
I wanna run a for loop to extract the data of each subarray.
But I cannot do a simple for loop because the index (0,5,10,15,1) is arbitrary.
Is there a way to run a for loop then skip the sub array if it is empty?
Thanks!
This will take $array and loop though it, echoing the keys.
You have an array in an array, you can place a foreach in a foreach:
// First we take the main array ($array) and loop though its values
foreach( $array as $main_key =>$sub_array){
echo $main_key.": <br />\n"; // echo the key, some extra html to format
// the values of the mainarray are arrays themselves, just loop again:
foreach($subarray as $sub_key =>$subvalue){
echo '- '.$subvalue."<br />\n";
}
}
There's a bit of a trap here if you foreach in a foreach:
foreach($array as $key =>$value){
foreach($value as $key=>$value){ /* ... */; }
}
This will create very weird results. The inner foreach uses the same parameter-names and will mess everything up.
Here is the array:
[cart] => Array
(
[ProductId] => Array
(
[0] => P121100001
[1] => P121100002
)
[SellerId] => Array
(
[0] => S12110001
[1] => S12110001
)
[SpecifyId] => Array
(
[0] => 1
[1] => 2
)
[Quantity] => Array
(
[0] => 1
[1] => 1
)
[Price] => Array
(
[0] => 12
[1] => 29
)
[TotalPrice] => 41
)
I have the ProductId and I want to remove all the other items matching P121100002's key.
Is there an easy way to do this I can't can seem to come up with one?
You can loop through the full array and use unset() to, well, "unset" the specified index:
$index = array_search($cart['ProductId'], 'P121100002');
if ($index !== false) {
foreach ($cart as $key => $arr) {
unset($cart[$key][$index]);
}
}
The slight caveat to this approach is that it may disrupt your index orders. For instance, say you have:
[ProductId] => Array (
[0] => P121100001
[1] => P121100002
[2] => P121100003
)
And you want to remove P121100002, which has a corresponding index of 1. Using unset($cart['ProductId'][1]) will cause your array's to become:
[ProductId] => Array (
[0] => P121100001
[2] => P121100003
)
This may be something to remain concerned with if you're going to use a for loop to iterate through in the future. If it is, you can use array_values() to "reset" the indexes in the unset() loop from above:
foreach ($cart as $key => $arr) {
unset($cart[$key][$index]);
$cart[$key] = array_values($cart[$key]);
}
foreach($yourArray['ProductId'] as $key => $value) {
if ($value == $productIdToRemove) {
foreach($yourArray as $deleteKey => $deleteValue) {
unset($yourArray[$deleteKey][$key]);
}
break;
}
}
Use array_key_exists along with the unset() function
For example I have this array:
Array (
[0] => Array (
[id] => 45 [name] => Name1 [message] => Ololo [date_create] => 21:03:56 )
[1] => Array (
[id] => 46 [name] => visitor [message] => Hi! [date_create] => 21:06:28 )
)
I need converting to:
Array (
[id] => Array (
[0] => 45, [1] => 46
)
[name] => Array (
[0] => Name1, [1] => visitor
)
[message] => Array (
[0] => Ololo, [1] => Hi!
)
[date_create] => Array (
[0] => 21:03:56, [1] => 21:06:28
)
)
I like to know a function for converting this,
Try this block of code:
// Assuming the array you have is called $mainArray.
// The output will be $outputArray.
$outputArray = array();
foreach ($mainArray as $index => $array) { // Iterate through all the arrays inside the main array.
// foreach ($mainArray as $array) { // Use this if the numeric index order doesn't matter.
foreach ($array as $key => $value) { // Iterate through each inner array.
// Load the multidimensional array with the first key as one of (id, name, message, date_create) and second key as the numeric index (if you need it).
$outputArray[$key][$index] = $value;
// $outputArray[$key][] = $value; // Use this if the numeric index order doesn't matter.
}
}
print_r($outputArray);