Changing array key value in PHP - php

I have an array like below,
[test] => Array
(
[0] => 1
[1] => 3
[2] => 5
[3] => 13
[4] => 32
[5] => 51
)
i need to change this array into like below,
[test] => Array
(
[2] => 1
[4] => 3
[6] => 5
[8] => 13
[10] => 32
[12] => 51
)
i need to change the key value. How can i do this?.

$newArray = array_combine(
range(2,count($originalArray)*2,2),
array_values($originalArray)
);

The array_values() function returns an array containing all the values of an array and also it reset all the keys. you can do it as
$arr = array(0 => 1, 1 => 3, 2 => 5, 3 => 13, 4 => 32, 5 => 51);
$count = 1;
$tempArr = array();
foreach ($arr as $key => $val) {
$tempArr[$count * 2] = $val;
$count++;
}
var_dump($tempArr);exit;
Try this code at your side.

Related

Concatenate value elements of two or more different arrays in PHP

I originally have this array
Array
(
[0] => Amministrativo ^25^9,11,2,10,18,4,7,^17,13,^0.75^0^0.25
[1] => Logico deduttive^7^^21,^0.75^0^-0.25
[2] => Situazionali^8^^20,^0.75^0^0.375
)
Using the function explode and array_diff i can get to this
Array
(
[0] => Amministrativo
[1] => 25
[2] => 9,11,2,10,18,4,7,
[3] => 17,13,
[4] => 0.75
[5] => 0
[6] => 0.25
)
Array
(
[0] => Logico deduttive
[1] => 7
[2] =>
[3] => 21,
[4] => 0.75
[5] => 0
[6] => -0.25
)
Array
(
[0] => Situazionali
[1] => 8
[2] =>
[3] => 20,
[4] => 0.75
[5] => 0
[6] => 0.375
)
but I would like to concatenate the elements of each array to get a unique array. I think I need to use the array_map function but I don't know how. This below is the result I would like to achieve
Array (
[0] => Amministrativo Logico deduttive Situazionali
[1] => 25 7 8
[2] => 9,11,2,10,18,4,7,
[3] => 17,13,21,20,
[4] => 0.75 0.75 0.75
[5] => 0 0 0
[6] => 0.25 -0.25 0.375
)
tnks
EDIT:
I tried the code that is here and it is fine.
But now I realized that there is also the problem that the arrays can be in variable number 1, 2, 3 or more and I can't know it before, I should adapt this code
$result = array_map(function ($item1, $item2,$item3) {
return "$item1 $item2 $item3";
}, $test[0], $test[1],$test[2]);
The lines of the original array are split with explode. The result is a two-dimensional array. This is transposed (swapping rows and columns). Then the lines are reassembled with implode.
function array_transpose(array $a) {
$r = array();
foreach($a as $keyRow => $subArr) {
foreach($subArr as $keyCol => $value) $r[$keyCol][$keyRow] = $value;
}
return $r;
}
$arr = [
'Amministrativo ^25^9,11,2,10,18,4,7,^17,13,^0.75^0^0.25',
'Logico deduttive^7^^21,^0.75^0^-0.25',
'Situazionali^8^^20,^0.75^0^0.375',
];
foreach($arr as $i => $row){
$arr[$i] = explode('^',$row);
}
$arr = array_transpose($arr);
foreach($arr as $i => $row){
$arr[$i] = implode(' ',$row);
}
var_export($arr);
Demo: https://3v4l.org/IdsDh

Find maximum value in PHP array

