Brian Scramlin Posted January 17, 2021 Share Posted January 17, 2021 Hello, I am attempting to I have a JSON array I am receiving from an endpoint. I convert this to `Array` with `json_decode` Then, I create a new `WireArray` and import it. My issue is using the `->sort()` function does not work because this array is filled with standard objects. I believe I need to convert these to `WireData` objects. Am I on the right track? $meevo_response_array = json_decode( $meevo_response ); $reviews = new WireArray(); $reviews->import($meevo_response_array); Here I am stuck: foreach ($reviews as $review) { // convert each $review object to WireData object } Link to comment Share on other sites More sharing options...
horst Posted January 17, 2021 Share Posted January 17, 2021 Whats about creating an empty WireArray and then do the foreach loop on the $meevo_response_array? $meevo_response_array = json_decode( $meevo_response ); $reviews = new WireArray(); foreach($meevo_response_array as $meevo_response_item) { $tmp = new WireArray(); $tmp->import($meevo_response_item); $reviews->add($tmp); } https://processwire.com/api/ref/wire-array/add/ Link to comment Share on other sites More sharing options...
Robin S Posted January 17, 2021 Share Posted January 17, 2021 5 hours ago, Brian Scramlin said: My issue is using the `->sort()` function does not work because this array is filled with standard objects. The json_decode() function has an "associative" argument that you can set to true: Quote associative When true, JSON objects will be returned as associative arrays; when false, JSON objects will be returned as objects. $meevo_response_array = json_decode($meevo_response, true); Or you can use the core wireDecodeJSON() function which does this automatically. $meevo_response_array = wireDecodeJSON($meevo_response); 4 Link to comment Share on other sites More sharing options...
Brian Scramlin Posted January 19, 2021 Author Share Posted January 19, 2021 @Robin S and @horst thank you both for your replies! I have attempted the solutions suggested and I continue to get the same error when calling $reviews->sort( 'transactionDateTime' ); Quote Argument 1 passed to WireArray::getItemPropertyValue() must be an instance of Wire, array given I can successfully convert the entire multi-dimensional array to WireArray objects. However, as I mentioned, the same error persists. I believe my issue may be related to using the `->sort()` function with an array. Here is the schema I am working with right now: ProcessWire\WireArray::__set_state(array( 'data' => array ( 0 => array ( 'ratingCount' => 2, 'transactionDateTime' => '2020-08-20T16:51:41.5596530', 'employeeCode' => 'MELANIE', 'employeeFirstName' => 'Melanie', 'employeeLastName' => 'Slinker', 'employeeNickName' => 'MELANIE', 'serviceId' => '8f219595-6134-4b78-9f15-ac170120a95a', 'shortcut' => 'WHC', 'displayName' => 'Women’s Haircut/Style', 'clientId' => '41a15d6b-4c66-4b1c-b67f-ac1a0139e55d', 'clientFirstName' => 'Sue', 'clientLastName' => 'Lopshire', 'tenantId' => 0, 'locationId' => 0, 'sale_Seq' => 0, 'saleLine_Seq' => 0, 'employee_User_TId' => 'd9589557-cf37-42f6-80ed-ac06011afd19', 'rating' => 5, 'comment' => 'Love my cut. Melanie does a great job', ), 1 => array ( 'ratingCount' => 1, 'transactionDateTime' => '2020-08-21T12:59:32.2407132', 'employeeCode' => 'MEGAN', 'employeeFirstName' => 'Megan', 'employeeLastName' => 'Nierman', 'employeeNickName' => 'Megan', 'serviceId' => '8f219595-6134-4b78-9f15-ac170120a95a', 'shortcut' => 'WHC', 'displayName' => 'Women’s Haircut/Style', 'clientId' => '738b74f0-e984-4cef-ac61-ac1c010294fd', 'clientFirstName' => 'Danielle', 'clientLastName' => 'Bryant', 'tenantId' => 0, 'locationId' => 0, 'sale_Seq' => 0, 'saleLine_Seq' => 0, 'employee_User_TId' => '4af682b8-50e4-4617-b54f-ac0701204d71', 'rating' => 5, 'comment' => 'Love Megan! Always leave loving my hair!', ), ), 'extraData' => array ( ), 'itemsRemoved' => array ( ), 'itemsAdded' => array ( ), 'duplicateChecking' => false, 'sortFlags' => 0, 'useFuel' => true, '_instanceNum' => 0, 'localHooks' => array ( ), 'trackChanges' => 0, 'changes' => array ( ), '_notices' => array ( 'errors' => NULL, 'warnings' => NULL, 'messages' => NULL, ), '_wire' => NULL, )); Link to comment Share on other sites More sharing options...
Brian Scramlin Posted January 19, 2021 Author Share Posted January 19, 2021 I have been able to accomplish the task natively with: usort($meevo_response_array, function ($a, $b) { return $b['transactionDateTime'] <=> $a['transactionDateTime']; }); But if I can do it the ProcessWire way, I try to. 1 Link to comment Share on other sites More sharing options...
Robin S Posted January 19, 2021 Share Posted January 19, 2021 @Brian Scramlin, to use WireArray::sort I think the items in the WireArray need to be Wire-derived objects, e.g. WireData. Relevant API documentation:https://processwire.com/api/ref/functions/wire-data/https://processwire.com/api/ref/functions/wire-array/ Demo: 2 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now