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);
Related
I have an arrays like this
Array
(
[original_data] => Array
(
[0] => Array
(
[reference_id] => 122
[id] => 121
[reference_name] => Dinesh
)
[1] => Array
(
[reference_id] => 123
[id] => 120
[reference_name] => Dinesh Test
)
)
[edited_data] => Array
(
[0] => Array
(
[reference_id] => 123
[id] => 120
[reference_name] => Dinesh Test2
)
)
)
I want to get the difference between this original_data and edited_data arrays. How to do that? I tried using array_diff. But, It didn't work correctly.
You can get the difference =>
$result = ref_array_diff($requestedData['edited_data'], $requestedData['data']);
print_r($result);
function ref_array_diff($arraya, $arrayb) {
foreach ($arraya as $keya => $valuea) {
if (in_array($valuea, $arrayb)) {
unset($arraya[$keya]);
}
}
return $arraya;
}
If you use array_map on both original data and edited data to get an array of original_data id values and edited_data id values. You can then use array_diff with no problem. Using the resulting list of id's which are in one but not the other you can lookup the original value using the array mapped values index key on the original arrays.
I'm making the assumption here that your id key is a unique identifier for each value in the data arrays. If this isn't the case you'll need to do some nested loops to compare deep values
This should work.
<?php
// your data
$data = array (
'original_data' => array
('0' => array
(
'reference_id' => 122,
'id' => 121,
'reference_name' => 'Balasuresh,'
),
'1' => array
(
'reference_id' => 123,
'id' => 120,
'reference_name' => 'Balasuresh',
),
),
'edited_data' => array
('0' => array
(
'reference_id' => 123,
'id' => 120,
'reference_name' => 'Balasuresh Test',
),
)
) ;
// let's get our reference_ids
$originalData = array() ;
foreach($data['original_data'] as $entry) {
$originalData[$entry['reference_id']] = $entry ;
}
// let's compare our edited data to our original data
$editedData = array() ;
foreach($data['edited_data'] as $editedEntry) {
if (isset($originalData[$editedEntry['reference_id']])) {
// for readability
$compare = $originalData[$editedEntry['reference_id']] ;
foreach($editedEntry as $key=>$value) {
if ($compare[$key] != $value) {
$editedData[$editedEntry['reference_id']][$key] = $value ;
}
}
}
}
$editedData now contains your differences
These are multi-dimensional associative arrays, you can recursively do an array_diff_assoc like this:
you can check this code here https://3v4l.org/ovaQB:
function array_diff_assoc_recursive($array1, $array2)
{
foreach($array1 as $key => $value)
{
if(is_array($value))
{
if(!isset($array2[$key]))
{
$difference[$key] = $value;
}
elseif(!is_array($array2[$key]))
{
$difference[$key] = $value;
}
else
{
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if($new_diff != FALSE)
{
$difference[$key] = $new_diff;
}
}
}
elseif(!isset($array2[$key]) || $array2[$key] != $value)
{
$difference[$key] = $value;
}
}
return !isset($difference) ? 0 : $difference;
}
$a=["original_data"=>[
[
'reference_id' => 122,
'id' => 121,
'reference_name' => 'Balasuresh'
],[
'reference_id' => 123,
'id' => 120,
'reference_name' => 'Balasuresh'
]
],
'edited_data' => [[
'reference_id' => 123,
'id' => 120,
'reference_name' => 'Balasuresh Test'
]]
];
print_r(array_diff_assoc_recursive($a['edited_data'], $a['original_data']));
output:
Array
(
[0] => Array
(
[reference_id] => 123
[id] => 120
[reference_name] => Balasuresh Test
)
)
I have this array:
Array
(
[01] => Array
(
[cat_id] => 15
[offset] => 4951
)
[02] => Array
(
[cat_id] => 15
[offset] => 4251
)
[03] => Array
(
[cat_id] => 15
[offset] => 4001
)
[04] => Array
(
[cat_id] => 15
[offset] => 4951
)
[05] => Array
(
[cat_id] => 15
[offset] => 3301
)
)
I have the code to get the key on first level using array_key_exists;
if ((array_key_exists("01", $completed_steps))) {
echo "Found 0!";
}
But I want now to get the cat_id value, how could I do that in a level 2 array?
Use below code, it will find key to n-level depth and search for given key
function multiKeyExists(array $arr, $key) {
// is in base array?
if (array_key_exists($key, $arr)) {
return $arr[$key]['cat_id']; // returned cat_id
}
// check arrays contained in this array
foreach ($arr as $element) {
if (is_array($element)) {
if (multiKeyExists($element, $key)) {
return $element[$key]['cat_id']; // returned cat_id
}
}
}
return false;
}
Try like this:
if ((array_key_exists('cat_id', $completed_steps['01'])) {
echo $completed_steps['01']['cat_id'];
}
You can get particular key-value from it index. See following example:
$check_key = "01";
if ((array_key_exists($check_key, $completed_steps))) {
echo "Found 0! value of cat_id = ".$completed_steps[$check_key]['cat_id'];
}
use the code below
$final_cat_id_array = array();
$key_to_check = 'cat_id';
$catFunc = function($currentArr) use (&$final_cat_id_array, $key_to_check){
if(is_array($currentArr) && array_key_exists($key_to_check, $currentArr)){
$final_cat_id_array[] = $currentArr[$key_to_check];
}
};
$arr = array(
"01" => array("cat_id" => 1, "offset" => true),
"02" => array("cat_id" => 2, "offset" => false),
"03" => array("cat_id" => 3, "offset" => true),
);
array_walk($arr, $catFunc);
print_r($final_cat_id_array);
the final $final_cat_id_array will have all the cat_id.
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);
I have a array like this:
Array
(
[0] => Array
(
[score] => 80
[seen] => 1
)
[1] => Array
(
[score] => 4
[seen] => 1
)
[2] => Array
(
[score] => 4
[seen] => 0
)
[3] => Array
(
[score] => 4
[seen] => 0
)
[4] => Array
(
[score] => 4
[seen] => 0
)
)
I need to check whether is there any [seen] = 1 or not?
if ( /* ??? */ ){
echo "Yes, at least one of [seen] keys is 1";
} else {
echo "No, all [seen] keys are 0";
}
How can I create that condition ?
Using a simple foreach loop can help you
foreach ($array as $arr) {
if ($arr['seen']==1){
echo 'SEEN';
}
}
$seen = false;
foreach($my_array as $el){
if($el['seen']){
$seen = true;
break;
}
}
$seen is true if at least one of [seen] keys is 1
$seen is false if all [seen] keys are 0
Taking a functional approach, you can filter the array using a function that tests for 'seen' == 1. Then if the resulting array is non-empty, you know there are some elements that have 'seen' == 1 values.
<?php
$ar = array(
0 => array(
'score' => 80,
'seen' => 1 ),
1 => array(
'score' => 4,
'seen' => 1 ),
2 => array(
'score' => 4,
'seen' => 0 )
);
if ( array_filter($ar, function($x) { return $x['seen'] == 1; }) ){
echo "Yes, at least one of [seen] keys is 1";
} else {
echo "No, all [seen] keys are 0";
}
?>
Of course, this method is less efficient than those which break out of a loop as soon as a single 'seen' == 1 element is found. But it does give you a single expression in your conditional.
$seen = in_array(1, array_column($array, 'seen'));
Documentation:
array_column()
in_array()
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;
}
}