Below is my array:
Array
(
[3] => Array
(
[2] => 3
[4] => 1
[5] => 2
[6] => 2
)
[5] => Array
(
[2] => 1
[3] => 2
[4] => 3
[6] => 3
)
In this array I want to find the maximum number and if the array contains the same maximum values then we choose one maximum value randomly.
Expected output like below:
Array
(
[3] => Array
(
[2] => 3
)
[5] => Array
(
[4] => 3
)
[6] => Array
(
[2] => 3
)
)
This is what I've tried:
$found = Array( [3] => Array ( [2] => 3 [4] => 1 [5] => 2 [6] => 2 ) [5] => Array ( [2] => 1 [3] => 2 [4] => 3 [6] => 3 ) [6] => Array ( [2] => 3 [3] => 2 [4] => 2 [5] => 3 ))
$fmaxnum = array();
foreach($found as $fk => $fv){
$fmaxnum[$fk] = max($fv);
}
echo "<pre>";print_r($fmaxnum);echo "</pre>"
You will get max value with max() but for index you have to use array_keys()
You can try this:
$found = Array ( '3' => Array ( '2' => 3 ,'4' => 1, '5' => 2, '6' => 2 ),
'5' => Array ( '2' => 1 ,'3' => 2, '4' => 3, '6' => 3 ),
'6' => Array ( '2' => 3 ,'3' => 2, '4' => 2, '5' => 3 )
);
$fmaxnum = array();
foreach($found as $fk => $fv){
$max_key = array_keys($fv, max($fv));
$fmaxnum[$fk] = array(
($max_key[0]) => max($fv) /* This will give small index value */
/* (count($max_key)-1) => => max($fv) // this will give highest index value */
);
}
echo "<pre>";
print_r($fmaxnum);
echo "</pre>";
Simple solution with array_map and array_keys functions:
// supposing $arr is your initial array
$arrMax = array_map(function($v){
$maximum = max($v);
return [array_keys($v, $maximum)[0] => $maximum];
}, $arr);
print_r($arrMax);
The output:
Array
(
[3] => Array
(
[2] => 3
)
[5] => Array
(
[4] => 3
)
[6] => Array
(
[2] => 3
)
)
if you just want to know the highest value as I understood
In this array I want to find the maximum number and if the array
contains the same maximum values then we choose one maximum value
randomly.
you could just do this
$array = [['3','1','2','2'],['1','2','3','3'],['2','1','1','3']];
$result = [];
foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $value) {
$result[] = $value;
}
echo max($result);
this will foreach all the array and push the values on $result, then you just have one level array and easily use max function

Getting the first key in a multidimensional array with PHP

I have a multidimensional array called $test:
Array (
[First item] => Array (
[screen] => 2
[1] => 2
[2] => 2
[3] => 2
[4] => 2
)
[Second Item] => Array (
[screen] => 3
[1] => 3
[2] => 3
[3] => 3
[4] => 3
)
)
I am trying to get the keys: screen, 1, 2, 3, and 4.
They are the same for First item and Second item. Do you know how I can loop through this array to get those values? So, basically getting the keys for the first array in my multidimensional array. Thank you!
How about this:
$keys = array_keys($test['First item']);
Or if you want to do it manually:
$keys = array();
foreach($test['First item'] as $key => $value) {
$keys[] = $key;
}
That would be something like:
$keys = array_keys(reset($your_array));
reset() gets the first value of your array and array_keys() the keys of the resulting array.
You might want to split it in two lines using a temporary variable to avoid strict warnings.
It depends on what you are trying to archive:
$test = Array (
"First item" => Array (
"screen" => 2,
1 => 2,
2 => 2,
3 => 2,
4 => 2,
),
"Second Item" => Array (
"screen" => 3,
1 => 3,
2 => 3,
3 => 3,
4 => 3,
)
);
To get all the keys
$vals = [];
foreach($test as $k=>$v){
$vals = array_merge($vals, array_keys($v));
}
this will yield you:
Array
(
[0] => screen
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => screen
[6] => 1
[7] => 2
[8] => 3
[9] => 4
)
Separated in another multidimensional array:
foreach($arr as $k=>$v){
$vals[] = array_keys($v);
}
Only the unique keys:
foreach($test as $k=>$v){
$vals = array_unique(array_merge($vals, array_keys($v)));
}

