count of stdClass objects Array in php - php

I have following array of stdClass Object structure and want count number of offers. Std class is received as a response from third party API so it is dynamic.
stdClass Object
(
[Offer] => Array
(
[0] => stdClass Object
(
[Offerid] => 1
[LoanAmount] => 2****
[InterestRate] => 2*
[Term] => 36
[MonthlyPayment] => 7***
[Annualfee] => 0
[OriginationFee] => 1***
)
[1] => stdClass Object
(
[Offerid] => 1
[LoanAmount] => 2****
[InterestRate] => 2*
[Term] => 36
[MonthlyPayment] => 7***
[Annualfee] => 0
[OriginationFee] => 1***
)
[2] => stdClass Object
(
[Offerid] => 1
[LoanAmount] => 2****
[InterestRate] => 2*
[Term] => 36
[MonthlyPayment] => 7***
[Annualfee] => 0
[OriginationFee] => 1***
)
)
)
i want count number of arrays in [Offer], for that i have done following:
echo "count----------".count($offers);
but it gives 1 as a count like
count----------1
in this case count is 3 and i want 3 as output.
Please suggests.
i have also used this
echo "count----------".count((array)$offers);
This also dont works.

You can do it like convert "stdClass Object" into normal array and try after that count($offer);
Just write (array)$object; It will convert as normal array

Count should work.
<?php
$obj = new stdClass();
$obj->Offer = array(
array('Offerid' => 1, 'LoanAmount' => '2***', 'InterestRate' => '2*', 'Term' => 36, 'MonthlyPayment' => '7***', 'Annualfee' => 0, 'OriginationFee' => '1***'),
array('Offerid' => 1, 'LoanAmount' => '2***', 'InterestRate' => '2*', 'Term' => 36, 'MonthlyPayment' => '7***', 'Annualfee' => 0, 'OriginationFee' => '1***'),
array('Offerid' => 1, 'LoanAmount' => '2***', 'InterestRate' => '2*', 'Term' => 36, 'MonthlyPayment' => '7***', 'Annualfee' => 0, 'OriginationFee' => '1***'),
);
echo count($obj->Offer); // Outputs 3
?>
Live example

I have solved my question by following way:
foreach ($offers as $key=> $value)
{
echo "<br/>count->".count($value);
}
this loops itreate only once, and give me result.

Try this :
<?php
class Example {
public $public = 'prop:public';
private $prv = 'prop:private';
protected $prt = 'prop:protected';
}
$arrayobj = new ArrayObject(new Example());
var_dump($arrayobj->count());
$arrayobj = new ArrayObject(array('first','second','third'));
var_dump($arrayobj->count());
?>
The above example will output:
int(1)
int(3)
Answer Source

Related

array_multisort sort by order of appearance of assisting array

