Conversion of nested stdclassobject to a double dimensional array - php

How to convert a double std class object i.e (stdclass object having again a stdclass object in it) into an array?
I have user type; casting it only converted the single object but the object inside remained same.
Data:
Array ( [activities] => Array ( ) [goals] => stdClass Object (
[activeMinutes] => 30 [caloriesOut] => 3355
[distance] => 8.05 [steps] => 10000 )
[summary] => stdClass Object ( [activeScore] => -1 [activityCalories]
=> 1472 [caloriesBMR] => 2074 [caloriesOut] => 3308
[distances] => Array ( [0] => stdClass Object ( [activity] => total [distance] => 8.46 ) [1] => stdClass
Object ( [activity] => tracker [distance] => 8.46 )
[2] => stdClass Object (
[activity] => loggedActivities [distance] => 0 )
[3] => stdClass Object (
[activity] => veryActive [distance] => 2.35 )
[4] => stdClass Object (
[activity] => moderatelyActive [distance] => 1.63 )
[5] => stdClass Object (
[activity] => lightlyActive [distance] => 4.48 )
[6] => stdClass Object ( [activity] => sedentaryActive [distance] => 0 ) )
[fairlyActiveMinutes] => 32
[lightlyActiveMinutes] => 194 [marginalCalories] => 867 [sedentaryMinutes] =>
1125 [steps] => 11446 [veryActiveMinutes] => 31 ) )
This data has stdclass object inside an array how to convert that also into an array and make it on the whole as a double dimensional array.

I guess you have something like:
<?php
$a = [
'foo' => (object)[
'id' => 1,
'name' => 'foo',
],
'bar' => (object)[
'id' => 2,
'name' => 'bar',
'nested' => (object)['status' => 'ok'],
],
];
var_export($a);
Result:
array (
'foo' =>
stdClass::__set_state(array(
'id' => 1,
'name' => 'foo',
)),
'bar' =>
stdClass::__set_state(array(
'id' => 2,
'name' => 'bar',
'nested' =>
stdClass::__set_state(array(
'status' => 'ok',
)),
)),
)
And you try to receive something like:
array (
'foo' =>
array (
'id' => 1,
'name' => 'foo',
),
'bar' =>
array (
'id' => 2,
'name' => 'bar',
'nested' =>
array (
'status' => 'ok',
),
),
)
So the best way done it - is like mentioned #MineshPatel use next code:
var_export(json_decode(json_encode($a), true));

Related

merge two array with loop and key name

i have two separate arrays $first_one:
Array
(
[0] => Array
(
[_date] => 2019-10-16
[_number] => 1
[_order] => 1
[name] => jack
[other_ids] => Array
(
[b_id] => 1253
)
)
[1] => Array
(
[_date] => 2020-10-11
[_number] => 2
[_order] => 2
[name] => joey
[other_ids] => Array
(
[b_id] => 1433
)
)
)
and the $second_array:
Array
(
[0] => Array
(
[date] => 2019-10-16
[number] => 1
[order] => 1
[name] => jack
[last_name] => foobar
[other_ids] => Array
(
[b_id] => 1253
)
)
[1] => Array
(
[date] => 2020-10-11
[number] => 2
[order] => 2
[name] => joey
[last_name] => foobar
[other_ids] => Array
(
[b_id] => 1433
)
)
[2] => Array
(
[date] => 2020-10-28
[number] => 3
[order] => 3
[name] => tom
[last_name] => foobar
[other_ids] => Array
(
[b_id] => 1593
)
)
)
they are very similar but they came from different api's and they are different in numbers and also some key names.
so what i'm trying to do is to count the $first_one arrays and if for that many loop through the $second_one (in this example is 2) and if the [_number] [_order] from $first_one was equal (==) to [number] [number] $second_one then take some info (for example the last name) from it and put in a new array.
so is this possible to do?
$arr1 = [ [ '_date' => '2019-10-16','_number' => 1,'_order' => 1,
'name' => 'jack','other_ids' => ['b_id' => 1253]
],
['_date' => 2020-10-11,'_number' => 2,'_order' => 2,
'name' => 'joey','other_ids' => ['b_id' => 1433]
]
];
$arr2 = [ [ 'date' => '2019-10-16','number' => '1','order' => '1',
'name' => 'jack','last_name' => 'foobar','other_ids' => ['b_id' => 1253]
],
[ 'date' => '2019-10-11','number' => '2','order' => '2',
'name' => 'joey','last_name' => 'foobar','other_ids' => ['b_id' => 1433]
],
[ 'date' => '2019-10-28', 'number' => '3', 'order' => '3',
'name' => 'tom', 'last_name' => 'foobar', 'other_ids' => ['b_id' => 1593]
],
];
// first make second array more directly searchable
// make new array with the `number` as the key
foreach( $arr2 as $a){
$arr2new[$a['number']] = $a;
}
foreach ($arr1 as $a) {
if ( array_key_exists($a['_number'], $arr2new) &&
$a['_order'] == $arr2new[$a['_order']]['order'] )
{
$merged[] = ['name'=>$a['name'], 'other_ids' => $a['other_ids']];
}
}
print_r($merged);
RESULT
Array
(
[0] => Array (
[name] => jack
[other_ids] => Array
(
[b_id] => 1253
)
)
[1] => Array (
[name] => joey
[other_ids] => Array
(
[b_id] => 1433
)
)
)

