multiple cURL and output JSON? - php

I have written a multiple curl and it outputs two different arrays
I'm using curl_multi_getcontent.
for($i = 0; $i < $node_count; $i++)
{
$results = curl_multi_getcontent ( $curl_arr[$i] );
$results = json_decode($results,true);
/*echo "<pre>";
print_r($results);
echo "</pre>";*/
}
This outputs two arrays. if I wanted to get data from one of the arrays, how do I call the first array? or the second array?
Is there something like $results[0] or something for the first array
foreach($results[0] as $result){
echo $result['pagination'];
}

Yes, as of right now, the $result variables are overwritting each other through the for loop. Try this:
for($i = 0; $i < $node_count; $i++)
{
$results[$i] = curl_multi_getcontent ( $curl_arr[$i] );
$results[$i] = json_decode($results,true);
}
print_r($results);
You should now have two elements in your array that you can call via $results[0] or $results[1] etc...

PHP's documentation has an example using curl_multi_exec . Basically, it creates two cURL handles, adds them to the curl_multi_add_handle interface, and then runs them in parallel.

Related

Using an index twice in PHP

I am trying to use a for loop where it looks through an array and tries to make sure the same element is not used twice. For example, if $r or the random variable is assigned the number "3", my final array list will find the value associated with wordList[3] and add it. When the loop runs again, I don't want $r to use 3 again. Example output: 122234, where I would want something along the lines of 132456. Thanks in advance for the help.
for($i = 0; $i < $numWords; $i++){
$r = rand(0, $numWords);
$arrayTrack[$i] == $r;
$wordList[$r] = $finalArray[$i];
for($j = 0; $j <= $i; $j++){
if($arrayTrack[$j] == $r){
# Not sure what to do here. If $r is 9 once, I do not want it to be 9 again.
# I wrote this so that $r will never repeat itself
break;
}
}
Edited for clarity.
Pretty sure you are over complicating things. Try this, using array_rand():
$final_array = array();
$rand_keys = array_rand($wordList, $numWords);
foreach ($rand_keys as $key) {
$final_array[] = $wordList[$key];
}
If $numWords is 9, this will give you 9 random, unique elements from $wordList.
See demo
$range = range(0, $numWords - 1); // may be without -1, it depends..
shuffle($range);
for($i = 0; $i < $numWords; $i++) {
$r = array_pop($range);
$wordList[$r] = $finalArray[$i];
}
I do not know why you want it.. may be it is easier to shuffle($finalArray);??
So ideally "abcdefghi" in some random order.
$letters = str_split('abcdefghi');
shuffle($letters);
var_dump($letters);
ps: if you have hardcoded array $wordList and you want to take first $n elements of it and shuffle then (if this is not an associative array and you do not care about the keys)
$newArray = array_slice($wordList, 0, $n);
shuffle($newArray);
var_dump($newArray);
You can try array_rand and unset
For example:
$array = array('one','two','free','four','five');
$count = count($array);
for($i=0;$i<$count;$i++)
{
$b = array_rand($array);
echo $array[$b].'<br />';
unset($array[$b]);
}
after you have brought the data in the array, you purify and simultaneously removing the memory array
Ok... I have NO idea why you are trying to use so many variables with this.
I certainly, have no clue what you were using $arrayTrack for.
There is a very good chance I am mis-understanding all of this though.
<?php
$numWords=10;
$wordList=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
$finalArray=array();
for ($i=0; $i<$numWords; $i++) {
start:
$r=rand(0,$numWords);
$wordChoice=$wordList[$r];
foreach ($finalArray as $word) {
if ($word==$wordChoice) goto start;
}
$finalArray[]=$wordChoice;
}
echo "Result: ".implode(',',$finalArray)."\n";

PHP array_diff returns all results

I am reading a text file and putting the contents which are file id's into an array.
I then have a new array with the same id's but one extra.
My aim is to compare these two arrays and print out the new file id or file id's
The problem I have is that When I use the function array_diff, it prints everything and not the actual difference between the two arrays.
$results = array_diff($pNewList, $pSavedList);
$resultsCount = count($results);
for($x = 0; $x < $resultsCount; $x++){
echo $results[$x].'<br/>';
}
my output is printing every line even though the contents are the same in both arrays
Try this
$results = array_diff($pNewList, $pSavedList, TRUE);
$resultsCount = count($results);
for($x = 0; $x < $resultsCount; $x++){
echo $results[$x].'<br/>';
}

slice the array with pair and without pair in php

The array values are:
$input_array = array( "Student_1","Student_2","Student_3","Student_4","student_5","Student_6","Student_7","Student_8" );
Now, I need the output as:
$array = array( array('Student_1', 'Student_2'), array('Student_3', 'Student_4'),
array('Student_5', 'Student_6'), array('Student_7'), array('Student_8') );
$input_array = array("Student_1","Student_2","Student_3","Student_4","student_5","Student_6","Student_7","Student_8");
function sliceArray($arr, $pairs = 3){
$rtnArr = array();
$total = count($arr);
if($total < ($pairs * 2)) return 'Error: Invalid number of pairs!';
for($i=0; $i<$total; $i++){
if(count($rtnArr) < $pairs && isset($arr[$i+1])){ $rtnArr[] = array($arr[$i], $arr[$i+1]); $i++; }
else $rtnArr[] = array($arr[$i]);
}
return $rtnArr;
}
var_dump(sliceArray($input_array));
You can change the $pair value to get required number of pairs.
Since you didnt specify your formatting or output rules, I'll just assume the rules myself for this case.
I assume you want your array to take two inputs each from the first and pair them
array('Student_1', 'Student_2'),
But the last two are not paired
array('Student_7'), array('Student_8')
So i assume you want the last two to be seperate.
Now for the solution
NOTE: This only works for even arrays for the moment. Working on a more general answer now.
// We make a new array which takes every two elements from the first and appends to itself
for($i=0; $i < count($input_array)-2; $i=$i+2)
{
$array_output []= array($input_array[$i], $input_array[$i+1]); //append the teo elements
}
// Now add the final two elements to the array
$array_output []= array($input_array[count($input_array)-2]);
$array_output []= array($input_array[count($input_array)-1]);
//Output here
echo "<pre>";
print_r($array_temp);
echo "</pre>";
In case if you are new to php
$array []= "something"; // will append to the array
If you didn't want the last two to be seperate (assuming you made a typo),
change
for($i=0; $i < count($input_array)-2; $i=$i+2)
to
for($i=0; $i <= count($input_array); $i=$i+2)
and remove
/*
$array_output []= array($input_array[count($input_array)-2]);
$array_output []= array($input_array[count($input_array)-1]);
*/

A for loop not doing as expected

I am running a for loop and attempting to enter data into an array.
When I run the print_r of the array, it's as if the for loop is only running once when it should be running multiple times!
for($i=0; $i<count($count); $i++){
$currentField = $array[$i];
$test = "".$field."[".$i."]";
$this->_postData[$test] = $currentField;
$this->_currentItems[$i] = $test;
}
print_r($this->_currentItems);
die();
If I echo $count before, it says 3 (for example) but still when I print the array, it only has 1 value! Any ideas?
Thanks.
Try
for($i=0; $i<$count; $i++)
i think count is not needed .
try
for($i=0; $i<$count; $i++){

filtering out repeats

I'm creating a program which goes through data in a array and filter outs any repeats within it and then echos out anything which isn't a repeated piece of data
for ($i = 0; $i < count($urlArray); $i++) {
for ($j = 0; $j < count($urlArray); $j++) {
if($i != $j)
{
if($urlArray[$i] !== $urlArray[$j])
echo $urlArray[$i];
}
}
}
I'm fairly certain there's something wrong but I can't quite spot it, any help with this would be great.
I dont get, how your array is structured, but whats about just array_unique()
$urlArray = array_unique($urlArray);
Or in your case (because you want to echo it
foreach (array_unique($urlArray) as $url) echo $url;
Update:
Sorry, just mixed up two functions :) Of course its array_unique() and not array_filter().
You could use built in function array_unique() to remove duplicate values in array
$result = array_unique($urlArray);
print_r($result);

Categories