How to replace huge array with PHP generators - php

Seeking your help to convert one big array with PHP generators.
Below is my code for which I need rework:
I am getting a result set from a service call and assigning all to an array:
foreach ($objects->result as $pointStdObject) {
$pointStdObjects[] = $pointStdObject;
}
This piece of code is inside a while loop which queries for records with an offset of 1000.
Issue is $pointStdObjects[] tends to get very huge and I get PHP out of memory exception.
Later I again need to use this same array as:
foreach ($pointStdObjects as $pointStdObject) {
$point = $this->pointFactory->createPointFromStdObject($pointStdObject);
if (!$point) {
continue;
}
$points[] = $point;
}
return $points;
Please suggest if we can leverage PHP generators or yield here

function getStd()
{
///your code before that
foreach ($objects->result as $pointStdObject) {
yield $pointStdObject;
}
}
function useStd()
{
foreach (getStd() as $pointStdObject) {
$point = $this->pointFactory->createPointFromStdObject($pointStdObject);
if (!$point) {
continue;
}
$points[] = $point;
}
return $points;
}

Related

Removing an array from a PHP JSON object

So a bit of background information is I'm creating a web app and I have 50~ arrays that I'm currently using what I get from an API, I've created a script to find the arrays that I don't need lets call them "bad arrays" but the problem is I'm unsure how I can filter these arrays out with the method I'm using to search through them
I'm searching through them with this script
$tagItems = [];
foreach($tags['items'] as $item) {
if (!$item['snippet']['tags'] || !is_array($item['snippet']['tags'])) {
continue;
}
foreach($item['snippet']['tags'] as $tag) {
$tag = strtolower($tag);
if (!isset($tagItems[$tag])) {
$tagItems[$tag] = 0;
}
$tagItems[$tag]++;
}
}
But let's say I didn't want it to include the 8th array and the 15th array
$tags['items'][8]['snippet']['tags'];
$tags['items'][15]['snippet']['tags'];
I want these to be removed from the original $tags array. How can i achieve this?
EDIT: This needs to be dynamic. I do not know if there are going to be 45/50 arrays that will need removing or just 2/50. the array that needs removing can be reffered to as $index
I have a script which determines what array(s) need to be removed
$i = 0;
while ($i <= 50) {
$x = 0;
while ($x <= 50) {
if ($tags['items'][$i]['snippet']['channelId'] == $tags['items'][$x]['snippet']['channelId']) {
if ($x < $i) {
break;
} else {
echo $x.", ";
break;
}
}
$x++;
}
$i++;
}
I'm going to edit this a little more to provide some extra information that may be useful. My overall goal is to use the YouTube API to remove all but the first array of tags where the channel id appears multiple times. I'm using a script which finds all the array numbers that dont need to be removed an URL.
You can check for the array key
$tagItems = [];
$toRemove = array(8,15);
foreach($tags['items'] as $key => $item) {
if(in_array($key,$toRemove)){
continue;
}
if (!$item['snippet']['tags'] || !is_array($item['snippet']['tags'])) {
continue;
}
foreach($item['snippet']['tags'] as $tag) {
$tag = strtolower($tag);
if (!isset($tagItems[$tag])) {
$tagItems[$tag] = 0;
}
$tagItems[$tag]++;
}
}

finding an array range in PHP

I have a range of values in an array like:
$values = array(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.1,1.2,1.3,1.4,1.5);
I need to find the index of the smallest value in that array that's greater than or equal to a specified number. For example, if the user inputs 0.25, I need to know that the first array index is 2.
In other languages I've used, like R, there is a 'which' function that will return an array of indices that meet some criteria. I've not found that in PHP, so i'm hopeful someone else has solved this.
Thanks.
You can use array_filter
It does exactly what R which does.
Hope this function will help you,
function find_closest_item($array, $number) {
sort($array);
foreach ($array as $a) {
if ($a >= $number) return $a;
}
return end($array); // or return NULL;
}
I don't know if there's a built in function for that, but this should work:
function getClosest($input, $array)
{
foreach($array as $value)
{
if ($value >= $input)
{
return $value;
}
}
return false;
}
You can use the following working logic. There might be other built in functions which can be used to solve this problem.
<?php
$values = array(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.1,1.2,1.3,1.4,1.5);
$search=0.35;
$result_index = NULL;
$result_value=NULL;
$count=count($values);
for($i=0;$i<$count;$i++) {
if($values[$i]<$search) {
continue;
}
if($result_index==NULL) {
$result_index = $i;
$result_value = $values[$i];
continue;
}
if($values[$i]<$result_value) {
$result_index = $i;
$result_value = $values[$i];
}
}
print $result_index . " " . $result_value;
?>
You can build your own custom function:
function findIndex($input_array, $num)
{
foreach($input_array as $k => $v)
{
if($v >= $num)
{
return $k;
}
}
return false;
}
This function will return array index (key). If you don't want index, but the value, then other functions posted here will do the job. You should clarify what exactly you want to get as your question is a little bit amiguous

