I need to re-arrange a php multidimensional array so that to 'match' corresponding values from different arrays;
this is my reproducible example
<?php
// my original array
$myar= array(
array('A'=>'xxx','B'=>1),
array('A'=>'yyy','B'=>2),
array('A'=>'xxx','B'=>3),
array('A'=>'yyy','B'=>4)
);
print_r($myar);
// my desired result, new array
$myar_new= array(
array('xxx'=>1,'yyy'=>2),
array('xxx'=>3,'yyy'=>4)
);
print_r($myar_new);
?>
any help for that?
thanks
If I got your logic right then this function is what you need.
(Edited)
function strange_reformat($srcArray) {
$newArray = [];
$c = count($srcArray);
$i = 0;
$groupStart = null;
$collect = [];
while($i < $c) {
$row = current($srcArray[$i]);
if ($row == $groupStart) {
$newArray[] = $collect;
$collect = [];
}
$tmp = array_values($srcArray[$i]);
$collect[] = [$tmp[0] => $tmp[1]];
if ($groupStart === null) $groupStart = $row;
$i++;
}
$newArray[] = $collect;
return $newArray;
}
print_r(strange_reformat($myar));
yes, that's it...
but now I need to generalise it, please consider this case
$myar= array(
array('A'=>'xxx','B'=>1),
array('A'=>'yyy','B'=>2),
array('A'=>'zzz','B'=>5),
array('A'=>'xxx','B'=>3),
array('A'=>'yyy','B'=>4),
array('A'=>'zzz','B'=>6)
);
function strange_reformat($srcArray) {
$newArray = [];
$c = count($srcArray);
for ($i=0; $i<$c; $i+=3) {
$first = array_values($srcArray[$i]);
$second = array_values($srcArray[$i+1]);
$third = array_values($srcArray[$i+2]);
$newArray[] = [$first[0]=>$first[1], $second[0]=>$second[1], $third[0]=>$third[1]];
}
return $newArray;
}
print_r(strange_reformat($myar));
How can I add one element of array with every element of another array using foreach loop in php?
For clear understanding attaching screen shot.
in this picture element at index one will be add to all elements of another array. then element at index two will be add to every element of another array and so on...
Please review below code.
<?php
$arr = array(11,12,13,14,15);
$arrAnotherArray = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
$subArrLen = count($arrAnotherArray);
foreach($arr as $key => $value){
$arr[$key] = array();
$sum = 0;
foreach($arrAnotherArray as $key1=>$val){
$sum = $value + $val;
array_push($arr[$key],$sum);
}
}
echo "<pre>";
print_r($arr);
echo "</pre>";
?>
Try Like this :
$mainArray = array("11","12","13"....);
$returnArray = array();
foreach($mainArray as $key => $value){
$subArray = array();
//Create the sub array here as you want it.
$subArray[] = $key + 1;
$subArray[] = $key + 2;
$subArray[] = $key + 3;
$subArray[] = $key + 4;
$subArray[] = $key + 5;
$subArray[] = $key + 6;
$returnArray[$value] = $subArray;
}
I don't understand how you want your output to be formatted but here's the addition logic...
$main = range(11,14);
$sub = range(1,6);
$output = [];
foreach ($main as $mainNumber) {
foreach ($sub as $subNumber) {
$output[$mainNumber . '+' . $subNumber] = $mainNumber + $subNumber;
}
}
var_dump($output);
How can I implement the code:
$numberList3 = array();
for($i = 0; $i < 10; $i++)
{
$numberList3[$i] = $i;
}
print_r($numberList3);
Using a foreach loop as the no. of times the loop is going to execute is decided by the user at run time.
Any suggestion.?
Use array_fill maybe?
<?php
$n = 10;
$arr = array_fill(0,$n,0);
foreach($arr as $k => $v) {
$arr[$k] = $k;
}
print_r($arr);
Or, as suggested by #deceze, use range
<?php
$n = 10;
$arr = array();
foreach(range(0,$n-1) as $v) {
$arr[$v] = $v;
}
print_r($arr);
Or when the value is the same as the key, you can use just this:
<?php
$n = 10;
$arr = range(0,$n-1);
// no foreach needed
print_r($arr);
foreach() works for object and array not for a single value.
What you can do create an array or object from users input.
like:
$userInput = 10;
$forEachArray = array_fill(0, $userInput, 0);
$arrayToDisplay = array();
foreach($forEachArray as $key){
$arrayToDisplay[$key] = $key;
}
print_r($arrayToDisplay);
let me start off by saying what I have works just fine. However, I was wondering if there was a method using foreach loops, instead of while loops, that will return the same result. My current code is:
$a[0] = $_GET['a1'];
$a[1] = $_GET['a2'];
$a[2] = $_GET['a3'];
$a[3] = $_GET['a4'];
$a[4] = $_GET['a5'];
$a[5] = $_GET['a6'];
$a[6] = $_GET['a7'];
$a[7] = $_GET['a8'];
$a[8] = $_GET['a9'];
$a[9] = $_GET['a10'];
$a[10] = $_GET['a11'];
$a[11] = $_GET['a12'];
$a[12] = $_GET['a13'];
$a[13] = $_GET['a14'];
$a[14] = $_GET['a15'];
$a[15] = $_GET['a16'];
$a[16] = $_GET['a17'];
$a[17] = $_GET['a18'];
$a[18] = $_GET['a19'];
$a[19] = $_GET['a20'];
$a[20] = $_GET['a21'];
$a[21] = $_GET['a22'];
$a[22] = $_GET['a23'];
$a[23] = $_GET['a24'];
$a[24] = $_GET['a25'];
$a[25] = $_GET['a26'];
$a[26] = $_GET['a27'];
$a[27] = $_GET['a28'];
$array1 = array(
array(-0.0165333833412173,-0.805148897111146,-0.253228140499216,-0.184436610496828,-0.182460815146422,-0.562680616467753),
array(0.0530980021095432,0.197367160352892,0.486754591654999,0.100148472516492,0.129649075750223,-0.0891313538023273),
array(1.20820560236013,1.60297691019929,1.88645801788488,1.4097784563,1.35491780171462,1.83839262576814),
array(0.35474704442591,0.669288291278341,-0.618871707987998,0.314393917137463,0.369309934864932,0.302877707428276),
array(1.73737846681824,1.23045039205127,1.74985170545799,2.53444768504332,0.947425891979588,2.49693773110446),
array(1.08515864048978,1.19974869194067,1.40391203998614,0.645993089768699,1.18500365352365,0.183710871687018),
array(-0.158201130751605,0.245894811416122,-0.13491139550657,0.268781009345758,0.584094496717681,-0.475038989317082),
array(2.69120997158388,2.6335032877555,2.1678974119528,1.98159587601917,2.47871441914596,3.11218576826585),
array(1.58301428618536,1.4690581543491,1.64181831092486,1.31270093574087,0.956030437566211,1.68095459548238),
array(0.567053945109392,0.672686283651576,0.50520628014895,0.573859412747649,0.906581004455213,0.675935843127413),
array(2.21154924140175,2.4629227185282,2.44302564998387,1.76594656224375,2.24292394905905,2.46555856986067),
array(0.789058773638184,0.56494483459565,1.02220848055446,0.740827710977414,0.0568788365193514,0.974084436394248),
array(-1.22527151151768,-0.37116067796664,-0.993915606653743,-0.265414040283105,-0.0483919594705323,-1.26478189171631),
array(1.93198111668015,0.978691339072353,0.896606685235391,1.43344220993764,1.44428119515243,0.832846974841109),
array(0.0960968892129648,0.179429676667834,0.653561055735465,0.611111687937286,0.420917607921928,0.788124847059183),
array(-0.570188484564322,-0.367869613869776,-0.425708923999659,0.58428081962228,0.431696028180505,-0.560777966949268),
array(2.18753554884398,2.10453220450978,2.18396016604212,1.53663043726462,1.6128493800773,2.47023229651768),
array(0.933680338305853,0.701683572817621,1.05817969240128,1.13326689801564,1.06305023491714,1.28172788495342),
array(0.237030103809889,1.19429662809383,1.32352397170755,0.787441428409051,1.06861486730769,0.196323518345157),
array(1.27102015175144,0.999617071482296,1.14337292692749,1.79352497701309,1.14261585172827,1.18874166503307),
array(0.746730730970985,0.584005303699165,0.00845233248507581,0.617844244971367,0.646839413026708,0.393004935019083),
array(1.33877796293032,1.18403793988876,1.16686497194317,1.18487924665819,0.796233877768925,1.21668080067963),
array(1.66146357909026,1.52622605987311,1.51673796371559,2.45659237209795,1.58826974938031,1.87667392945756),
array(1.23205756178945,1.25461474916892,1.0738857243227,1.21949513612512,0.847503017550969,1.33075294766838),
array(0.976236852070675,1.0676137689421,1.08390543200419,1.19114160706157,0.804407032221587,0.918305581716723),
array(1.0514345299356,1.24825633668808,1.15750685444102,1.34538988886631,1.24490007119083,0.994386901437322),
array(-50.5445732202499,-53.6034355984409,-52.2301074110166,-59.4416059910645,-50.3559798265968,-51.5135149950383),
);
$array2 = array();
$x = 0;
while($x < COUNT($array1)){
$y = 0;
while($y < COUNT($array1[$x])){
$array2[$x][$y] = $array1[$x][$y] * $a[$x];
//echo($array2[$x][$y] . ', ');
$y++;
}
//echo('<br>');
$x++;
}
Again, while this works, I am trying to get into the habit of writing cleaner code. Additionally, I am still relatively new to PHP, so I'm still struggling a bit with some of its syntax and structuring.
Thanks in advance.
This should work identical to what you have.
$array2 = array();
foreach($a as $k1=>$a1)
{
foreach($array1[$k1] as $k2=>$a2)
{
$array2[$k1][$k2] = $a2 * $a1;
}
}
The basic format of a foreach loop is foreach($array as $key=>$value) if you don't need the array key, you can shorten it to foreach($array as $value).
You can reduce the code by directly accessing the $_GET values instead of copying them into $a.
$result = array();
foreach($array1 as $k1 => $array2) {
foreach($array2 as $k2 => $value) {
$result[$k1][$k2] = $value * $_GET['a'.($k1+1)];
}
}
Hi anybody can help me to find the maximum value of the array that are given in the below . i expect the result of 650 is the maximum value....
$my_array = array(array(128,300,140)10,15,array(130,array(500,650)));
Here you go, using RecursiveArrayIterator in 3 readable lines of code:
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
$flattenedArray = iterator_to_array($it);
$max = max($flattenedArray);
Or, if you want to not flatten (and copy), but prefer to iterate (uses far less memory, but slower):
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
$max = 0;
foreach ($it as $value) {
$max = max($value, $max);
}
Flatten the array, then call max() on it. The return value of max() should be 650 from your example.
Also possible is
$data = array(array(128,300,140),10,15,array(130,array(500,650)));
$max = 0;
array_walk_recursive(
$data,
function($val) use (&$max) {
if($val > $max) $max = $val;
}
);
echo $max; // 650
You could also do it recursively, if the item is an array, call the function again to return the max item from that array.
In the end you should have always the max item and then in the last iteration, you could call the max from those results.
This does the trick:
function flatten($ar) {
$toflat = array($ar);
$res = array();
while (($r = array_shift($toflat)) !== NULL) {
foreach ($r as $v) {
if (is_array($v)) {
$toflat[] = $v;
} else {
$res[] = $v;
}
}
}
return $res;
}
$arr = array(array(128,300,140),10,15,array(130,array(500,650)));
echo max(array_flatten($arr));
EDIT: Updated flatten array with the one at How to "flatten" a multi-dimensional array to simple one in PHP?
<?php
$my_array = array(array(128,300,140),10,15,array(130,array(500,650)));
function findLargest($arr) {
$largest = 0;
foreach ($arr as $item) {
if (is_array($item)) {
$item = findLargest($item);
}
if ($item > $largest) {
$largest = $item;
}
}
return $largest;
}
echo "Largest is ".findLargest($my_array)."\n";
?>
function maximum($in)
{
if (!is_array($in)) $max = $in;
else foreach ($in as $element)
{
$elementMax = maximum($element);
if (isset($max)) $max = max($elementMax, $max); else $max = $elementMax;
}
return $max;
}