PHP: Warning: Sort() Expects Parameter 1 to Be Array, Resource Given

Unknown usort() expects parameter 1 to be array, null given error

This is so stupid, i switched to xampp and it worked fine. I don't know what was the problem, might have been some request problem.

Sorting foreach by date give Warning: usort() expects parameter 1 to be array, null given in

You can sort out each array entry by the date directly in the query with:

ORDER BY publish_up DESC (descending order)
ORDER BY publish_up ASC (ascending order)

As in:

$jquery1 = "SELECT `fulltext`,alias,metakey,publish_up,metadesc FROM xxx_content  WHERE catid=22 AND state= '1' ORDER BY publish_up ASC";

If you want to keep this PHP function date_compare then you have to replace:

$a['publish_up']

by

$a->publish_up

As in:

$jfeed1= $jdb1->loadObjectList();
function date_compare($a, $b)
{
$t1 = strtotime($a->publish_up);
$t2 = strtotime($b->publish_up);
return $t1 - $t2;
}
usort($jfeed1, 'date_compare');
foreach ($jfeed1 as $jitem1):

return $t1 - $t2; is ascending

return $t2 - $t1; is descending

PHP: is this a bug: shuffle() expects parameter 1 to be array, object given?

No, it's not a bug.

PHP 5 allows you to use foreach() to loop through objects that aren't arrays. These objects are called Iterators.

Unfortunately, the old array-based functions, like shuffle() cannot process Iterators.

The main reason for this is that an Iterator may not even be sortable -- for example, you can have iterators that read directly from a file or a URL, and read a new line of data each time the foreach() loop cycles. This clearly can't be sorted because it's read during the foreach() process.

You can convert an Iterator into an array, using the cleverly named iterator_to_array() function. However, this may be a bad idea if you don't know how much data the iterator is going to process, as you may find it uses a lot of memory.

Some iterators may provide methods within the iterator object itself for sorting or filtering the data. If so, this is a better solution than trying to sort it as an array.

If you're working with an ORM, then this implies that your Iterator object is reading data from a DB. In this case, sorting it via the DB query (ie ORDER BY or whatever methods the ORM provides to do that) would probably be a better solution than sorting the data in PHP.

Warning: key() expects parameter 1 to be array, string given

I think the array you pasted on the question is not property formatted. I can see some issue on your associative array keys(string without quotes). See it working fine for me after adding quotes on string key.

<?php
$product = array("Test" => array("price"=>"9999.00", "priceOld"=>"", "percentageSaved"=>0, "currency"=>"$"));
print '<pre>';
print_r($product);
print '</pre>';
$keys = key($product);
echo "Your key is: $keys";
?>

Program Output:

Array
(
[Test] => Array
(
[price] => 9999.00
[priceOld] =>
[percentageSaved] => 0
[currency] => $
)

)

Your key is: Test

Edit: If you want to get internal keys of Test, then use array_keys() method like this.

 print_r(array_keys($product['Test']));

Goutte - array_push() expects parameter 1 to be array, null given

You need global $res1Array at the top of the updateCalendarDetailsData() function. The anonymous callback functions are trying to use this global variable, but you're initializing the local variable, not the global variable. You seem to be assuming that global simply allows you to access variables from any outer scope, but it's just for global variables.

You have similar problems with $TEMP and $EVENT.

A better method is to include the variables in the use() list of the functions. Since you're modifying the variables with array_push(), you need to declare them as references with &.

<?php
function updateCalendarDetailsData()
{
$client = new Client();

$x = 1;
$LIMIT = 3;
global $x;
global $LIMIT;
$x++;
$res1Array = array();

$ffUrlArr = ["https://www.forexfactory.com/calendar.php?month=Jan2020"];
foreach ($ffUrlArr as $key => $v) {

try {
$crawler = $client->request('GET', $ffUrlArr[$key]);
} catch (\Exception $ex) {
error_log($ex);
}

$TEMP = array();

$count = $crawler->filter('.calendar_row')->count();
$i = 1; // count starts at 1
$crawler->filter('.calendar_row')->each(function ($node) use ($count, $i, &$res1Array) {
$EVENT = array();

$EVENTID = $node->attr('data-eventid');

$API_RESPONSE = file_get_contents('https://www.forexfactory.com/flex.php?do=ajax&contentType=Content&flex=calendar_mainCal&details=' . $EVENTID);

$API_RESPONSE = str_replace("<![CDATA[", "", $API_RESPONSE);
$API_RESPONSE = str_replace("]]>", "", $API_RESPONSE);

$html = <<<HTML
<!DOCTYPE html>
<html>
<body>
$API_RESPONSE
</body>
</html>
HTML;

$subcrawler = new Crawler($html);

$subcrawler->filter('.calendarspecs__spec')->each(function ($LEFT_TD) use (&$res1Array, &$TEMP, &$EVENT) {

$LEFT_TD_INNER_TEXT = trim($LEFT_TD->text());

if ($LEFT_TD_INNER_TEXT == "Source") {

$TEMP = array();
$LEFT_TD->nextAll()->filter('a')->each(function ($LINK) use (&$TEMP) {
array_push($TEMP, $LINK->text(), $LINK->attr('href'));
});

$EVENT['sourceTEXT'] = $TEMP[0];
$EVENT['sourceURL'] = $TEMP[1];
$EVENT['latestURL'] = $TEMP[3];
}

if ($LEFT_TD_INNER_TEXT == "Measures") {
$EVENT['measures'] = $LEFT_TD->nextAll()->text();
}

if ($LEFT_TD_INNER_TEXT == "Usual Effect") {
$EVENT['usual_effect'] = $LEFT_TD->nextAll()->text();
}

if ($LEFT_TD_INNER_TEXT == "Derived Via") {
$EVENT['derived_via'] = $LEFT_TD->nextAll()->text();
var_dump($EVENT);
print_r($EVENT);
if (empty($EVENT)) {
echo "test";
}
array_push($res1Array, $EVENT); // <---- HERE I GET THE ERROR!
}
});
$i++;
if ($i > $count) {
echo "<pre>";
var_dump($res1Array);
print_r($res1Array);
echo "</pre>";
exit;
}
});
}
return $res1Array;
}


Related Topics



Leave a reply



Submit