How do i Print the object value? (php) - php

I'm a newbie in PHP object. so I want to make random forest classification. I have a problem to print out the tree decision tree in my classification.
in this case my result classification :
[tree:protected] => Phpml\Classification\DecisionTree\DecisionTreeLeaf Object
(
[value] => <= 3.45
[numericValue] => 3.45
[operator] => <=
[columnIndex] => 1
[leftLeaf] =>
[rightLeaf] =>
[records] => Array
(
[0] => 0
[1] => 1
[2] => 2
)
[classValue] => late
[isTerminal] => 1
[isContinuous] => 1
[giniIndex] => 0.44444444444444
[level] => 0
)
how can I just print 3.45, the colomindex=1, late, the gini index, and the level?
I want to use that value to build a decision tree.

I'm not sure if this answers your question but from my understanding, you would need to create an array like so: $tree = ["key" => "value"]; and then echo $tree["key"];

Related

finding the lowest value integer from data stored in an array using php

I have an array called $quotes and when I print_r an example of the results are like this:
Array ( [id] => advshipper [methods] => Array (
[0] => Array ( [id] => 2-0-0 [title] => Small Parcels [cost] => 4.5 [icon] => [shipping_ts] => [quote_i] => 0 )
[1] => Array ( [id] => 3-0-0 [title] => Large Parcels up to 1150mm long [cost] => 8.95 [icon] => [shipping_ts] => [quote_i] => 1 )
[2] => Array ( [id] => 4-0-0 [title] => Extra Large Parcels over 1150mm long [cost] => 15 [icon] => [shipping_ts] => [quote_i] => 2 ) ) [module] => Shipping )
What I need is a simple way to look at $quotes, find which [cost] has the lowest value and then record, in this example, the [0] so that they can be used in another code segment.
Short of exploding the array and then looping through all the content, is there a simple method to achieve what I want?
As you are using the version higher than 5.5, you can simply use array_column function:
echo min(array_column($quotes['id'],'cost'));
And if you want, you can retrieve the id of the row as well:
echo min(array_column($quotes['id'], 'cost', 'id'));
array_reduce($quotes, function($minimum, $current) {
return $current['cost'] < $minimum['cost'] ? $current : $minimum;
}, $quotes[0]);
This will return the row of $quotes that has the lowest value of cost.
If I understood that correctly, you need to find the lowest value for cost array.
You can do an array_map to get the values
$lowestcost = array_map(function($costval) {
return $costval['cost'];
}, $array);
Then you need to use
min($lowestcost)
Hope this helps

Echo value from PHP array when brackets in key-value pairs