I'd like your help with this tough situation I'm in. Suppose the following array $_xm_anal that holds the sizes/availability info of a product:
Array
(
[availability] => Array
(
[BLK] => Array
(
[SIZE4] => 2
[SIZE8] => 1
[SIZE10] => 3
[SIZE14] => 8
[SIZE16] => 4
[SIZE19] => 12
[SIZE24] => 1
)
)
[color_list] => Array
(
[BLK] => Array
(
[0] => BLK
[COLORCODE] => BLK
[1] => BLACK
[ColorLANG] => BLACK
)
)
[size_list] => Array
(
[ID] => 20
[SIZE1] => 39
[SIZE2] => 40
[SIZE3] => 40.5
[SIZE4] => 41
[SIZE5] => 41.5
[SIZE6] => 42
[SIZE7] => 42.5
[SIZE8] => 43
[SIZE9] => 43.5
[SIZE10] => 44
[SIZE11] => 44.5
[SIZE12] => 45
[SIZE13] => 45.5
[SIZE14] => 46
[SIZE15] => 46.5
[SIZE16] => 47
[SIZE17] => 47.5
[SIZE18] => 48
[SIZE19] => 48.5
[SIZE20] => 49
[SIZE21] => 36
[SIZE22] => 37
[SIZE23] => 37.5
[SIZE24] => 38
[SIZE25] => 38.5
[TitleLANG] => NO NO NO
)
[text_size] => Array
(
[SIZE4] => 41
[SIZE8] => 43
[SIZE10] => 44
[SIZE14] => 46
[SIZE16] => 47
[SIZE19] => 48.5
[SIZE24] => 38
)
)
The PHP code that translates the above array into the appropriate size options is the one below:
foreach ($_xm_anal["availability"][$Prod["color_code"]] as $k => $v) { ?> // $Prod["color_code"] equals to BLK in this example
<? if ($v) {
$hasSize = 1; ?>
<li> <?= $_xm_anal["size_list"][$k] ?></li>
<? } else { ?>
<li style="background:#f0eee8"> <s><?= $_xm_anal["size_list"][$k] ?></s></li>
<? } ?>
<? } ?>
And the final outcome looks like this:
The problem started when they added sizes in an unsorted manner, ie size 38 corresponds to key SIZE24, so $_xm_anal['availability']['BLK'] needs to be sorted in the appropriate order according to its keys, which can be determined by $_xm_anal['text_size']
So I wrote the code below to first create an array of the correct order of $_xm_anal['availability']['BLK'] keys:
$tmp = $_xm_anal['text_size'];
asort($tmp);
$tmp2 = array_keys($tmp);
This produces the array $tmp2 that equals to:
Array
(
[0] => SIZE24
[1] => SIZE4
[2] => SIZE8
[3] => SIZE10
[4] => SIZE14
[5] => SIZE16
[6] => SIZE19
)
So the only problem now is that I can't find the correct flag to use in the appropriate array_multisort to sort out the initial $_xm_anal array based on that $tmp2 array...
array_multisort($tmp2, SORT_NATURAL, $_xm_anal["availability"][$Prod["color_code"]]);
What is the correct flag to use to actually use the $tmp2 in the order its keys are met?
OK, I was able to sort it a bit indirectly/less elegant, but I couldn't think of any better solution... So I did this:
$tmp = $_xm_anal['text_size'];
asort($tmp);
foreach ($tmp as $kkk => $vvv) {
$tmp[$kkk]=$_xm_anal["availability"][$Prod["color_code"]][$kkk];
}
$_xm_anal["availability"][$Prod["color_code"]] = $tmp;
If anyone can think of a more elegant solution, please feel free to post it! TIA

PayPal recurring history convert to specific array format

