Jump to content

Importing different arrays (pageArray/regular PHP array) together


a-ok
 Share

Recommended Posts

Is it possible to combine a pageArray with a regular PHP array? I'm returning a list of news items from PW, which is fine, but I'm also returning Instagram and Twitter posts, from API endpoint URLs (which is JSON but I have converted to PHP arrays) and I'd like to then ultimately import them all into one big array then sort by a custom date field (that I'll build and store per loop) so you get a list of news items, Instagram posts and tweets all sorted by date.

Any thoughts on importing a pageArray and a regular PHP array? Should I be using $a->getArray(); then combining them?

Thanks!

Link to comment
Share on other sites

You can use explode method to get only need properties of pages, so you can equalize array structure of pages and items from the Instagram feed.

$x = pages('template=product')->explode(['id', 'created', 'title']);
$y = getInstagramFeedArray();

$result = array_merge($x, $y);
  • Like 3
Link to comment
Share on other sites

4 minutes ago, Zeka said:

You can use explode method to get only need properties of pages, so you can equalize array structure of pages and items from the Instagram feed.


$x = pages('template=product')->explode(['id', 'created', 'title']);
$y = getInstagramFeedArray();

$result = array_merge($x, $y);

Very nice! I guess this is the best way, right? PW API offers nothing related? Thank you!

Link to comment
Share on other sites

You basically have two choices

  1. Convert the PageArray into a regular array OR
  2. Convert the social media array into a WireArray (e.g. each to be a WireData which you throw into one WireArray)

#2 will give you the advantage of easily sorting by your custom date field. I am not sure though if a WireArray can be combined with a PageArray just like that (although the latter is from a WireArray, so, yeah, maybe it is possible).

Edit:

Depending on your operations and size of the PageArray, since Page objects can be big, to have the best of both worlds (1 and 2), you can use explode as per @Zeka's suggestion, but convert that into a WireArray. Then, convert your social media array into a WireArray and add it to the first WireArray. You can then do your sorting by the custom date field. I don't know how much this will cost you in server resources though. Just a thought...

Edited by kongondo
  • Like 2
Link to comment
Share on other sites

58 minutes ago, kongondo said:

You basically have two choices

  1. Convert the PageArray into a regular array OR
  2. Convert the social media array into a WireArray (e.g. each to be a WireData which you throw into one WireArray)

#2 will give you the advantage of easily sorting by your custom date field. I am not sure though if a WireArray can be combined with a PageArray just like that (although the latter is from a WireArray, so, yeah, maybe it is possible).

Edit:

Depending on your operations and size of the PageArray, since Page objects can be big, to have the best of both worlds (1 and 2), you can use explode as per @Zeka's suggestion, but convert that into a WireArray. Then, convert your social media array into a WireArray and add it to the first WireArray. You can then do your sorting by the custom date field. I don't know how much this will cost you in server resources though. Just a thought...

Thanks for this. I didn't know you convert it into a WireArray tbh. This is what I currently have, which seems okay, but when I come to thinking about pagination... not sure how that'll work (so maybe WireArray is best if I can work it out). I haven't included my Instagram API fetch stuff here... just the variable.

$news = $pages->find('parent=/news/, sort=-created, limit=16')->explode(['id', 'created', 'title', 'url', 'images', 'summary_text']);
$instagrams = $instagram->getInstagramRecent();

$items = array_merge($news, $instagrams['data']);

bd($items);

foreach ($items as $key => $item) {

	if (isset($item['user']['id']) && $item['user']['id'] == '1907645821') { // Instagram post
		$sort[$key] = $item['caption']['created_time'];
	} else { // News article
		$sort[$key] = $item['created'];
	}
}

array_multisort($sort, SORT_DESC, $items);

bd($items);

if (isset($item['user']['id']) && $item['user']['id'] == '1907645821') { // Instagram post
	$type = 'Instagram';
	$title = NULL;
	$image = $item['images']['standard_resolution']['url'];
	$text = '<p class="instagram">' . $item['caption']['text'] . '</p>';
	$link = $item['link'];

} else { // News article
	$type = 'News';
	$title = $item['title'];
	foreach($item['images'] as $img) {
		$itemImage = $img->url;
		break;
	}
	$image = $itemImage;
	$text = $item['summary_text'];
	$link = $item['url'];
}

 

Link to comment
Share on other sites

6 hours ago, oma said:

Is there any documentation on turning an array into a WireArray? ->makeNew() doesn’t accept arrays.

setArray()

http://processwire.com/api/ref/wire-array/set-array/

http://processwire.com/api/ref/wire-data/set-array/

You might want to use the WireData one if WireArray throws errors about does not accept ...etc type.

$arr = array('color'=>'silver', 'make'=>'volvo', 'age'=> 2, 'seats'=> 5, 'style'=>'suv');
$wireData = new WireData();
$wireData->setArray($arr);

echo $wireData->make;// volvo
echo $wireData->age;// 2

As you can see, the $arr indexes/keys become the property names and values the values. So, you might want to use single words for the $arr keys

Edited by kongondo
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...