addition of strings inside an multidimensional array - php - php

I have an array like this. Each array is having a user ID with scores populated dynamically. I want to add all the strings inside the array and have the key as userID
array(3) {
[13702]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "9"
[2]=> .....
..... more elements
}
[13703]=>
array(2) {
[0]=>
string(1) "7"
[1]=>
string(1) "6"
.....
..... more elements
}
[13774]=>
array(1) {
[0]=>
string(1) "7"
.....
..... more elements
}
}
I want to make it like below
array(
'13702'=> 13,//this is the sum of strings inside it
'13703'=> 13,
'13774'=> 7,
);
Please help

No need for array reduce, a simple array_map applying array_sum on each sub array should suffice:
$result = array_map('array_sum', $array);

Related

How to loop through array of multiple arrays in php

I am trying to loop through array of arrays in php. Usually get stalked with complex array sometimes but I need your kind assistance with this.
var_dump($array) produced the array below:
$arrayVal = array(6) {
["item_id"]=>
array(2) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
}
["request_explanation"]=>
array(2) {
[0]=>
string(7) "Welcome"
[1]=>
string(11) "Hello World"
}
["quantity"]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "4"
}
["unit_cost"]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "3"
}
["total_cost"]=>
array(2) {
[0]=>
string(1) "0"
[1]=>
string(1) "0"
}
["supporting_document"]=>
string(0) ""
}
My database table:
I want to be able to save each of the value in that array into the table above. Thanks for helping me.
Use the indexes of one of the sub-arrays to access all the other sub-arrays:
foreach ($array['item_id'] as $i => $item_id) {
$request_explanation = $array['request_explanation'][$i];
$quantity = $array['quantity'][$i];
// repeat this for all the columns
// Now you can insert all these variables into the database
}
Use a loop to build 2 separate arrays:
foreach($array['ExpensesList'] as $index => $val){
$array1[$index] = $array['ExpensesList'][$index][0];
$array2[$index] = $array['ExpensesList'][$index][1];
}
Then insert each array into your database individually.
This will not work if any sub array contains an index at 2, so this is explicitly for the example structure you provided.

PHP : How to include another array's elements in another array's index?

I have two arrays:
array(3)
{
[0]=>
string(1) "1"
[1]=>
string(1) "1"
[2]=>
string(1) "2"
}
array(3)
{
[0]=>
string(1) "abc"
[1]=>
string(1) "def"
[2]=>
string(1) "ghi"
}
Is there any way I could put the first array's elements into the second array's index producing the following result :
array(3)
{
[1]=>
string(1) "abc"
[1]=>
string(1) "def"
[2]=>
string(1) "ghi"
}
How can I do this in PHP? Thanks in advance.
Since the expected output is impossible the next best thing it to make a multidimensional array where the question id is a subarray for answers.
$arr1 = [1,1,2];
$arr2 = ["abc","def","ghi"];
Foreach($arr1 as $key => $id){
$threads[$id][] = $arr2[$key];
}
Var_dump($threads)
Outputs:
array(2) {
[1]=>
array(2) {
[0]=> "abc"
[1]=> "def"
}
[2]=>
array(1) {
[0]=> "ghi"
}
}
https://3v4l.org/qpJDA
You can use "array_combine" method for this purpose.
http://php.net/array-combine
However, you cannot have same multiple indexes in an array
array(3)
{
[1]=> "abc"
[1]=> "def"
[2]=> "ghi"
}
You have index "1" twice in array. So "def" will replace "abc" in your array

Sorting a subarray by subarray value?

I only found questions on how to sort arrays with subarray values, but I would like to know if you can sort a subarray with subarray values.
foreach ($el->getArray('plain') as $element){
foreach ($element as $data)
{
<?php echo $data['name']; ?>
<?php echo $data['value']; ?>
}
}
I think the best solution is to instantiate $array by doing:
$array = $el->getArray('plain');
before the foreach and then sort it immediately and then loop through it.
However, I am not sure you can sort an array inside an array. First, is this possible and second how would you do it?
As per my comment above, stop overthinking:
php > $arr = array(array('c', 'b', 'a'), array('r', 'q','p'));
php > var_dump($arr);
array(2) {
[0]=>
array(3) {
[0]=>
string(1) "c"
[1]=>
string(1) "b"
[2]=>
string(1) "a"
}
[1]=>
array(3) {
[0]=>
string(1) "r"
[1]=>
string(1) "q"
[2]=>
string(1) "p"
}
}
php > sort($arr[1]);
php > var_dump($arr);
array(2) {
[0]=>
array(3) {
[0]=>
string(1) "c"
[1]=>
string(1) "b"
[2]=>
string(1) "a"
}
[1]=>
array(3) {
[0]=>
string(1) "p"
[1]=>
string(1) "q"
[2]=>
string(1) "r"
}
}
Note how the r/q/p array has now become p/q/r - it's been sorted, even though it's an array nested in another array.

Sorting multi-dimentional array

I am trying to sort a multidimensional array based on 1 field so when I run a foreach loop to loop through it will be sorted in the right order...
I have tried several sources but found very few results on multi-dimensional array sorting...
When I run the loop on the below array it shows id field order as 1|2 i want it to be 2|1 what would be the best way to achieve this?
Basically just sort it in descending order...
Array:
array(2) {
[0]=> array(1) {
[0]=> array(4) {
[0]=> string(1) "1"
["id"]=> string(1) "1"
[1]=> string(0) ""
["invoicenum"]=> string(0) ""
}
}
[1]=> array(1) {
[0]=> array(4) {
[0]=> string(1) "2"
["id"]=> string(1) "2"
[1]=> string(0) ""
["invoicenum"]=> string(0) ""
}
}
}
Have you try this?
array_multisort
<?php
// Obtain a list of columns
foreach ($data as $key => $row) {
$mid[$key] = $row['id'];
}
// Use $data to sort by the common key
array_multisort($mid, SORT_DESC, $data);
?>

how do i convert this array?

i have the following array:
["addToCart"]=>
array(3) {
[1]=>
array(5) {
["aantal"]=>
int(1)
["film_id"]=>
string(1) "1"
["zaal_id"]=>
string(1) "1"
["dag"]=>
string(7) "maandag"
["seats"]=>
array(4) {
[0]=>
string(2) "67"
[1]=>
string(2) "68"
[2]=>
string(2) "69"
[3]=>
string(2) "70"
}
}
You can see that i have an array called "seats" inside the "addToCart" array.There are 4 items in the "seats" array.
what i would like to have is 4 separate arrays, they should all have the same content but each of them needs to have 1 value of "seats".
I'm not sure I got exactly what you're looking to do, but this would result in an array of arrays where each has only one seat:
$seatArrays = array();
foreach ($addToCart as $arr)
{
foreach ($arr["seats"] as $seat)
{
$seatArr = $arr; // Copy the original array
$seatArr["seats"] = $seat; // Replace the "seats" subarray with the current seat
$seatArrays[] = $seatArr;
}
}

Categories