Quick question, how do you echo a variable when the array has key's that contain names with brackets etc. See array below:
[data] => Array
(
[0] => stdClass Object
(
[id] => 4
[contact_id] =>
[status] => Complete
[is_test_data] => 0
[datesubmitted] => 2014-04-16 22:18:39
[sResponseComment] =>
[responseID] => 4
[[question(1), option(0)]] =>
[[question(2)]] => John
[[question(3)]] => Wtf#wtf.com
[[question(5)]] => 0975735289
[[question(6)]] => 3010
[[question(9), option(0)]] =>
[[question(10)]] => Testing
[[question(11)]] => Later
[[question(12)]] => This year
)
Lets say i want to echo the answer to question 2, which is "John". How do i go about doing this?
Well you have an array conataining an array of objects:
echo $array['data'][0]->{'[question(2)]'};

PhP MultiDimensional Array fetching

I am not really familiar on how php handles array, in .NET I can access array using this method
array[x][y];
My question is:
I am retrieving records from the database and returning it to the $res_merchant_field
$res_merchant_field = $this->CI->merchantfield_model->merchantfield_list( $str_where );
and $res_merchant_field will be populated with this record:
Array
(
[0] => stdClass Object
(
[MFID] => 1
[MFName] => Bill No
[FTID] => 1
[DTID] => 1
[MFRequired] => 1
[MFDefaultValue] =>
[MFDueDate] => 0
[MFToBePaid] => 0
[MFMaxLength] => 12
[MFOrderNo] => 1
[MFStatus] => 1
)
[1] => stdClass Object
(
[MFID] => 2
[MFName] => Gallons Consumed
[FTID] => 1
[DTID] => 2
[MFRequired] => 1
[MFDefaultValue] =>
[MFDueDate] => 0
[MFToBePaid] => 0
[MFMaxLength] => 5
[MFOrderNo] => 2
[MFStatus] => 1
)
[2] => stdClass Object
(
[MFID] => 3
[MFName] => Amount Due
[FTID] => 3
[DTID] => 1
[MFRequired] => 1
[MFDefaultValue] =>
[MFDueDate] => 0
[MFToBePaid] => 1
[MFMaxLength] => 15
[MFOrderNo] => 3
[MFStatus] => 1
)
)
How can I access and fetch the record from that array with this condition:
it will look through all the array find specific index, lets say index 0 which is MFID,
after getting the MFID and comparing it with another variable, if it is true,
it will get the DTID for that array MFID.
example:
get MFID = 1, the DTID will be 1, if I get the MFID = 3, the DTID will be 1.
or how can I access the array like $array[x][y]?
Thanks in advance.
The problem is that the second level is not an array but instead an object, to access a property you will have to use this format.
$array[$x]->$y;
Unfortunately you cannot access a property by an index do o get the MFID of the 0th Item you will need to say
$array[0]->MFID;

complex multidimentional associative array process with foreach

I've had to ask this one again, sorry, but I'm having a problem trying to process this array. I have tried several different ways but none where right, here's the array:
Array (
[search] => Array (
[response] => Array (
[errors] =>
[number_of_hotels] => 1 of 1
)
[lr_rates] => Array (
[hotel] => Array (
[hotel_ref] => 3116
[hotel_currency] => [U] => USD
[hotel_rooms] => Array (
[room] => Array (
[ref] => 6382
[type] => 1
[type_description] => Standard
[sleeps] => 8
[rooms_available] =>
[adults] => 8
[children] =>
[breakfast] => false
[dinner] => false
[description] =>
[alternate_description] =>
[rack_rate] => 82.01
[date] => 19/08/201220/08/201221/08/2012
[numeric_hotelcurrencyprice] => FullFullFull
[formatted_date] => 19 August 201220 August 201221 August 2012
[price] => FullFullFull
[hotelcurrencyprice] => FullFullFull
[numeric_price] => FullFullFull
[requested_currency] => GBPGBPGBP
[numeric_hotelcurrencyprice] => FullFullFull
[available_online] => false
[minimum_nights] => 1
[bed_type] =>
[cancellation_policy] =>
[cancellation_days] =>
[cancellation_hours] =>
[room_terms] =>
)
[room] => Array (
[ref] => 6382
[type] => 1
[type_description] => Standard
[sleeps] => 8
[rooms_available] =>
[adults] => 8
[children] =>
[breakfast] => false
[dinner] => false
[description] =>
[alternate_description] =>
[rack_rate] => 82.01
[date] => 19/08/201220/08/201221/08/2012
[numeric_hotelcurrencyprice] => FullFullFull
[formatted_date] => 19 August 201220 August 201221 August 2012
[price] => FullFullFull
[hotelcurrencyprice] => FullFullFull
[numeric_price] => FullFullFull
[requested_currency] => GBPGBPGBP
[numeric_hotelcurrencyprice] => FullFullFull
[available_online] => false
[minimum_nights] => 1
[bed_type] =>
[cancellation_policy] =>
[cancellation_days] =>
[cancellation_hours] =>
[room_terms] =>
)
)
[cancellation_type] => First Night Stay Chargeable
[cancellation_policy] => 2 Days Prior to Arrival
[CityTax] => Array (
[TypeName] =>
[Value] =>
[OptedIn] =>
[IsCityTaxArea] =>
)
)
)
)
)
Ok, I need to traverse the array and create a loop, so for every instance of ROOM it will repeat the process. Then i need to extract the data from room array and use it to populate rows in MySQL for each instance of room.
This is the code I have so far which prints the names and values in the room array. However, it only gets one of the room arrays. What can I do to read all of the rooms? I am also thinking this is too many for-each but don't seem to be able to traverse down ['']['']['']...
or by just using the associative name.
foreach($arr['search'] as $lr_rates) {
foreach($lr_rates['hotel'] as $hotel) {
foreach($hotel['room'] as $field => $value){
print $field;print $value;
}
}
}
It might also be worth mentioning the values in these arrays are always fluctuating.
I think you can really simplify this quote a bit. If you know that this will always be the structure then you can jump right down into the hotels and then into the rooms.
foreach($arr['search']['lr_rates']['hotel'] as $hotel) {
// You can access all of the keys in the hotel array here
foreach($hotel['hotel_rooms'] as $room) {
// Do stuff with the room array
}
}
I would recommend either building your insert script on the fly and calling the database just once for the write, or if you are updating then using a transaction. As the number of rooms gets larger you will slow your script down with a bunch of writes to disk.
the formatting of your data's output is very bad and unreadable. I cannot really identify what you are trying to do.
possibility: the inner array [hotel_rooms] => Array () uses the key room multiple times. as array keys are unique, you overwrite the data at the index room. this is why you only get one room.
possibility: there are rooms inside a room -> use a recursive function to iterate over all rooms like this:
function handleRoom(array $room) {
// do something with $room
if (array_key_exists('room', $room)) {
handleRoom($room['room']);
}
}
$room = array(
'some' => 'room',
'data' => 'and another',
'room' => array(
'is' => 'inside',
'of the' => 'main room',
),
);
handleRoom($room);

Sort MPTT resultset into a multidimensional array PHP

I have been experimenting with the Modified Pre-Order Tree Traversal
Pattern, my test case code is returning the results as expected however I am
having trouble converting the 2D array into a multi-dimensional array to present it.
Here is an example of a 3 level menu result, I need to convert this into a multi-dimensional array so that I can iterate it in TAL:
Array
(
[0] => Array
(
[CategoryID] => 1
[ParentID] => 0
[CategoryName] => Default Parent
[lt] => 1
[rt] => 14
[tree_depth] => 1
)
[1] => Array
(
[CategoryID] => 8
[ParentID] => 1
[CategoryName] => SysAdmin
[lt] => 2
[rt] => 7
[tree_depth] => 2
)
[2] => Array
(
[CategoryID] => 2
[ParentID] => 8
[CategoryName] => Linux
[lt] => 3
[rt] => 4
[tree_depth] => 3
)
[3] => Array
(
[CategoryID] => 3
[ParentID] => 8
[CategoryName] => Windows
[lt] => 5
[rt] => 6
[tree_depth] => 3
)
[4] => Array
(
[CategoryID] => 5
[ParentID] => 1
[CategoryName] => Code
[lt] => 8
[rt] => 13
[tree_depth] => 2
)
[5] => Array
(
[CategoryID] => 6
[ParentID] => 5
[CategoryName] => PHP
[lt] => 9
[rt] => 10
[tree_depth] => 3
)
[6] => Array
(
[CategoryID] => 7
[ParentID] => 5
[CategoryName] => Perl
[lt] => 11
[rt] => 12
[tree_depth] => 3
)
)
I need to structure the data so that every parent has a 'Children' key which is an array of arrays repeated, with no limitation on the amount of children a parent/child/grandchild can have, the tree_depth key is worked out automatically by the DBMS, so I simply need to alter the structure of the array.
Any pointers greatly appreciated, I have played with usort() and array_walk_recursive to no avail.
Thanks in advance
I think a simple foreach can do the trick here (with the help of references):
Set up a $menu associative array $cat_id => $element_details_anb_children:
$menu = array(); $ref = array();
foreach( $tree as $d ) {
$d['children'] = array();
if( isset( $ref[ $d['ParentID'] ] ) ) { // we have a reference on its parent
$ref[ $d['ParentID'] ]['children'][ $d['CategoryID'] ] = $d;
$ref[ $d['CategoryID'] ] =& $ref[ $d['ParentID'] ]['children'][ $d['CategoryID'] ];
} else { // we don't have a reference on its parent => put it a root level
$menu[ $d['CategoryID'] ] = $d;
$ref[ $d['CategoryID'] ] =& $menu[ $d['CategoryID'] ];
}
}
This should build two arrays: the multidimensional array you want ($menu) and a flat array which only holds references for each category. On each iteration it nests the category into its parent if it already exists (which is why I keep the reference table). Of course it works only if your initial $tree array is ordered (i.e. the parent comes before its children).

Categories