I am reading an API that brings me the horoscopes and from time to time a horoscope is missing.
Doing the following I save only the horoscopes that come to me in an array:
$datosJson = [];
foreach ($datos_diario['horoscopo'] as $key => $value) {
array_push($datosJson, $key);
}
var_dump($datosJson);
Which returns the following:
array(9) {
[0]=>
string(7) "acuario"
[1]=>
string(5) "aries"
[2]=>
string(6) "cancer"
[3]=>
string(11) "capricornio"
[4]=>
string(9) "escorpion"
[5]=>
string(7) "geminis"
[6]=>
string(3) "leo"
[7]=>
string(5) "libra"
[8]=>
string(6) "piscis"
}
The problem I have is that when going through that information with a for, Aquarius is in position 0, so the horoscope is not displayed in order.
Creating another array where the complete information is, how can I tell $dataJson: look, position 0 has to be Aries, as it is in $horoscope and that they are ordered, it doesn't matter if I get 2, 3, 11 from the api, 12 horoscopes.
$horoscopos = ['aries', 'tauro', 'geminis', 'cancer', 'leo', 'virgo',
'libra', 'escorpion', 'sagitario', 'capricornio', 'acuario', 'piscis'];
Try the following: Perform a foreach on horoscope and with the following logic: Does the value that brings horoscope exist in dataJson? If it exists I don't do anything, if I don't delete it from the horoscope and in this way the horoscope keeps the data that comes to me and sorts, the problem is that when doing the unset the positions are eliminated and if I don't get Taurus for example, Aries I am left in position 0 and geminis in position 2 and when doing the for it gives an error because it does not find position 1:
foreach ($horoscopos as $key => $value) {
$indice = array_search($value ,$datosJson,false);
if ($indice == ""){
unset($horoscopos[$key]);
}
}
Bring back:
array(8) {
[0]=>
string(5) "aries"
[2]=>
string(7) "geminis"
[3]=>
string(6) "cancer"
[4]=>
string(3) "leo"
[6]=>
string(5) "libra"
[7]=>
string(9) "escorpion"
[9]=>
string(11) "capricornio"
[11]=>
string(6) "piscis"
}
Based on the latter, can the positions be ordered in ascending order, changing the values it has so that it stays like this? That would fix everything.
array(8) {
[0]=>
string(5) "aries"
[1]=>
string(7) "geminis"
[2]=>
string(6) "cancer"
[3]=>
string(3) "leo"
[4]=>
string(5) "libra"
[5]=>
string(9) "escorpion"
[6]=>
string(11) "capricornio"
[7]=>
string(6) "piscis"
}
SOLVED THANKS TO PHPMyguel's ANSWER
I always recommend that before getting into a problem that would make sense to have a solution, you look at the documentation of the language in question, since most of the time it already has a function that solves the problem for us.
What you have is a group A , which is nothing more than a group
array
with all the information and which is also properly ordered:Then, on the other hand, you have group B , which is another one
array
that, in addition to being able to arrive with missing values, will also arrive in a disorderly manner.If I understood you correctly, that's correct.
What you need is to get the matching values in A and B sorted according to the order provided by A . For this there is a function called
array_intersect()
that returns just what we want.In this way we could solve it with:
This would get:
Which I think is just what you need.
Reference: array_intersect()
EDIT
Through the function
array_values()
you can delete the indices (really what it does is obtain only the values, leaving the indices out) so that it generates new ones based on the position that each element occupies within thearray
.This would get: