Extracting a property from an array of an array of objects - php

I've got an object, containing an array of objects, containing an array of values:
stdClass Object (
[devices] => Array (
[0] => stdClass Object (
[location] => 1
[delegate] =>
[type] => 1
[id] => 1234
[IP] => 1.2.3.4
[name] => host1
[owner] => user6
[security] => 15
)
[1] => stdClass Object (
[location] => 2
[delegate] =>
[type] => 1
[id] => 4321
[IP] => 4.3.2.1
[name] => host2
[owner] => user9
[security] => 15
)
)
)
I want to extract just the id and name into an array in the form of:
$devices['id'] = $name;
I considered using the array_map() function, but couldn't work out how to use it... Any ideas?

This will generate you a new array like I think you want
I know you says that delegate is an object but the print does not show it that way
$new = array();
foreach($obj->devices as $device) {
$new[][$device->id] = $device->name;
}

Would something like this not work? Untested but it cycles through the object structure to extract what I think you need.
$devices = myfunction($my_object);
function myfunction($ob){
$devices = array();
if(isset($ob->devices)){
foreach($ob->devices as $d){
if(isset($d->delegate->name && isset($d->delegate->id))){
$devices[$d->delegate->id] = $d->delegate->name;
}
}
}
return($devices);
}

Im usually using this function to generate all child and parent array stdclass / object, but still make key same :
function GenNewArr($arr=array()){
$newarr = array();
foreach($arr as $a=>$b){
$newarr[$a] = is_object($b) || is_array($b) ? GenNewArr($b) : $b ;
}
return $newarr;
}

Related

php removing an element from objects

So, I have following db result:
Array
(
[0] => stdClass Object
(
[id] => 1
[user] => 1
[img] => 2016/02/img_8488.jpg
[url] => /p=?44
[sent_date] => 2016-02-13 00:00:00
)
[1] => stdClass Object
(
[id] => 2
[user] => 185
[img] =>
[url] => /?p=54
[sent_date] => 2016-02-06 00:00:00
)
)
How would I remove [id] and [sent_date] from the query result?
I am not sure if I am using unset right.
unset($results[0]['id']);
$reindex = array_values($results);
$objectarray = $reindex;
Instead of removal or unset you can create a new array;
$i = 0;
$newResult = array();
foreach($result as $value){
$newResult[$i]["user"] = $value->user;
$newResult[$i]["img"] = $value->img;
$newResult[$i]["url"] = $value->url;
$i++;
}
print_r($newResult);
$newResult will return the new array and your original array remains same you can use it if you need.
Or removal of indexes is must required than use unset inside the foreach loop as:
unset($value->id);
unset($value->sent_date);
Side note:
Also keep in mind you can not use it as $value["id"] becuase its a property not an array index.
Use unset($results[0]->id); and unset($results[0]->sent_date) instead and it should work. If you want to do this in all of the array objects:
for($i = 0; $i<sizeof($results); $i++)
{
unset($results[$i]->id);
unset($results[$i]->sent_date);
}

Retrieving correct record from multidimentional array

I'm having a mental freeze moment. If I have an array in the following format:
$myData = Array
(
[0] => stdClass Object
(
[id] => 1
[busID] => 5
[type] => SMS
[number] => 5128888888
)
[1] => stdClass Object
(
[id] => 2
[busID] => 5
[type] => APP
[number] => 5125555555
)
[2] => stdClass Object
(
[id] => 4
[busID] => 5
[type] => APP
[number] => 5129999988
[verified] => 1
[default] => 0
)
)
And I only have an var for ID of the record, how do I retrieve the rest of the detail for that set.
$myID = 2;
// get number 5125555555 and it's type
echo $myData[][$myID]['number']; // ???
The way you have your data arranged your going to have to loop through your array to identify the object corresponding to $myID.
foreach($myData as $object) if($object->id == $myID) echo $object->number;
The alternative is to arrange your $myData as an associative array with the id field as the key. Then you could access it simply with $myData[$myID]->number.
Actually it's an array that contains StdClass objects , try looping over $myData and access each attribute :
foreach ( $myData as $data )
{
print_r($data->id);
// ...
}
You can avoid loop by using following logic:
<?php
$myID = 2;
$myData = json_decode(json_encode($myData),1); // Convert Object to Array
$id_arr = array_column($myData, 'id'); // Create an array with All Ids
$idx = array_search($myID, $id_arr);
if($idx !== false)
{
echo $myData[$idx]['type'] . ' -- ' . $myData[$idx]['number'];
}
?>
Working Demo
Note: array_column is supported from PHP 5.5.
For lower versions you can use this beautiful library https://github.com/ramsey/array_column/blob/master/src/array_column.php
You can create a custom function to achieve this, you need to pass the array and id whose details you want and the function will return the array with matching id, like below
function detailsById($myData,$id){
foreach($myData as $data){
if($data->id == $id){
return $data;
}
}
}
Just call this function with your array and id..
$data=detailsById($myData,2);
echo "<pre>";print_r($data);
This will give you :
stdClass Object
(
[id] => 2
[busID] => 5
[type] => APP
[number] => 5125555555
)
And further to print 'number' and 'type' use $data array
$data['type'];
$data['number'];

sort mysql rows by column?

I have following sql query:
SELECT job_id, job_type FROM jobs
I'm getting the following result (set of rows) from mysql query:
RESULT (print_r):
Array
(
[0] => stdClass Object
(
[job_id] => 239
[job_type] => 'type1';
}
[1] => stdClass Object
{
[job_id] => 53
[job_type] => 'type2';
}
[2] => stdClass Object
{
[job_id] => 76
[job_type] => 'type3';
}
[3] => stdClass Object
{
[job_id] => 78
[job_type] => 'type1';
}
)
As you can see I've got three types of job: type1, type2, type3
Is there any way to map/regroup these results by job_type?
Basically I'd like to have something similar to this:
Array
(
['type1'] => Array (
[0] => stdClass Object
{
[job_id] => 239
[job_type] => 'type1';
}
[1] => stdClass Object
{
[job_id] => 76
[job_type] => 'type1';
}
)
['type2'] => Array (
[0] => stdClass Object
{
[job_id] => 53
[job_type] => 'type2';
}
)
['type3'] => Array (
[0] => stdClass Object
{
[job_id] => 78
[job_type] => 'type3';
}
)
)
Or maybe I should use different query?
I've tried to use array_map() with no luck, but I was getting only one array with elements from only one job_type.
Thanks in advance.
You cannot do this with predefined PHP functions. But you can do it yourself pretty easily.
For example: Assuming you have your MySQL result as written in your question in a variable called $rows, you can do the following to get the desired map.
$map = array();
foreach($rows as $row) {
if (!isset($map[$row->job_type])) {
$map[$row->job_type] = array($row);
} else {
$map[$row->job_type][] = $row;
}
}
Now $map contains your desired array.
Unfortunately for this task does not have a native solution in pure PHP/MySQL. So You need to sort it on PHP side manualy. I think You should write function for this an use it when You need
function sortBy($array, $key, $sortKeys=false){
// I don't test this function, just write it for answer, so it may contain errors
$tmp = array();
foreach($array as $k=>$v){
$tmp[$array[$k][$key]][] = $v;
}
if($sortKeys)
ksort($tmp);
return $tmp;
}
And use it like
print_r(sortBy(mySQLSelect(),'job_type'));

Ambiguity in PHP array

I want to display the Itinerary details from developer.ean.com API. By passing the customer's Itinerary ID and Email ID I got the details of reservation.
The result is comming in json format so I decoded it and creating array by using :
$array=json_decode($result);
The problem is whatever the result comming from API contain problem like :
For some records it providing array like this:
[Itinerary] => stdClass Object
(
[HotelConfirmation] => Array
(
[0] => stdClass Object
(
[supplierId] => 13
[chainCode] => EP
[arrivalDate] => 07/24/2012
[departureDate] => 07/26/2012
)
[Hotel] => Array
(
[0] => stdClass Object
(
[hotelId] => 394808
[statusCode] => A
[name] => Barkston Youth Hostel
)
)
)
)
In this case the HotelConfirmation and Hotel is Array which contain [0] as object
and for some records it providing array like this:
[Itinerary] => stdClass Object
(
[HotelConfirmation] => stdClass Object
(
[supplierId] => 13
[chainCode] => EP
[arrivalDate] => 07/24/2012
[departureDate] => 07/26/2012
)
[Hotel] => stdClass Object
(
[hotelId] => 394808
[statusCode] => A
[name] => Barkston Youth Hostel
)
)
and In this case the HotelConfirmation and Hotel is itself an object
I providing only few data here actually its big array and I want to provide list of it. But the array containing ambiguity like this. How can I handle this issue. Is there any solution.
Thanks in advance.
Pass true as the second argument to json_decode. This will create an array instead of stdClass
$array=json_decode($result, true);
you can normalize the input like so:
// get objects as arrays
$array = json_decode($result, true);
// remove the sub-level [0], when necessary
foreach ($array as $key => $value) {
if (is_array($value[0])) {
$array[$key] = $value[0];
}
}
Then the result always looks the same:
[Itinerary] => Array
(
[HotelConfirmation] => Array
(
[supplierId] => 13
[chainCode] => EP
[arrivalDate] => 07/24/2012
[departureDate] => 07/26/2012
)
[Hotel] => Array
(
[hotelId] => 394808
[statusCode] => A
[name] => Barkston Youth Hostel
)
)
First do this:
$array = json_decode($result, true);
Which will convert objects into associative arrays
And do adjustment like this:
if (isset($array['HotelItineraryResponse']['Itinerary']['HotelConfirmation'][0])) {
$array['HotelItineraryResponse']['Itinerary']['HotelConfirmation'] = $array['HotelItineraryResponse']['Itinerary']['HotelConfirmation'][0];
}
It will definitly work.
You can use is_array() to check for it:
if (is_array($hotel)) {
$hotel = $hotel[0];
}
just change to this:
$array=json_decode($result,TRUE);
and handle arrays always?
Looks like you may have to account for both possibilities in your model... Checking to see if the hotel node contains and array or an obj and operating accordingly.
you can type case the object inside of an array.
$array = json_decode($result);
$array = (array)$array
or alternatively you can pass true as the second argument in your json_decode();
according to php documentation
When TRUE, returned objects will be converted into associative arrays.
$array = json_decode($result, true);
Check for object or array:
if( is_object($Itinerary -> HotelConfirmation)) {
// do one thing or nothing
} elseif( is_array($Itinerary -> HotelConfirmation)) {
$Itinerary -> HotelConfirmation = array_shift( $Itinerary -> HotelConfirmation );
}

CakePHP Model Query Return Data Formating

I'm looking for a way to make it so cake returns all database data in the same format/structure... Currently it returns two different types of format depending on the relationship.
If a model 'B' is associated with the current model 'A' being queried it will then place model associations for 'B' underneath it as you can see in [User] below. I want it so that all queries use that structure.
example:
$this->find('all', ....
returns:
Array
(
[0] => Array
(
[UserGroup] => Array
(
[id] => 53
[user_id] => 100003332014851
[media_id] =>
[name] => john
[description] => qwasdfad
)
[User] => Array
(
[id] => 100003332014851
[session_id] => ssm2qbrotmm13ho1ipm8ii2492
[username] =>
[password] => -1
[Planner] => Array
(
)
[Purchase] => Array
(
)
[Listing] => Array
(
)
)
)
I want this to look like:
Array
(
[0] => Array
(
[UserGroup] => Array
(
[id] => 53
[user_id] => 100003332014851
[media_id] =>
[name] => john
[description] => qwasdfad
[User] => Array
(
[id] => 100003332014851
[session_id] => ssm2qbrotmm13ho1ipm8ii2492
[username] =>
[password] => -1
[Planner] => Array
(
)
[Purchase] => Array
(
)
[Listing] => Array
(
)
)
)
)
)
In CakePHP, the find() method return data like your first format. But If you want to format like second one then you have to process it by hand (try to avoid this if possible)
$data = $this->find('all');
$assocs = Set::extract('/User', $data); // extracting all `User` array
foreach($assocs as $key => $assoc) {
unset($data[$key]['User']); // removing the associate `User` from `$data`
$data[$key]['UserGroup']['User'] = $assoc['User']; // adding associate under `UserGroup`
}
ended up doing this... it changes the output to what we need. The top level item does not have a header which is fine I just adjusted our scripts for that... maybe this will help somebody else if they need a custom idea
also no guarantee this covers all possible results but so far it works with all the queries we have.
class AppModel extends Model {
function afterFind($results, $primary) {
//if this is a primary, structure like a secondary so entire site is same format
if ($primary) {
$class = get_class($this);
//simple fix for primary
foreach ($results as $key => $result) {
$result = $this->formatData($result, $class);
$results[$key] = $result;
}
}
return $results;
}
function formatData($result, $class) {
$array = array();
if (isset($result[$class])) {
$array = $result[$class];
unset($result[$class]);
}
$array += $result;
return $array;
}
You can also use contain in this case along with find as UserGroup.User for your desired result

Categories