Sum up array values by keys

I need to sum up some values from subarrays in an array.
I have this array
Array
(
[smecid_2] => Array
(
[0] => 1
[1] => SMEC 55.6
[2] => 960
[3] => 864
[4] => 960
[5] => 864
)
[smecid_6] => Array
(
[0] => 3
[1] => SMEC 55.6 ATEX EX
[2] => 1290
[3] => 1161
[4] => 3870
[5] => 3483
)
)
What I want to do is sum up all fields from key [4] of each subarray and be able to echo the total in $total;
In this example $total; would be 4830 (960+3870).
Also, the array could hold more subarrays then these 2, when a user submits more products to order.
<?php
$array = array
(
'smecid_2' => array
(
0 => 1,
1 => 'SMEC 55.6',
2 => 960,
3 => 864,
4 => 960,
5 => 864,
),
'smecid_6' => array
(
0 => 3,
1 => 'SMEC 55.6 ATEX EX',
2 => 1290,
3 => 1161,
4 => 3870,
5 => 3483,
)
);
$sum = 0;
foreach ($array as $subarray)
{
$sum += $subarray[4];
}
echo $sum;
See it in action

How to combine both array and insert into database php

if only one array is there for example
$values = array(x, y, z);
i am adding them into database like this
foreach ($values as $value)
{
$insertFunction = addValues($value);
}
my arrays:
$array1 = Array ( 1, 2, 1, 2, 1, 2, 1, 2, 1, 2 );
$array2 = Array ( fb1, or1, fb2, or2, fb3, or3, fb4, or4, fb5, or5 );
But i want both array to combine and insert them into database.
How can i do this please help me
Updated:
When i am printing the POST values i am getting out put like this
Array ( [0] => 1 [1] => 2 [2] => 1 [3] => 2 [4] => 1 [5] => 2 [6] => 1 [7] => 2 [8] => 1 [9] => 2 )
Array ( [0] => fb1 [1] => or1 [2] => fb2 [3] => or2 [4] => fb3 [5] => or3 [6] => fb4 [7] => or4 [8] => fb5 [9] => or5 )
when i tried with array_merge my out put is like this
Array ( [0] => 1 [1] => 2 [2] => 1 [3] => 2 [4] => 1 [5] => 2 [6] => 1 [7] => 2 [8] => 1 [9] => 2 [10] => fb1 [11] => or1 [12] => fb2 [13] => or2 [14] => fb3 [15] => or3 [16] => fb4 [17] => or4 [18] => fb5 [19] => or5 )
How to insert them in separate columns in a table $array1 and $array2
my database table is like this
1.id
2.username
3.network_id
id is primary key
network_id values coming in array1
username values coming in array2
EDIT:
After you mentioned seperated columns I think I understand what you're looking for:
I'm assuming that array1 and array2 are in the same size.
for($i = 0; $i < count($array1); $i++)
{
$array2[$i] = (int)$array2[$i]; //"validating" the username (an integer)
mysql_query("INSERT INTO yourTableName (`username`,`network_id`) VALUES('".$array2[$i]."','".$array1[$i]."')");
}
Result:
tblName:
username: 1 2 1 ...
network_id: fb1 or1 fb2 ...
Is that what you were looking for?
Ignore this and merging:
$combined = array_merge($array1 , $array2);
//$combined = Array ( 1, 2, 1, 2, 1, 2, 1, 2, 1, 2,fb1, or1, fb2, or2, fb3, or3, fb4, or4, fb5, or5 );
I think you need array_merge.
You can use array_merge(), function to merge multiple array into one.
$arrays = array_merge($array1 , $array2);
foreach ($arrays as $value)
{
$insertFunction = addValues($value);
}
If you want associate an element from array a to an element from array b, you have to use array_combine() function.
$arrays = array_combine($array1,$array2);
foreach ($array as $aValue)
{
$insertFunction = addValues($aValue);
}

Categories