Jump to content

Native Array of Objects to WireArray and WireData Objects


Brian Scramlin
 Share

Recommended Posts

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

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

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);

 

  • Like 4
Link to comment
Share on other sites

@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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...