Compare differences in Multidimensional array

I have a rather ugly query, and the results from the query are then post-processed using php which turns each row into it's own multidimensional array.
I want to refactor the query but need to make sure I do not change what it returns in any way.
So What I want to do is copy the original query and call that, store the results.
then run the function again with my new query.
Loop over the two arrays of results and compare them for any differences what so ever (keys, values, missing entries, type differences etc).
What is the easiest way to do this?
Essentially I know how to call the two queries etc,
I guess my real question is, at the end once I have my two arrays of the results how do I go through and compare them.
What I would love to end up with is a side by side "print_r" type output with a red line or similar going across highlighting any differences.
First of all, you can use array_uintersect_assoc() like this.
// First get intersecting values
$intersect = array_uintersect_assoc($expected, $results, "checkStructure");
print_r($intersect);
//Then print results that are in intersecting set (e.g. structure of $expected, value of $results
print_r(array_uintersect_assoc($results, $intersect, "checkStructure"));
function checkStructure($x, $y) {
if (!is_array($x) && !is_array($y)) {
return 0;
}
if (is_array($x) && is_array($y)) {
if (count($x) == count($y)) {
foreach ($x as $key => $value) {
if(array_key_exists($key,$y)) {
$x = checkStructure($value, $y[$key]);
if ($x != 0) return -1;
} else {
return -1;
}
}
}
} else {
return -1;
}
return 0;
}
If still not, take help of array_diff()
and array_diff_assoc(). Or try following code.
function multidimensional_array_diff($a1,$a2)
{
$r = array();
foreach ($a2 as $key => $second)
{
foreach ($a1 as $key => $first)
{
if (isset($a2[$key]))
{
foreach ($first as $first_value)
{
foreach ($second as $second_value)
{
if ($first_value == $second_value)
{
$true = true;
break;
}
}
if (!isset($true))
{
$r[$key][] = $first_value;
}
unset($true);
}
}
else
{
$r[$key] = $first;
}
}
}
return $r;
}
Why don't you just make a VIEW that turns an ugly query into something you can just SELECT against? What you're talking about is making a materialized view, something that MySQL doesn't handle as well as other database platforms.
Why not write the results of each query out to a text files then compare the two text files with the diff command?

iterate through array, number of keys is variable, the first value being processed differently

