I want to check each subarray for item2 as the value in the first element (index 0).
If so, I want to return the second element's value (index 1).
This is my multidimensional array:
$arr = Array
(
1 => Array
(
0 => 'item',
1 => 3,
2 => 20,
),
2 => Array
(
0 => 'item2',
1 => 1,
2 => 21,
),
7 => Array
(
0 => 'item3',
1 => 4,
2 => 26,
),
20 => Array
(
0 => 'item4',
1 => 1,
2 => 39,
),
22 => Array
(
0 => 'item1',
1 => 10,
2 => 39,
),
23 => Array
(
0 => 'item2',
1 => 11,
2 => 39,
)
);
The expected result is: [1,11]
I used this function but it doesn't return all of the results:
function in_array_r($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}
Of course, use can not have multiple array elements with same key (item2=>1,item2=>11) , but you can collect all values [1] (1,11)
For example
$arr = // your array()
$res = array();
foreach ($arr as $row) {
if (isset($row[0]) && $row[0] == 'item2') {
$res[] = $row[1];
}
}
var_dump($res);
Related
I have an array like so
$dataArray = array(
array( 20800, 21679, 0 ),
array( 15254, 0, 3726 ),
array( 17426, 2973, 0 ),
array( 4391, 37, 0 ),
array( 39194, 435, 0 )
);
I am creating a sub array from this. Any value greater than 2000 becomes a 1, otherwise it becomes a 0.
foreach ($dataArray as $row => $data) {
foreach ($data as $key => $value) {
if($value > 1999) {
$dataArray[$row][$key] = 1;
} else {
$dataArray[$row][$key] = 0;
}
}
}
This produces the following
$dataArray = array(
array( 1, 1, 0 ),
array( 1, 0, 1 ),
array( 1, 1, 0 ),
array( 1, 0, 0 ),
array( 1, 0, 0 )
);
Now what I am trying to do is produce another array that shows the positions of the 1's within the above array. Each row should be represented by a new array. The output I am looking for is this
$dataArray = array(
array( 1, 2 ),
array( 1, 3 ),
array( 1, 2 ),
array( 1 ),
array( 1 )
);
So I can see that row 1 has a 1 in position 1 and 2, row 2 has a 1 in positions 1 and 3 etc.
I wanted to make use of my current loop, so I am trying something like this
$position = array();
foreach ($dataArray as $row => $data) {
foreach ($data as $key => $value) {
if($value > 1999) {
$position[$row] = $key + 1;
$dataArray[$row][$key] = 1;
} else {
$dataArray[$row][$key] = 0;
}
}
}
But this seems way off according to my output. How can I achieve what I am after?
Thanks
<?php
$dataArray = array(
array( 20800, 21679, 0 ),
array( 15254, 0, 3726 ),
array( 17426, 2973, 0 ),
array( 4391, 37, 0 ),
array( 39194, 435, 0 )
);
$endResult = [];
foreach ($dataArray as $key => $value) {
foreach ($value as $subkey => $subvalue) {
if ($subvalue >= 2000) {
$endResult[$key][] = $subkey +1;
}
}
}
print_r($endResult);
Array
(
[0] => Array
(
[0] => 1
[1] => 2
)
[1] => Array
(
[0] => 1
[1] => 3
)
[2] => Array
(
[0] => 1
[1] => 2
)
[3] => Array
(
[0] => 1
)
[4] => Array
(
[0] => 1
)
)
You could use array_map along with array_keys:
$dataArray = [
[1, 1, 0],
[1, 0, 1],
[1, 1, 0],
[1, 0, 0],
[1, 0, 0]
];
$onePositions = array_map(function($row) {
return array_map(function($position) { return $position + 1; }, array_keys($row, 1));
}, $dataArray);
echo '<pre>'; var_dump($onePositions); echo '</pre>';
Demo here
You can make use of the second parameter in the array_keys() function to get the positions after you populate the ones and zeros:
$position = array();
$binary = array();
foreach ($dataArray as $row => $data) {
// if you want to have the positions start at 1 instead of 0
// add a dummy value to the final array
$binary[$row][] = 0;
foreach ($data as $key => $value) {
if($value > 1999) {
$binary[$row][] = 1;
} else {
$binary[$row][] = 0;
}
}
$positions[] = array_keys($binary[$row], 1);
}
Demo Here
I have the following multidimensional array. I had to create keys the way it looks to group them accordingly.
Array
(
[Oranges] => Array
(
[Name] => Oranges
[l.VA123] => 17
[l.MA123] => 12
[l.GA123] => 9
[l.CT123] => 5
)
[Apple] => Array
(
[Name] => Apple
[l.CA123] => 13
)
[Grapes] => Array
(
[Name] => Grapes
[l.WI123] => 8
[l.FL123] => 5
)
)
However, I need all the subarrays to have the same keys. Missing ones should be filled with a value of 0. The final array should be like below so that all subarrays have equal length.
Array
(
[Oranges] => Array
(
[Name] => Oranges
[l.VA123] => 17
[l.MA123] => 12
[l.GA123] => 9
[l.CT123] => 5
[l.CA123] => 0
[l.WI123] => 0
[l.FL123] => 0
)
[Apple] => Array
(
[Name] => Apple
[l.CA123] => 13
[l.WI123] => 0
[l.FL123] => 0
[l.VA123] => 0
[l.MA123] => 0
[l.GA123] => 0
[l.CT123] => 0
)
[Grapes] => Array
(
[Name] => Grapes
[l.WI123] => 8
[l.FL123] => 5
[l.CA123] => 0
[l.VA123] => 0
[l.MA123] => 0
[l.GA123] => 0
[l.CT123] => 0
)
)
You need a simple + operator. As from manual:
The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.
$items = Array
(
'Oranges' => Array
(
'Name' => 'Oranges',
'l.VA123' => 17,
'l.MA123' => 12,
'l.GA123' => 9,
'l.CT123' => 5,
),
'Apple' => Array
(
'Name' => 'Apple',
'l.CA123' => 13,
),
'Grapes' => Array
(
'Name' => 'Grapes',
'l.WI123' => 8,
'l.FL123' => 5,
),
);
// static keys
$keys = [
'l.VA123' => 0,
'l.MA123' => 0,
'l.GA123' => 0,
'l.CT123' => 0,
'l.CA123' => 0,
'l.WI123' => 0,
'l.FL123' => 0,
];
// keys generated from source array, tricky approach
$keys = array_fill_keys(
// here we merge all elements of `$items` into one array
// as keys are repeated - you definitely got all keys that
// can be in `$items`, `array_keys` will give you these keys
// `array_fill_keys` will create array where key is what you need
// and value is 0.
array_keys(call_user_func_array('array_merge', $items)),
0
);
// keys generated from source array, SIMPLE approach
$keys = [];
foreach ($items as $item) {
foreach ($item as $k => $v) {
if ($k != 'Name') {
$keys[$k] = 0;
}
}
}
foreach ($items as &$item) {
$item = $item + $keys;
}
print_r($items);
Probably someone can come up with something more efficient, but without a list of keys that you want, I think you'll need to take a couple of passes of the array:
<?php
$fruits = [
"Oranges"=>["Name"=>"Oranges", "l.VA123"=>17, "l.MA123"=>12, "1.GA123"=>9, "1.CT123"=>5],
"Apple"=>["Name"=>"Apple", "1.CA123"=>13],
"Grapes"=>["Name"=>"Grapes", "1.WI123"=>8, "1.FL123"=>5]
];
$keys = [];
foreach ($fruits as $fruit) {
unset($fruit["Name"]);
$keys = array_merge($keys, array_keys($fruit));
}
$keys = array_fill_keys(array_unique($keys), 0);
foreach ($fruits as &$fruit) {
$fruit = array_merge($keys, $fruit);
}
print_r($fruits);
Since all keys and default values are "known", create an associative array, use a foreach() and modify the rows by reference, and use the union-assignment (combined) operator. This will allow the original values to overwrite the default values.
Code: (Demo)
$keys = [
'l.VA123' => 0,
'l.MA123' => 0,
'l.GA123' => 0,
'l.CT123' => 0,
'l.CA123' => 0,
'l.WI123' => 0,
'l.FL123' => 0,
];
foreach ($items as &$row) {
$row += $keys;
}
var_export($items);
If you want the keys to be consistently positioned, then use array_replace() or array_merge() instead of the union assignment operator.
Code: (Demo)
foreach ($items as &$row) {
$row = array_replace($keys, $row);
}
I have a multidimensional array
Array
(
[0] => Array
(
[entity_id] => 1
[order_id] => 1
[is_accepted] => 0
[is_canceled] => 0
[is_completed] => 0
)
[1] => Array
(
[entity_id] => 1
[order_id] => 1
[is_accepted] => 0
[is_canceled] => 0
[is_completed] => 0
)
)
I want check if both [is_canceled] values are 1 it return true.
If one of [is_canceled] value is 1 it return false.
Check this snippet,
var_dump(!in_array(0,array_column($orders,'is_canceled')));
array_column -> Return the values from a single column in the input array
in_array -> Checks if a value exists in an array
Give it a try, this should work.
All you need is a simple foreach loop to iterate over the array of objects, to check each object's is_canceled field.
function containsNoCancelledOrders($orders) {
foreach ($orders as $order) {
if ($order["is_canceled"]) return false;
}
return true;
}
$orders = [
[
"entity_id" => 1,
"order_id" => 1,
"is_accepted" => 0,
"is_canceled" => 0,
"is_completed" => 0,
],
[
"entity_id" => 1,
"order_id" => 1,
"is_accepted" => 0,
"is_canceled" => 0,
"is_completed" => 0,
],
];
var_dump($orders);
echo containsNoCancelledOrders($orders) ? 'true' : 'false';
try this:
function check($array)
{
if ($array[0]['is_cancelled'] == 0 && $array[1]['is_cancelled'] == 0) {
return false;
}
return true;
}
$check = check($array);
var_dump($check);
I have array in php :
Array
(
[id] => 1
[comp_id] => 1
[transaction_purpose] => 0
[source_of_funds] => 1
[beneficiary_relationship] => 0
[cus_occupation] => 0
[cus_id_image_2] => 0
[cus_id_image_3] => 0
[ben_id_type] => 0
[ben_id_number] => 1
)
I want to get only array key=>value pair if the valie is 1.
result array should be:
Array
(
[id] => 1
[comp_id] => 1
[source_of_funds] => 1
[ben_id_number] => 1
)
I tried with:
$returnArray = array();
foreach($mainArray as $r){
if($r>0){
array_push($returnArray, $mainArray);
}
}
But, It's giving me 4 times main array. Is there any way to achieve this? Thanks..
Just use array_filter():
$newarray = array_filter($array, function($var) {
return ($var === 1);
});
$newarray = array_filter($array);
Demo
$array = array(
'id' => 1,
'comp_id' => 1,
'transaction_purpose' => 0,
'source_of_funds' => 1,
'beneficiary_relationship' => 0,
'cus_occupation' => 0,
'cus_id_image_2' => 0,
'cus_id_image_3' => 0,
'ben_id_type' => 0,
'ben_id_number' => 1
);
$newarray = array_filter($array);
print_r($newarray);
Array
(
[id] => 1
[comp_id] => 1
[source_of_funds] => 1
[ben_id_number] => 1
)
Try this:
$returnArray = array_filter($result);
You can see PHP's array_filter function for more info.
Well, what else are you expecting to happen?
array_push($returnArray, $mainArray);
If you find an element which has >0 value, you push the ENTIRE original array onto the new one, not just the key/value you just tested.
You probably want:
$newarr = array();
foreach($mainArray as $key => $value) {
if ($value > 0) {
$newarr[$key] = $value;
}
}
I have an array $MyArray which has some elements which are also array (lets call them subarrays). I want to know how many elements the subarray with the most elements has. The problem is, that I don't know if the index exists:
max(
#count($MyArray[$i*7]),
#count($MyArray[$i*7+1]),
#count($MyArray[$i*7+2]),
#count($MyArray[$i*7+3]),
#count($MyArray[$i*7+4]),
#count($MyArray[$i*7+5]),
#count($MyArray[$i*7+6])
);
Struckture of $MyArray:
Array(
12 => array (
0 => array ( 0 => 0, 1 => 1, ),
1 => array ( 0 => 13, 1 => 1, ),
2 => array ( 0 => 15, 1 => 1, ),
3 => array ( 0 => 20, 1 => 1, ),
4 => array ( 0 => 69, 1 => 1, )
),
5 => array (
0 => array ( 0 => 55, 1 => 1, ),
1 => array ( 0 => 32, 1 => 1, ),
2 => array ( 0 => 12, 1 => 1, ),
3 => array ( 0 => 21, 1 => 5, )
),
....
)
Can this be done better (faster)?
edit: I know foreach and I don't want to loop over every element in this array. I just want an interval of it. # is used, because I don't know if $MyArray[$i*7 + x] is Null or an array.
$i is a element of [0, 1, 2, 3, 4] (sometimes also 5)
$biggest = 0;
foreach ($MyArray as $value) {
if ($biggest < count($value)) {
$biggest = count($value);
}
}
I see, you want the size of the biggest array in the array, correct?
Simple and old school approach:
<?php
$max = -1;
foreach($MyArray as $subarray)
{
$items = count($subarray);
if($items > $max)
$max = $items;
}
echo $max;
?>
This works best since you only want to know how many elements the subarray with the most elements has.
$max = 0;
foreach ($MyArray as $value) {
$max = max($max,count($value));
}
Try this:
$arr = array();
for ($j=0;$j<=6;$j++) {
if (isset($MyArray[$i*7+$j])) $arr[] = count($MyArray[$i*7+$j]);
}
$result = max($arr);
I don't know exactly what $i refers to though...
// get the interesting part of the array
$chunk = array_intersect_key($input_array, array_flip(range($i*7, $i*7+6)));
// max(count)
$max = $chunk ? max(array_map('count', $chunk)) : 0;