Fetch value from array/string - php

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;
}
}
}

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.

Merge rows between two arrays of objects based on column value [duplicate]

This question already has answers here:
Merging and group two arrays containing objects based on one identifying column value
(4 answers)
Closed 5 months ago.
After I merged two arrays like this array_merge($array1, $array2);, it becomes like this:
array(10) {
[0]=>
object(stdClass) (2) {
["id"]=>
string(1) "1"
["text"]=>
string(5) "one"
}
[1]=>
object(stdClass) (2) {
["id"]=>
string(1) "2"
["text"]=>
string(8) "two"
}
[2]=>
object(stdClass) (2) {
["id"]=>
string(1) "3"
["text"]=>
string(4) "three"
}
[3]=>
object(stdClass) (2) {
["id"]=>
string(1) "4"
["text"]=>
string(8) "four"
}
[4]=>
object(stdClass) (2) {
["id"]=>
string(1) "5"
["text"]=>
string(3) "five"
}
[5]=>
object(stdClass) (2) {
["id"]=>
string(1) "1"
["unit"]=>
string(1) "0"
}
[6]=>
object(stdClass) (2) {
["id"]=>
string(1) "2"
["unit"]=>
int(0)
}
[7]=>
object(stdClass) (2) {
["id"]=>
string(1) "3"
["unit"]=>
int(0)
}
[8]=>
object(stdClass) (2) {
["id"]=>
string(1) "4"
["unit"]=>
string(1) "0"
}
[9]=>
object(stdClass) (2) {
["id"]=>
string(1) "5"
["unit"]=>
int(1)
}
}
Which means both arrays are literally merged. But what I wanted is since both arrays has common property called id and same value for it, it should become like:
array(2) {
[0]=>
object(stdClass) (2) {
["id"]=>
string(1) "1"
["text"]=>
string(5) "one"
["unit"]=>
int(0)
}
[1]=>
object(stdClass) (2) {
["id"]=>
string(1) "2"
["text"]=>
string(8) "two"
["unit"]=>
int(2)
}
}
Note that array1 has id, text while array2 has id and unit.
I did refer here as tried the first answer which suggest to use array_map(), but for me I'm getting error saying argument 1 is not an array.
Combine two arrays into a single array based on a common column value
EDIT:
tried this (doesn't work):
$array1 = array_walk($array1, function(&$value) { $value = (array) $value; })
$array2 = array_walk($array2, function(&$value) { $value = (array) $value; })
function modifyArray($a, $b)
{
if (!empty($a) && !empty($b)) {
return array_merge($a, $b);
} else if (!empty($a) && empty($b)) {
return $a;
} else if (empty($a) && !empty($b)) {
return $b;
}
}
$new = array_map("modifyArray", $array1, $array2);
var_dump($new);
Merging objects is noticeably more tedious than arrays. I'd be tempted to convert the array of objects to an array or arrays in my own project, but I won't for this solution.
Unlike arrays which can enjoy array_merge() or the union operator, objects need to be pushed in manually.
I am temporarily grouping data by using the id values as first level keys in the loop, then optionally sorting by those keys, then re-indexing the output to remove the temporary keys.
Code: (Demo):
$output = [];
foreach ($poorly_merged as $object) {
if (!isset($output[$object->id])) {
$output[$object->id] = $object;
} else {
foreach ($object as $property => $value) {
$output[$object->id]->{$property} = $value;
}
}
}
ksort($output); // optionally order by ids
var_export(array_values($output));
Or:
$output = [];
foreach ($poorly_merged as $object) {
if (!isset($output[$object->id])) {
$output[$object->id] = (object)[];
}
foreach ($object as $property => $value) {
$output[$object->id]->{$property} = $value;
}
}
ksort($output); // optionally order by ids
var_export(array_values($output));
Better practice would be not to merge your twp input arrays to form the $poorly_merged array. You could use iterate the second array of objects and add that data into the first -- this would be a more direct solution.

How to get a count each unique element in the general array

Good day.
Code:
array(4) {
[0]=> array(1) {
[0]=> array(3) {
[0]=> string(11) "art_7880" [1]=> string(1) "1" [2]=> int(2950)
}
[1]=> array(3) {
[0]=> string(8) "art_7880" [1]=> string(1) "1" [2]=> int(2955)
}
[2]=> array(3) {
[0]=> string(8) "art_7880" [1]=> string(1) "1" [2]=> int(1335)
}
[3]=> array(3) {
[0]=> string(8) "art_7883" [1]=> string(1) "1" [2]=> int(4335)
}
}
I get array unique elements:
$arr_uniq = array();
foreach ($all_array as $keys => $elms ) {
if(!in_array($elms[0], $arr_uniq)) {
$arr_uniq[] = $elms[0];
}
}
Tell me pleasse how to get a count each unique element in the general array?
result should been next:
art_7880 - 3
art_7883 - 1
Assuming $all_array is subarray of your main array in your var_dump snipett, the general idea is
$result = array();
foreach ($all_array as $elms)
$result[$elms[0]]++;
array_count_values()
http://php.net/array_count_values
You should be able to easily apply this function.

How do I sort this array

I grouped an array using the following script
$grouped_array = array();
foreach($ungrouped_array as $item) {
//group them by id
$grouped_array[$item['id']][] = $item;
}
Now this grouped array looks like this
array(3) {
[1]=>
array(2) {
[0]=>
array(1) {
["id"]=>
string(1) "1"
}
[1]=>
array(1) {
["id"]=>
string(1) "1"
}
}
[6]=>
array(1) {
[0]=>
array(1) {
["id"]=>
string(1) "6"
}
}
[2]=>
array(4) {
[0]=>
array(1) {
["id"]=>
string(1) "2"
}
[1]=>
array(2) {
["id"]=>
string(1) "2"
["sub"]=>
string(1) "1"
}
[2]=>
array(2) {
["id"]=>
string(1) "2"
["sub"]=>
string(1) "2"
}
[3]=>
array(1) {
["id"]=>
string(1) "2"
}
}
}
I have deleted the most part of the array to make it shorter but there is no [0] field in this grouped array
All array fields are given the name of [id]'s value. I have no problem with that, I just have to short it again by [ID]
any suggestion will be great.
This should work to get 1, 2, 6:
<?php
$grouped_array = array();
foreach($ungrouped_array as $item) {
$grouped_array[$item['id']][] = $item;
}
// sort by key.
ksort( $grouped_array, SORT_NUMERIC );
print_r( $grouped_array );

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