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.
Related
I have an array that looks like this.
Array
(
[0] => Array
(
[0] => 1
[1] => 500
[2] => 800
)
[1] => Array
(
[0] => 1
[1] => 100
[2] => 200
)
[2] => Array
(
[0] => 1
[1] => 300
[2] => 400
)
)
I want to use a foreach loop (not a function) to do several things. First I just want to subtract subelement [1] from subelement [2] to generate sub element [3] and then insert subelement3 back into the output array. See below:
$output=
Array
(
[0] => Array
(
[0] => 1
[1] => 500
[2] => 800
[3] => 300
)
[1] => Array
(
[0] => 1
[1] => 100
[2] => 200
[3] => 100
)
[2] => Array
(
[0] => 1
[1] => 400
[2] => 250
[3] => 150
)
)
To do the above I am using the for each loop below to generate the values of subelement 3 and it works ok. My problem is inserting the values back into the output array in the right place. I have commented out my last attempt which failed. Sorry if I am missing the obvious here.
foreach($result as $sub)
{
//get values
$sub[3]=$sub[1]-$sub[2];
echo "<difference>".$sub[3]."<br>";//works ok
//insert values back into array
//$result[$sub[0]][3] = $sub[3];
}
print "<pre>";
print_r($result);
print "</pre>";
die();
The problem:
$result[$sub[0]][3] = $sub[3]; //= $result[1][3] = $sub[3]
$sub[0] always return 1 (according to your input array) so it's not what you're looking for.
The solution:
Use the $array as $key => $val format in your foreach loop
and then access the sub array according to the relevant key.
foreach($result as $key => $sub)
{
//get values
$result[$key][3]=$sub[1]-$sub[2];
You can use array_map too, it's more elegant:
$output = aray_map( function ($item) {
$item[] = $sub[2] - $sub[1];
return $item;
}, $arr);
Demo
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.
I am trying to write some php code to process the second dimension's value of an array based on similar values of the first dimension values.
Following is the sample output.
[0] => Array (
[0] => 1
[1] => 0.091238491238491
)
[1] => Array (
[0] => 2
[1] => 0.2221793635487
)
[2] => Array (
[0] => 2
[1] => 0.10662717512033
)
[3] => Array (
[0] => 4
[1] => 0.44354338998346
)
[4] => Array (
[0] => 6
[1] => 0.2248243559719
)
[5] => Array (
[0] => 6
[1] => 0.31764705882353
)
[6] => Array (
[0] => 6
[1] => 0.15764625384879
)
[7] => Array (
[0] => 6
[1] => 0.19160083160083
)
[8] => Array (
[0] => 12
[1] => 0.31054875069499
)
[9] => Array (
[0] => 12
[1] => 0.10915034227918
)
[10] => Array (
[0] => 15
[1] => 0.32915461266474
)
//...........goes to 46000 elements
Now what I want to do is, if the index 0 values of each array is similar then I want to add the index 1's value.
So for example, if 0 index values for 4 arrays are same , I want to add index 1 values of all 4 arrays.
If there is a unique value on 0th index, dont add it with anything, simply store index 1's value and move on.
Thanks very much.
Ghanshyam
$added = array();
foreach ($array as $item) {
if (isset($added[$item[0]])) {
$added[$item[0]] += $item[1];
} else {
$added[$item[0]] = $item[1];
}
}
$p=0;
$temp = $final_prod_ex[0][1];
for($x=0; $x<count($final_prod)-1; $x++){
if($final_prod_ex[$x][0]==$final_prod_ex[$x+1][0]){
$temp = $temp + $final_prod_ex[$x+1][1];
}
else{
$ans[$p] = $temp." ".$final_prod_ex[$x][0];
$temp = $final_prod_ex[$x+1][1];
$p++;
}
}
Finally figured it out after a lot of thinking(I'm new to programming)...Array's name is $final_prod_ex. Comment on this if I can make it better. And sorry #deceze. I could not understand your solution. I know you were trying to give the value of one array as an index to another. But what the scenario is, that value isnt like 0,1,2,3,4.... Its like 1,3,5,6,7,10. We are missing numbers in between. Maybe I didnt understand your solution. Correct me if I am wrong.
Thanks for all the help.
I want to compare two tabdelimeted files. I take the files and converts them into two arrays with the following structure:
Array 1
Array
(
[0] => Array
(
[name] => name1
[qty] => 200
)
[1] => Array
(
[name] => name2
[qty] => 9
)
[2] => Array
(
[name] => name3
[qty] => 3
)
[3] => Array
(
[name] => name4
[qty] => 1
)
)
Array 2
Array
(
[0] => Array
(
[name] => name1
[qty] => 180
)
[1] => Array
(
[name] => name2
[qty] => 9
)
)
How can I compare these two arrays and where the value is different to replace the value in the array 2 with array of value 1.
The easiest way to do this would be to create an associative array for the second set of data, instead of the array format you has used above. Since you only seem to have two "columns", and these are effectively a key/value relationship this should be nice and easy.
This example takes the two input arrays you have generated to do it, but you can probably adjust this so that you create the associative array directly as you read the second:
// first create an associative array from the second indexed array
$secondAssoc = array();
foreach ($secondArray as $row) {
$secondAssoc[$row['name']] = $row['qty'];
}
/*
$secondAssoc now looks like:
Array
(
[name1] => 180
[name2] => 9
)
*/
// Now loop the first array and update it
foreach ($firstArray as $rowId => $row) {
if (isset($secondAssoc[$row['name']]) && $secondAssoc[$row['name']] != $row['qty']) {
$firstArray[$rowId]['qty'] = $secondAssoc[$row['name']];
}
}
/*
$firstArray now looks like this:
Array
(
[0] => Array
(
[name] => name1
[qty] => 180
)
[1] => Array
(
[name] => name2
[qty] => 9
)
[2] => Array
(
[name] => name3
[qty] => 3
)
[3] => Array
(
[name] => name4
[qty] => 1
)
)
*/
See it working.
EDIT Here is a version that also creates an array, $modifiedItems, that holds only the items that have changed:
// first create an associative array from the second indexed array
$secondAssoc = array();
foreach ($secondArray as $row) {
$secondAssoc[$row['name']] = $row['qty'];
}
// Now loop the first array and update it
$modifiedItems = array();
foreach ($firstArray as $rowId => $row) {
if (isset($secondAssoc[$row['name']]) && $secondAssoc[$row['name']] != $row['qty']) {
$firstArray[$rowId]['qty'] = $secondAssoc[$row['name']];
$modifiedItems[] = array('name'=>$row['name'],'qty'=>$secondAssoc[$row['name']]);
}
}
See it working.
I've got the following 2D array, stored inside the array variable $my_array
Array
(
[0] => Array
(
[0] => 3
[1] => 6
[2] => 3
)
[1] => Array
(
[0] => 3
[1] => 6
[2] => 3
)
[2] => Array
(
[0] => 3
[1] => 6
[2] => 3
)
)
I wanted to decrement all the [1] sub array values by 3. Tried the following code, with no success.
$my_array[$i]['1']=($my_array[$i]['1'])-3;
print_r($my_array);
Ideas?
foreach ($my_array as &$val) {
$val[1] -= 3
}
Something like this is what you're after.
foreach($my_array as $k=>$v){
if (isset($my_array[$k][1]) && is_numeric($my_array[$k][1])){
$my_array[$k][1] -= 3;
}
}