Simple Machines Community Forum

General Community => Scripting Help => Topic started by: mickjav on August 05, 2022, 03:40:26 PM

Title: Array Help
Post by: mickjav on August 05, 2022, 03:40:26 PM
Managed to sort Arrays with one value but multidimensional Arrays are giving me a headache

I would like to add rec_id and artist to an array then I was thinking of using Array_search to get the artist if there if one but even my books are not helping me?

//Get A List If Singles For The Combi Array
$Recsquery = $smcFunc['db_query']('', '
SELECT  ar.rec_id, COALESCE(a.artist_the, a.artist_name) As Artist
FROM {db_prefix}art_and_rec AS ar
INNER JOIN {db_prefix}artists AS a ON (ar.art_id = a.art_id)
WHERE ar.main = 1 AND ar.rec_id IN(SELECT rec_id from {db_prefix}art_and_rec WHERE art_id = ' . $art . ')');

while ($recrow = $smcFunc['db_fetch_assoc']($Recsquery))
$recrow[] = $recrow['rec_id'];

Any help would be much appreciated

All the best mick
Title: Re: Array Help
Post by: Arantor on August 05, 2022, 04:22:22 PM
Um, why not sort it in the database query and be done with it?
Title: Re: Array Help
Post by: mickjav on August 05, 2022, 04:40:46 PM
Quote from: Arantor on August 05, 2022, 04:22:22 PMUm, why not sort it in the database query and be done with it?

do you mead ASC OR DESC

This is lookup for a artist "A Combination of Artists" which my db uses

It's being uses here
https://www.databasedreams.co.uk/testing/index.php?action=music;area=artists;sa=home;art=-2089005463;fmt=8#recs

To see the effect I want look here
https://www.databasedreams.co.uk/charts/index.php?msg=17036

The effect is shown Like:
Prisoner (Miley Cyrus Feat. Dua Lipa)


I have found something I thought I could understand

//Get A List If Singles For The Combi Array
$Recsquery = $smcFunc['db_query']('', '
SELECT  ar.rec_id, COALESCE(a.artist_the, a.artist_name) As Artist
FROM {db_prefix}art_and_rec AS ar
INNER JOIN {db_prefix}artists AS a ON (ar.art_id = a.art_id)
WHERE ar.main = 1 AND ar.rec_id IN(SELECT rec_id from {db_prefix}art_and_rec WHERE art_id = ' . $art . ')');

if ($smcFunc['db_affected_rows']() != 0)
{
while ($recrow = $smcFunc['db_fetch_assoc']($Recsquery))
{
$arrrow = array(
array('ID' => $recrow['rec_id'],
  'Art' => $recrow['Artist']),
  );
}
}
$smcFunc['db_free_result']($Recsquery);

And to get the artist I tried this:
$key = array_search($rec, array_column($arrrow, 'art'));

But it's still not returning the artist even though I know the queries retuning records
Title: Re: Array Help
Post by: Arantor on August 05, 2022, 04:48:32 PM
You know that you can sort the query by multiple columns in both ascending and descending order, right?

array_search finds a value in an array, it's about the most inefficient way possible to ever do sorting. In any case, array_search doesn't tell you the value it found, it tells you the array index where it found it, which is of... zero use to you as far as I can see.

The problem with what you're doing is that I can only see what you've done, I have no ability to make sense of what you're *trying* to do, if I'm honest it's usually why I avoid most of these threads.
Title: Re: Array Help
Post by: mickjav on August 05, 2022, 05:00:38 PM
I could do what I want but it would cost in number of queries used as I could convert the above to something I can use in_array with then call a function with a query that could return the value I want but as I said it's would be costly in queries run.

If you think it would help I could post the function??