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.
Related
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);
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
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.
How can I fetch the value "3" from this set of arrays:
array(1) { [0]=> string(1) "1" }
array(1) { [0]=> string(1) "3" }
array(1) { [0]=> string(1) "0" }
The arrays are output from a foreach statement of parenting array, which is:
array(3) { [0]=> string(8) "St" [1]=> string(1) "1" [2]=> string(1) "0" }
array(3) { [0]=> string(16) "Fu" [1]=> string(1) "3" [2]=> string(1) "0" }
array(3) { [0]=> string(13) "Pa" [1]=> string(1) "0" [2]=> string(1) "0" }
Where I am going for the second line value: "Fu" [1]=> string(1) "3"
Maybe I am doing it wrong from the first array?
You're not giving us much to go on. Are the 3 arrays already in a parent array, in an object, etc.? Below is how to get the # 3 from the 3 arrays...but I'm guessing this is not actually what you are asking, we likely need much more detail...the real problem you are trying to solve.
function getThree($arr1, $arr2, $arr3) {
$array = array();
$array[] = $arr1;
$array[] = $arr2;
$array[] = $arr3;
foreach( $array AS $subArray ) {
// whichever condition works for you
if( $subArray[0] == 'Fu' || $subArray[1] == 3 ) {
return $subArray;
}
}
}
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;
}
}