I am getting following history from PayPal recurring profile. Please help me to convert into array. I need to verify "P_TRANSTATE" value to every month. It's really hard to check through following array. Please suggest me or help me to convert array to the following view.
[status] => 1
[result] => Array
(
[HTTP/1_1_200_OK ... RESULT] => 0
[RPREF] => RGX51B669592
[PROFILEID] => 0
[P_PNREF1] => BL0PEE6F2E98
[P_TRANSTIME1] => 25-Aug-17 04:46 AM
[P_RESULT1] => 0
[P_TENDER1] => C
[P_AMT1] => 19.99
[P_TRANSTATE1] => 8
[P_PNREF2] => BP0PECB1799B
[P_TRANSTIME2] => 24-Sep-17 04:58 AM
[P_RESULT2] => 0
[P_TENDER2] => C
[P_AMT2] => 19.99
[P_TRANSTATE2] => 8
);
Need to following format
1] => Array
(
[P_PNREF] => BL0PEE6F2E98
[P_TRANSTIME] => 25-Aug-17 04:46 AM
[P_RESULT] => 0
[P_TENDER] => C
[P_AMT] => 19.99
[P_TRANSTATE] => 8
[2] => Array
(
[P_PNREF] => BP0PECB1799B
[P_TRANSTIME] => 24-Sep-17 04:58 AM
[P_RESULT] => 0
[P_TENDER] => C
[P_AMT] => 19.99
[P_TRANSTATE] => 8 );
If I don't misunderstood your question then this should work for you.
<?php
$array = array
(
'HTTP/1_1_200_OK ... RESULT' => 0,
'RPREF' => 'RGX51B669592',
'PROFILEID' => 'RP0000000040',
'P_PNREF1' => 'BQ1PECD4AEB8',
'P_TRANSTIME1' => '25-Aug-17 04:46 AM',
'P_RESULT1' => 0,
'P_TENDER1' => 'C',
'P_AMT1' => 19.99,
'P_TRANSTATE1' => 8,
'P_PNREF2' => 'BT1PFFF8A110',
'P_TRANSTIME2' => '24-Sep-17 04:58 AM',
'P_RESULT2' => 0,
'P_TENDER2' => 'C',
'P_AMT2' => 19.99,
'P_TRANSTATE2' => 8,
);
unset($array['HTTP/1_1_200_OK ... RESULT'],$array['RPREF'],$array['PROFILEID']);
$final_array = [];
foreach($array as $key=>$value){
$index = substr($key, -1);
$key = substr($key, 0, -1);
$final_array[$index][$key] = $value;
}
print '<pre>';
print_r($final_array);
print '</pre>';
?>
Output:
Array
(
[1] => Array
(
[P_PNREF] => BQ1PECD4AEB8
[P_TRANSTIME] => 25-Aug-17 04:46 AM
[P_RESULT] => 0
[P_TENDER] => C
[P_AMT] => 19.99
[P_TRANSTATE] => 8
)
[2] => Array
(
[P_PNREF] => BT1PFFF8A110
[P_TRANSTIME] => 24-Sep-17 04:58 AM
[P_RESULT] => 0
[P_TENDER] => C
[P_AMT] => 19.99
[P_TRANSTATE] => 8
)
)
DEMO: https://eval.in/978871
As per the comment of Parapluie: You can use preg_match() to get keys/index when it cross the digit after 9 because substr($key,-1) or substr($key,0,-1) will not work properly then. See below-
$re = '/(\D+)(\d+)/';
foreach($array as $key=>$value){
preg_match($re, $str, $matches);
$index = $matches[2];
$key = $matches[1];
$final_array[$index][$key] = $value;
}
You can do this, assuming $res is the paypal array response.
<?php
function convertArray($arr){
$temp = array();
$temp[1] = $arr['result'];
return $temp;
}
print_r(convertArray(r$es));
?>

find max value of a key in array

I have an array
[DoctorEducation] => Array
(
[0] => Array
(
[id] => 24
[user_id] => 91
[degree_type_id] => 1
[college_hospital] => sms
[diploma_name] =>
[specialization_id] => 0
[start_date] => 02/2009
[end_date] => 03/2012
[year_passing] => 0000
[created] => 2015-10-09 13:14:23
[updated] => 2015-10-09 13:16:18
)
[1] => Array
(
[id] => 26
[user_id] => 91
[degree_type_id] => 5
[college_hospital] => sms
[diploma_name] =>
[specialization_id] => 48
[start_date] => 03/2012
[end_date] => 05/2014
[year_passing] => 0000
[created] => 2015-10-09 13:16:18
[updated] => 2015-10-09 13:16:18
)
)
Now I want to find out which index i.e 0 or 1 or so on have the biggest value of degree_type_id. for example in current array index 1 has the biggest value of degree_type_id i.e 5.
I am getting this from DB. Here is the query
$fields = array(
'User.id',
'User.first_name',
'User.last_name',
'User.gender',
'User.dob',
'User.image',
'Specialization.name',
'User.age'
);
$getSpecialist = $this->User->find('all', array('fields' => $fields), array('conditions' => array('User.role_id' => 3, 'User.status' => 1)));
Try something like this:
$max_index = null;
$max_value = 0;
foreach($DoctorEducation as $key => $array){
if($array['degree_type_id'] > $max_value){
$max_value = $array['degree_type_id'];
$max_index = $key;
}
}
print_r($DoctorEducation[$max_index]);
This gives you the index and the value of the key with the highest degree_type_id
There may be a faster way to do this, but here's my solution:
$to_compare = array();
foreach($DoctorEducation as $idx => $arr){
$to_compare[$idx] = $arr['degree_type_id'];
}
$max = $to_compare[array_search(max($to_compare), $to_compare)];
Pretty simple with several functions:
$key = array_search(max(array_column($array['DoctorEducation'], 'degree_type_id')),
$array['DoctorEducation']);
Extract the appropriate column to an array
Find the maximum value in that column array
Search that column array for the maximum value, which returns the key
If there is more than one occurrence of the maximum value then you will get the first one.

updating php multidimensional array and resaving to db

I have the following structure for my array which is dynamically generated:
Array
(
[0] => Array
(
[friend_request_sender_id] => 1
[friend_request_date] => 07-08-2014
[friend_request_time] => 11:12:19
[friend_request_recipient_id] => 5
[friend_request_sent] => 1
[friend_request_accepted] => 0
)
[1] => Array
(
[friend_request_sender_id] => 1
[friend_request_date] => 07-08-2014
[friend_request_time] => 11:12:47
[friend_request_recipient_id] => 2
[friend_request_sent] => 1
[friend_request_accepted] => 0
)
)
I would like to update the first array value [friend_request_accepted] => 0 to this: [friend_request_accepted] => 1 which i have achieved by doing the following:
$test = get_user_meta($current_user->ID,'get_user_friends', true );
foreach($test as &$values){
if($values['friend_request_recipient_id'] === '5'){
$values['friend_request_accepted'] = '1';
break; // Stop the loop after we've found the item
}
}
However, i would like to actually save this new value into the database, overwriting the existing value. The array should then look like this:
Array
(
[0] => Array
(
[friend_request_sender_id] => 1
[friend_request_date] => 07-08-2014
[friend_request_time] => 11:12:19
[friend_request_recipient_id] => 5
[friend_request_sent] => 1
[friend_request_accepted] => 1
)
[1] => Array
(
[friend_request_sender_id] => 1
[friend_request_date] => 07-08-2014
[friend_request_time] => 11:12:47
[friend_request_recipient_id] => 2
[friend_request_sent] => 1
[friend_request_accepted] => 0
)
)
How should I do this?
this line doesn't work:
if($values['friend_request_recipient_id'] === '5'){
$values['friend_request_accepted'] = '1';
break; // Stop the loop after we've found the item
}
because
[friend_request_recipient_id] => 5
is an integer I think you just need to sure in condition because '===' is also checked the types of variable.

Adding Elements To An StdClass Array - PHP / Codeigniter

I have an array that is an stdClass. The output of that array is as follows :
Array
(
[0] => stdClass Object
(
[vendor_id] => 1
[user_id] => 1
[date_created] => 2013-06-12 16:48:38
[date_edited] =>
[status] => active
[user_firstname] => Stuart
[user_surname] => Blackett
)
)
What I would like to do is add two variables to this stdClass. They are "total_bookings" and "total_venues";
I currently am looping through the results and then getting a count to create the total. I would like to add those two vars to the end of that stdClass array.
My PHP is as follows :
$vendors = $this->po_model->get_all_vendors();
$this->template->set('total_vendors', count($vendors));
$count = 0;
foreach($vendors as $vendor)
{
$count++;
$total_venues = $this->po_model->get_count_venues($vendor->user_id);
$total_bookings = $this->po_model->get_count_bookings($vendor->user_id);
$vendors[$count]['total_venues'] = $total_venues;
$vendors[$count]['total_bookings'] = $total_bookings;
}
However, When I var_dump that my Array looks like this :
Array
(
[0] => stdClass Object
(
[vendor_id] => 1
[user_id] => 1
[date_created] => 2013-06-12 16:48:38
[date_edited] =>
[status] => active
[user_firstname] => Stuart
[user_surname] => Blackett
)
[1] => Array
(
[total_venues] => 6
[total_bookings] => 14
)
)
So my question is, How do I add total_venues and total_bookings to that stdClass()?
Thanks
$myArray[$indexOfObject]->total_venues = 6;
$myArray[$indexOfObject]->total_bookings= 14;
Your example:
foreach($vendors as $key => $vendor)
{
$total_venues = $this->po_model->get_count_venues($vendor->user_id);
$total_bookings = $this->po_model->get_count_bookings($vendor->user_id);
$vendors[$key]->total_venues = $total_venues;
$vendors[$key]->total_bookings = $total_bookings;
}
its an object, you should use object notations, not array notations. also change your move your count++ below these two instructions
$vendors[$count]->total_venues = $total_venues;
$vendors[$count]->total_bookings = $total_bookings;
$count++;

Categories