Unexpected php usort result

I have an array with files. And I want to sort them by their min value (I'll explain below).
This is an example of array:
$files = array(
array(
'name' => "DevTools/tools/files/design/js/custom/http_build_query.js",
'range' => array(
'min' => 0
)
),
array(
'name' => "DevTools/tools/files/design/js/custom/Form.js",
'range' => array(
'min' => 0
)
),
array(
'name' => "DevTools/tools/files/design/js/custom/VanillaJS.js",
'range' => array(
'min' => 0
)
),
array(
'name' => "resources/js/plugins/jquery.js"
),
array(
'name' => "resources/js/plugins/jquery-ui.js"
),
array(
'name' => "resources/js/plugins/popper.js"
),
array(
'name' => "resources/js/plugins/bootstrap-v4/index.js",
'range' => array(
'min' => 800
)
),
array(
'name' => "resources/js/plugins/bootstrap-v4/util.js",
'range' => array(
'min' => 0
)
),
array(
'name' => "resources/js/plugins/bootstrap-v4/dropdown.js",
'range' => array(
'min' => 200
)
),
array(
'name' => "resources/js/plugins/bootstrap-v4/collapse.js",
'range' => array(
'min' => 0
)
),
array(
'name' => "resources/js/custom/global.js"
),
array(
'name' => "layouts/cms/.js/+0.js",
'range' => array(
'min' => 0
)
),
array(
'name' => "resources/js/plugins/module/delete.js"
),
array(
'name' => "resources/js/plugins/module/form.submit.js"
),
array(
'name' => "resources/js/plugins/module/piece.image.js"
),
array(
'name' => "resources/js/plugins/select2.js"
)
);
It's easy to understand. We have names for all files. And some of them have [range][min].
What I need to do is keeping their order, but sorting only files from same folder which have the [range][min], so we reorder these ones by [min].
My code
So I use this function:
usort($files, function (array $a, array $b) {
if (!isset($a['range']) || !isset($b['range'])
|| dirname($a['name']) != dirname($b['name'])
|| $a['range']['min'] == $b['range']['min']) {
return 0;
}
return $a['range']['min'] <=> $b['range']['min'];
});
Which does exactly what I said above. So only those ones are correcly reordered, as you can see here:
[6] => Array
(
[name] => resources/js/plugins/bootstrap-v4/util.js
[range] => Array
(
[min] => 0
)
)
[7] => Array
(
[name] => resources/js/plugins/bootstrap-v4/collapse.js
[range] => Array
(
[min] => 0
)
)
[8] => Array
(
[name] => resources/js/plugins/bootstrap-v4/dropdown.js
[range] => Array
(
[min] => 200
)
)
[9] => Array
(
[name] => resources/js/plugins/bootstrap-v4/index.js
[range] => Array
(
[min] => 800
)
)
Which is great.
The problem
Now I add in my $files array this one:
array(
'name' => "resources/js/plugins/module/piece.images.js"
)
Append it anywhere you want in $files, the usort will reorder entire array with no logic:
Array
(
[0] => Array
(
[name] => DevTools/tools/files/design/js/custom/http_build_query.js
[range] => Array
(
[min] => 0
)
)
[1] => Array
(
[name] => resources/js/plugins/bootstrap-v4/collapse.js
[range] => Array
(
[min] => 0
)
)
[2] => Array
(
[name] => resources/js/plugins/select2.js
)
[3] => Array
(
[name] => resources/js/plugins/module/piece.image.js
)
[4] => Array
(
[name] => resources/js/plugins/module/form.submit.js
)
[5] => Array
(
[name] => resources/js/plugins/module/delete.js
)
[6] => Array
(
[name] => layouts/cms/.js/+0.js
[range] => Array
(
[min] => 0
)
)
[7] => Array
(
[name] => resources/js/plugins/bootstrap-v4/util.js
[range] => Array
(
[min] => 0
)
)
[8] => Array
(
[name] => resources/js/custom/global.js
)
[9] => Array
(
[name] => resources/js/plugins/bootstrap-v4/dropdown.js
[range] => Array
(
[min] => 200
)
)
[10] => Array
(
[name] => DevTools/tools/files/design/js/custom/Form.js
[range] => Array
(
[min] => 0
)
)
[11] => Array
(
[name] => resources/js/plugins/bootstrap-v4/index.js
[range] => Array
(
[min] => 800
)
)
[12] => Array
(
[name] => resources/js/plugins/popper.js
)
[13] => Array
(
[name] => resources/js/plugins/jquery-ui.js
)
[14] => Array
(
[name] => resources/js/plugins/jquery.js
)
[15] => Array
(
[name] => DevTools/tools/files/design/js/custom/VanillaJS.js
[range] => Array
(
[min] => 0
)
)
[16] => Array
(
[name] => resources/js/plugins/module/piece.images.js
)
)
I have no idea why is that happening. If you can find out why, can you please see if there is a solution please?
If I didn't explain well, please ask in comments for clarification.
So, what I need to do is keeping their order, but sorting only files from same folder which have the [range][min], so we reorder these ones by [min].

how to add new key and value to multidimensional in PHP without using the loops....?

Array ( [0] => Array (
[date] => 01-06-2018
[nav] => 30.65100 )
[1] => Array (
[date] => 31-05-2018
[nav] => 30.84900 )
[2] => Array (
[date] => 30-05-2018
[nav] => 30.73200 )
[3] => Array (
[date] => 29-05-2018
[nav] => 30.81500 )
The above code is the Multi-array, we have added a common id like id_code = 0089 to every array in it without using any loops in PHP. Can anyone helps me and it is possible or not .....?
If you mean not manually loop through the array then yes, it is possible using array_walk:
$array = [
0 => [
"date" => "01-06-2018",
"nav" => "30.65100"],
1 => [
"date" => "01-06-2018",
"nav" => "30.65100"],
2 => [
"date" => "01-06-2018",
"nav" => "30.65100"],
3 => [
"date" =>"01-06-2018",
"nav" => "30.65100"]
];
array_walk($array, function(&$item1) {
$item1['id_code'] = "0089";
});
print_r($array);
Output:
Array
(
[0] => Array
(
[date] => 01-06-2018
[nav] => 30.65100
[id_code] => 0089
)
[1] => Array
(
[date] => 01-06-2018
[nav] => 30.65100
[id_code] => 0089
)
[2] => Array
(
[date] => 01-06-2018
[nav] => 30.65100
[id_code] => 0089
)
[3] => Array
(
[date] => 01-06-2018
[nav] => 30.65100
[id_code] => 0089
)
)
Demo https://3v4l.org/lCGIO
An effective solution will be to use array_map function as:
$keyValue = 'some value';
$data = array_map(function($d) use ($keyValue){
return $d + ['keyName' => $keyValue];
}, $data);
I think this is the closer you can get, but it is probably not your expected result. The exact result you need cannot be done without using a loop as far as I know.
$t = array( 0 => array( 'date' => '01-06-2018', 'nav' => '30.65100' ), 1 => array( 'date' => '31-05-2018', 'nav' => '30.84900' ), 2 => array( 'date' => '30-05-2018', 'nav' => '30.73200' ), 3 => array( 'date' => '29-05-2018', 'nav' => '30.81500' ));
$tt = array( 0 => array( 'id' => '648'), 1 => array( 'id' => '332'), 2 => array( 'id' => '889'), 3 => array( 'id' => '285') );
$final = array_map(null, $t, $tt);
print_r($final);
The output will look like
Array
(
[0] => Array
(
[0] => Array
(
[date] => 01-06-2018
[nav] => 30.65100
)
[1] => Array
(
[id] => 648
)
)
[1] => Array
(
[0] => Array
(
[date] => 31-05-2018
[nav] => 30.84900
)
[1] => Array
(
[id] => 332
)
)
[2] => Array
(
[0] => Array
(
[date] => 30-05-2018
[nav] => 30.73200
)
[1] => Array
(
[id] => 889
)
)
[3] => Array
(
[0] => Array
(
[date] => 29-05-2018
[nav] => 30.81500
)
[1] => Array
(
[id] => 285
)
)
)

PHP's json_decode returns NULL with an elasticsearch query result

I've seen similar question here, but no one with an aggregation which might have something to do with the problem at hand.
This is the code I'm trying to run:
require '../vendor/autoload.php';
$client = Elasticsearch\ClientBuilder::create()->build();
$params = [
'index' => 'search_log',
'type' => 'search_log',
'body' => [
'size' => 0,
'aggs' => [
'popular_terms' => [
'terms' => [
'field' => 'filteredSearch'
]
]
]
]
];
// Execute query
$response = $client->search($params);
// Decode response
$myData = json_decode($response);
var_dump($myData);
The output I get:
NULL
I have the problem narrowed down to the decoding part. If I execute a print_r of the $response, this is what I get (actually all in one line, I've pretty-printed it manually so it's easier to read):
Array (
[took] => 245
[timed_out] =>
[_shards] => Array (
[total] => 5
[successful] => 5
[failed] => 0
)
[hits] => Array (
[total] => 5124004
[max_score] => 0
[hits] => Array ( )
)
[aggregations] => Array (
[popular_terms] => Array (
[doc_count_error_upper_bound] => 37750
[sum_other_doc_count] => 11388032
[buckets] => Array (
[0] => Array (
[key] => term1
[doc_count] => 385107
)
[1] => Array (
[key] => term2
[doc_count] => 169381
)
[2] => Array (
[key] => term3
[doc_count] => 155258
)
[3] => Array (
[key] => term4
[doc_count] => 150382
)
[4] => Array (
[key] => term5
[doc_count] => 124759
)
[5] => Array (
[key] => term6
[doc_count] => 102589
)
[6] => Array (
[key] => term7
[doc_count] => 98791
)
[7] => Array (
[key] => term8
[doc_count] => 98772
)
[8] => Array (
[key] => term9
[doc_count] => 98091
)
[9] => Array (
[key] => term10
[doc_count] => 94559
)
)
)
)
)
My desired result would be to print just term1, term2, etc, without the rest of stuff that comes with Elasticsearch responses.

How to group image order by time from array

I have array of photo with filename and time. I want to find the photos which are taken in hour. For example, I have photos taken from 10.00, 10.15, 10.59. So when I search. I want these photo to be listed under 10.00.
I think it's something to do with in_array or array_search. But I still don't have an idea how.
Here is my array: $pixArr[]
Array
(
[0] => Array
(
[file] => IMG_7519.JPG
[time] => 13:02
)
[1] => Array
(
[file] => IMG_7518.JPG
[time] => 13:01
)
[2] => Array
(
[file] => IMG_7517.JPG
[time] => 13:00
)
[3] => Array
(
[file] => IMG_7516.JPG
[time] => 11:39
)
[4] => Array
(
[file] => IMG_7515.JPG
[time] => 11:39
)
[5] => Array
(
[file] => IMG_7514.JPG
[time] => 11:38
)
[6] => Array
(
[file] => IMG_7513.JPG
[time] => 11:29
)
[7] => Array
(
[file] => IMG_7512.JPG
[time] => 11:26
)
[8] => Array
(
[file] => IMG_7511.JPG
[time] => 11:26
)
)
I expect it coming out something like this:
10:00
listPhoto.php
<?
//now how to list files by hour???
?>
you may use array_filter As #u_mulder suggested:
<?php
$pixArr = array
(
0 => array
(
'file' => 'IMG_7519.JPG',
'time' => '13:02'
),
1 => array
(
'file' => 'IMG_7518.JPG',
'time' => '13:01'
),
2 => array
(
'file' => 'IMG_7517.JPG',
'time' => '13:00'
),
3 => array
(
'file' => 'IMG_7516.JPG',
'time' => '11:39'
),
4 => array
(
'file' => 'IMG_7515.JPG',
'time' => '11:39'
),
5 => array
(
'file' => 'IMG_7514.JPG',
'time' => '11:38'
),
6 => array
(
'file' => 'IMG_7513.JPG',
'time' => '11:29'
),
7 => array
(
'file' => 'IMG_7512.JPG',
'time' => '11:26'
),
8 => array
(
'file' => 'IMG_7511.JPG',
'time' => '11:26'
),
);
print_r(array_filter($pixArr, function($value){
if($value['time'] >= '11:00' && $value['time'] <= '12:00')
return $value;
}));
See Demo: https://eval.in/513466
you can using assort This function sorts an array such that array indices maintain their correlation with the array elements they are associated with.
I have been tried this problem.
$arr = [
[
"file" => "IMG_7519.JPG",
"time" => "13:02"
],
[
"file" => "IMG_7519.JPG",
"time" => "12:02"
],
[
"file" => "IMG_7519.JPG",
"time" => "15:02"
]
];
asort($arr); //sorting array
echo "<pre>".print_r($arr)."</pre>";
and this is the result.
Array ( [1] => Array ( [file] => IMG_7519.JPG [time] => 12:02 ) [0] => Array ( [file] => IMG_7519.JPG [time] => 13:02 ) [2] => Array ( [file] => IMG_7519.JPG [time] => 15:02 ) )

Categories