Hi I have a PHP array with a variable number of keys (keys are 0,1,2,3,4.. etc)
I want to process the first value differently, and then the rest of the values the same.
What's the best way to do this?
$first = array_shift($array);
// do something with $first
foreach ($array as $key => $value) {
// do something with $key and $value
}
I would do this:
$firstDone = FALSE;
foreach ($array as $value) {
if (!$firstDone) {
// Process first value here
$firstDone = TRUE;
} else {
// Process other values here
}
}
...but whether that is the best way is debatable. I would use foreach over any other method, because then it does not matter what the keys are.
Here is one way:
$first = true;
foreach($array as $key => $value) {
if ($first) {
// something different
$first = false;
}
else {
// regular logic
}
}
$i = 0;
foreach($ur_array as $key => $val) {
if($i == 0) {
//first index
}
else {
//do something else
}
$i++;
}
I would do it like this if you're sure the array contains at least one entry:
processFirst($myArray[0]);
for ($i=1; $i<count($myArray); $1++)
{
processRest($myArray[$i]);
}
Otherwise you'll need to test this before processing the first element
I've made you a function!
function arrayCallback(&$array) {
$callbacks = func_get_args(); // get all arguments
array_shift($callbacks); // remove first element, we only want the callbacks
$callbackindex = 0;
foreach($array as $value) {
// call callback
$callbacks[$callbackindex]($value);
// make sure it keeps using last callback in case the array is bigger than the amount of callbacks
if(count($callbacks) > $callbackindex + 1) {
$callbackindex++;
}
}
}
If you call this function, it accepts an array and infinite callback arguments. When the array is bigger than the amount of supplied functions, it stays at the last function.
You can simply call it like this:
arrayCallback($array, function($value) {
print 'callback one: ' . $value;
}, function($value) {
print 'callback two: ' . $value;
});
EDIT
If you wish to avoid using a function like this, feel free to pick any of the other correct answers. It's just what you prefer really. If you're repeatedly are planning to loop through one or multiple arrays with different callbacks I suggest to use a function to re-use code. (I'm an optimisation freak)

How do I store the results of this recursive function?

I have the following PHP code which works out the possible combinations from a set of arrays:
function showCombinations($string, $traits, $i){
if($i >= count($traits)){
echo trim($string) . '<br>';
}else{
foreach($traits[$i] as $trait){
showCombinations("$string$trait", $traits, $i + 1);
}
}
}
$traits = array(
array('1','2'),
array('1','2','3'),
array('1','2','3')
);
showCombinations('', $traits, 0);
However, my problem is that I need to store the results in an array for processing later rather than just print them out but I can't see how this can be done without using a global variable.
Does anyone know of an alternative way to achieve something similar or modify this to give me results I can use?
Return them. Make showCombinations() return a list of items. In the first case you only return one item, in the other recursive case you return a list with all the returned lists merged. For example:
function showCombinations(...) {
$result = array();
if (...) {
$result[] = $item;
}
else {
foreach (...) {
$result = array_merge($result, showCombinations(...));
}
}
return $result;
}
In addition to the other answers, you could pass the address of an array around inside your function, but honestly this isn't nearly the best way to do it.
Using the variable scope modifier static could work. Alternatively, you could employ references, but that's just one more variable to pass. This works with "return syntax".
function showCombinations($string, $traits, $i){
static $finalTraits;
if (!is_array($finalTraits)) {
$finalTraits = array();
}
if($i >= count($traits)){
//echo trim($string) . '<br>';
$finalTraits[] = $string;
} else {
foreach($traits[$i] as $trait){
showCombinations("$string$trait", $traits, $i + 1);
}
}
return $finalTraits;
}
$traits = array(
array('1','2'),
array('1','2','3'),
array('1','2','3')
);
echo join("<br>\n",showCombinations('', $traits, 0));
Of course, this will work as expected exactly once, before the static nature of the variable catches up with you. Therefore, this is probably a better solution:
function showCombinations($string, $traits, $i){
$finalTraits = array();
if($i >= count($traits)){
$finalTraits[] = $string;
} else {
foreach($traits[$i] as $trait){
$finalTraits = array_merge(
$finalTraits,
showCombinations("$string$trait", $traits, $i + 1)
);
}
}
return $finalTraits;
}
although the solution by Lukáš is the purest as it has no side effects, it may be ineffective on large inputs, because it forces the engine to constantly generate new arrays. There are two more ways that seem to be less memory-consuming
have a results array passed by reference and replace the echo call with $result[]=
(preferred) wrap the whole story into a class and use $this->result when appropriate
the class approach is especially nice when used together with php iterators
public function pageslug_genrator($slug,$cat){
$page_check=$this->ci->cms_model->show_page($slug);
if($page_check[0]->page_parents != 0 ){
$page_checks=$this->ci->page_model->page_list($page_check[0]->page_parents);
$cat[]=$page_checks['re_page'][0]->page_slug;
$this->pageslug_genrator($page_checks['re_page'][0]->page_slug,$cat);
}
else
{
return $cat;
}
}
this function doesnt return any value but when i m doing print_r $cat it re
store the results in a $_SESSION variable.

Categories