So this is an example:
Array (
[0] => Array ( [title] => Title_1 [checkout] => 1 [no_gateway] => 0 )
[1] => Array ( [title] => Title_2 [checkout] => 1 [no_gateway] => 1 )
[2] => Array ( [title] => Title_3 [checkout] => 0 [no_gateway] => 0 )
[3] => Array ( [title] => Title_4 [checkout] => 1 [no_gateway] => 1 )
[4] => Array ( [title] => Title_5 [checkout] => 0 [no_gateway] => 0 )
[5] => Array ( [title] => Title_6 [checkout] => 1 [no_gateway] => 0 )
)
I need to print out all values under [title] key having [checkout] => 1 & [no_gateway] => 0
In my case it should looks like
Title_1
Title_6
Please help php-beginner :) Thanks!
foreach($array as $row) {
if ($row['checkout'] && !$row['no_gateway']) {
print $row['title'];
}
}
foreach ($items as $item) {
if($item['checkout'] == 1 && $item['no_gateway'] == 0) {
echo $item['title'];
}
}
assuming your array is called $items
print_r(
array_map(function ($a) { return $a["title"]; },
array_filter($original,
function ($a) { return $a["checkout"] && !$a["no_gateway"]; }
)
)
);
You tagged the question with the answer: foreach
// assuming $arr is the array containing the values from the example
foreach ($arr as $record) {
if ($record['checkout'] && !$record['no_gateway']) {
echo $record['title'], "\n";
}
}
foreach( $array as $value ) {
if( $value["checkout"] == 1 && $value["no_gateway"] == 0 ) {
print $value["title"].PHP_EOL;
}
}
Related
I have this array:
Array
(
[8008] => Array
(
[main_mob_vnum] => 8008
[0] => Array
(
[drop_mob_vnum] => 50300
[drop_mob_count] => 2
[drop_mob_percent] => 110.00
)
[1] => Array
(
[drop_mob_vnum] => 39030
[drop_mob_count] => 2
[drop_mob_percent] => 85.00
)
)
[8009] => Array
(
[main_mob_vnum] => 8009
[0] => Array
(
[drop_mob_vnum] => 50300
[drop_mob_count] => 4
[drop_mob_percent] => 110.00
)
[1] => Array
(
[drop_mob_vnum] => 50125
[drop_mob_count] => 1
[drop_mob_percent] => 75.00
)
)
[8010] => Array
(
[main_mob_vnum] => 8009
[0] => Array
(
[drop_mob_vnum] => 50125
[drop_mob_count] => 4
[drop_mob_percent] => 110.00
)
)
)
I want to make a function like
function searchArray($array,$vnum)
if $vnum is == with drop_mob_vnum take the previously array and put all this array keys on new array
For example, searchArray($array,50300) should return an array like: array(8008,8009) because 8010 doesn't have a drop_mob_vnum == 50300
I know StackOverflow requires that I post my problem with code, but I don't know where to start. If someone could help me, I would appreciate it. Thank you!
This function should do the trick:
function find_vnum($array,$value)
{
$result = array();
foreach ($array AS $k1 => $v1)
{
foreach ($v1 AS $k2 => $v2)
{
if ($k2 === 'main_mob_vnum') {
continue;
}
foreach ($v2 AS $k3 => $v3)
{
if ($k3 !== 'drop_mob_vnum') {
continue;
}
if (($v3 === $value) && !in_array($k1, $result)) {
$result[] = $k1;
}
}
}
}
return $result;
}
Given your sample data:
print_r(find_vnum($data,50300));
// Array
// (
// [0] => 8008
// [1] => 8009
// )
You can see a working demo here.
I have this Array
Array
(
[2014-08-14] => Array
(
[18:00:00] => Array
(
[6] => Array
(
[price] => 15.36
[avail_clean] => 5
[avail_noclean] => 6
)
[7] => Array
(
[price] => 17.35
[avail_clean] => 2
[avail_noclean] => 3
)
)
[19:00:00] => Array
(
[6] => Array
(
[price] => 15.36
[avail_clean] => 5
[avail_noclean] => 6
)
[7] => Array
(
[price] => 17.35
[avail_clean] => 2
[avail_noclean] => 3
)
)
)
)
How can I get the following for 6 & 7 seperately:
- Sum of price
- max of avail_clean
- max of avail_noclean
I got that far for the price:
foreach ($bookable as $date=>$key) {
foreach ($key as $time=>$key2) {
foreach($key2 as $room=>$key3){
foreach($key3 as $price=>$key4){
if($price == "price"){
if(isset($sumRoom[$room]['total'])){
$sumRoom[$room]['total'] += $key4;
}else{
$sumRoom[$room]['total'] = $key4;
}
}
}
}
}
}
Gives me this
Array(
[6] => Array
(
[total] => 30,72
)
[7] => Array
(
[total] => 34,7
)
)
But what about the max(), where should I put that?
foreach ($bookable as $date=>$times) {
foreach ($times as $time=>$rooms) {
foreach($rooms as $room=>$options){ $sumRoom[$room]['total'] = 0;
foreach($options as $option=>$value){
if($option == "price"){
$sumRoom[$room]['total'] += $value;
}
if($option == "avail_clean"){
$avail_clean[$room][] = $value;
}
if($option == "avail_noclean"){
$avail_noclean[$room][] = $value;
}
}
$sumRoom[$room]['avail_clean_max'] = max($avail_clean[$room]);
$sumRoom[$room]['avail_noclean'] = max($avail_noclean[$room]);
}
}
}
I've edited my answer.. I've merged all in one array i.e. $sumRoom
I have an object array that looks like this:
Array
(
[0] =>Object
(
[ClassScheduleID] => 2263
[Name] => Workout 1
[Location] => Object
(
[BusinessID] => 1
)
)
[1] =>Object
(
[ClassScheduleID] => 2263
[Name] => Workout 1
[Location] => Object
(
[BusinessID] => 13
)
)
[2] =>Object
(
[ClassScheduleID] => 2264
[Name] => Workout 2
[Location] => Object
(
[BusinessID] => 22
)
)
I am looking to identify that the ClassScheduleID of 2263 is a duplicate, and remove the duplicate entry's entire object from the array. So that I get:
Array
(
[0] =>Object
(
[ClassScheduleID] => 2263
[Name] => Workout 1
[Location] => Object
(
[BusinessID] => 1
)
)
[1] =>Object
(
[ClassScheduleID] => 2264
[Name] => Workout 2
[Location] => Object
(
[BusinessID] => 22
)
)
I tried the solution proposed here
How to remove duplicate values from a multi-dimensional array in PHP
but the count() remained the same
$count = 0;
foreach($arrayObj as $key => $value) {
if ($value['ClassScheduleID'] == 2263) {
$count++;
}
if ($count > 1){
unset($arrayObj[$key]);
$count--;
}
}
It works: http://ideone.com/fork/zrdDtu
Edit: Modified to delete any duplicates:
foreach($arrayObj as $key => $value) {
$count = 0;
foreach($arrayObj as $nkey => $nvalue) {
if ($value['ClassScheduleID'] == $nvalue['ClassScheduleID']) {
$count++;
}
if ($count > 1){
unset($arrayObj[$key]);
$count--;
}
}
}
var_dump($arrayObj);
See it here: http://ideone.com/fork/85RCst
function get_unique_array($array)
{
$result = array_map("unserialize", array_unique(array_map("serialize", $array)));
foreach ($result as $key => $value)
{
if ( is_array($value) )
{
$result[$key] = get_unique_array($value);
}
}
return $result;
}
Demo: http://3v4l.org/WOTHi#v430
In array count all true where category is 1 after counting the category will move to 2 thn count all true and so on?
Array generated:
foreach($_POST as $key => $data)
{
$a = explode("_", $key);
$f = array(
'category' => $a[1],
'answer' => $data
);
$f_data[] = $f;
}
FROM
Array
(
[0] => Array
(
)
[1] => Array
(
[category] => 1
[answer] => true
)
[2] => Array
(
[category] => 1
[answer] => true
)
[3] => Array
(
[category] => 1
[answer] => true
)
[4] => Array
(
[category] => 1
[answer] => false
)
[5] => Array
(
[category] => 1
[answer] => false
)
[6] => Array
(
[category] => 1
[answer] => true
)
[7] => Array
(
[category] => 1
[answer] => true
)
[8] => Array
(
[category] => 2
[answer] => true
)
[9] => Array
(
[category] => 2
[answer] => true
)
[10] => Array
(
[category] => 2
[answer] => true
)
[11] => Array
(
[category] => 2
[answer] => false
)
[12] => Array
(
[category] => 2
[answer] => true
)
[13] => Array
(
[category] => 2
[answer] => true
)
)
To
array (
category: 1,
count: 5
),
array(
category: 2,
count: 5
)
This is the solution :
$return = array();
foreach($cont as $item) {
if($item['answer'] == 'true') {
$verify = false;
foreach($return as $key => $value) {
if($value['category'] == $item['category']) {
$return[$key]['count']++;
$verify = true;
break;
}
}
if(!$verify)
$return[] = array('category' => $item['category'], 'count' => 1);
}
}
And this is better solution...
$return = array();
foreach($cont as $item) {
if($item['answer'] == 'true') {
if(array_key_exists($item['category'], $return))
$return[$item['category']]++;
else
$return[$item['category']] = 1;
}
}
Untested, but I believe this is what you are after (pending any minor mistakes):
foreach($_POST as $key => $data)
{
$a = explode("_", $key);
$f[$a[1]]['category'] = $a[1];
if ($data == "true") {
$f[$a[1]]['answer']++;
}
}
So i have this array:
Array
(
[0] => Array
(
[Page] => Array
(
[id] => 2
)
[PageRevision] => Array
(
[PageId] => 1
[PageRevisionId] => 3
[goldMaster] => 0
)
[PageLanguage] => Array
(
[name] => Contact Page
)
[PageSetting] => Array
(
[url] => contact2
)
)
[1] => Array
(
[Page] => Array
(
[id] => 2
)
[PageRevision] => Array
(
[PageId] => 1
[PageRevisionId] => 2
[goldMaster] => 1
)
[PageLanguage] => Array
(
[name] => Contact Page 2
)
[PageSetting] => Array
(
[url] => contact
)
)
)
What i need to do is determine , out of the two array given, if one of them has a higher PageRevisionId than the other one and if it's goldMaster is set to 0. But i am struggling to find any method to do this.
if ($array[0]['PageRevision']['PageRevisionId'] > $array[1]['PageRevision']['PageRevisionId']
&& $array[0]['PageRevision']['goldMaster'] == 0) {
// your code
}
You can use this code with an unlimited number of element in you main array:
$max_pageRevisionId = 0;
$max_goldMaster = 0;
foreach($myarray as $key => $value) {
if($value['PageRevision']['PageRevisionId'] > $max_pageRevisionId) {
$max_pageRevisionId = $value['PageRevision']['PageRevisionId'];
$max_goldMaster = $value['PageRevision']['goldMaster'];
}
}
if($max_goldMaster > 0) {
// Do something
} else {
// Do something else
}
$elem = ($array [0]['PageRevision']['PageRevisionId'] > $array [1]['PageRevision']['PageRevisionId'])?
1:
0;
var_dump ($array [0]['PageRevision']['goldMaster '] == 0)