0

Before I waste time trying a bunch of different things, thought I'd ask here to see if anyone has a good strategy for this.

I have almost 3,000 arrays that are being produced in a while loop. Perhaps also I'm thinking is valuable is that I can attach the ID of the soon to be array

$results = $mysqli->query("SELECT ID, post_content
                           FROM wp_posts
                           WHERE post_type = 'product';");

while($row = $results->fetch_array()) {

      $id2 = $row['ID'];
      $id = mysqli_real_escape_string($mysqli, $id2);

It's important to note that when I say comparing these arrays I'm not looking to compare them based off their content, but rather the number of nodes in the array.

So I can of course do that easily

$arraycount = count($wp_post_array);

And thusly I can get the two things I need: the unique ID and the count of the array.

echo '<br />' . 'ID: ' . $id. '<br />';
echo '<br />' . 'ARRAY COUNT: ' . $arraycount . '<br />';

Giving me an output like this:

enter image description here

So, I think this is the first step. As I said I have around 3,000 of these, so manually trying to match these up is not something I want to do.

My output (just for the sake of the question - I don't really care what it is as long as I'm able to compare), would maybe look something like:

ARRAY COUNT: 55
ID: 5740, 8805, ..., etc

Anyone have any experience with doing something like this? I'm sure there would be an easy solution if I was working with 2 or 3 arrays, but in the case of thousands? I'm not sure what the smartest strategy would be. Is there a built-in PHP function that can help me, or a library, or just a smart idea to get these to compare the array count?

Edit:

Sample Array

Array
(
    [0] => Arcu Magna Accessus 16-03 AB Class non Nunc Corrupti Elit Sit 4" Provocatus
    [1] => 6" Superue Tibi Sit
    [2] => 6380-0362
    [3] => Quos Porro Oppressu EA
    [4] => Leo id Louor & Quam Tibi Commune
    [5] => Memoriam:
    [6] => mE Magni Illa Harusen
    [7] => eA Quos Elit Armorum
    [8] => Responsuros Louor:
    [9] => 939/07 Hac A/Lorem molfstum
    [10] => Ex successum ex dominatio justo fortitudinis sed rem ornare hac hic successus li urna iucunda usus hac te nemo vulputate.
)

Which is beheld in a variable and then counted.

bbruman
  • 667
  • 4
  • 20
  • 1
    Are you saying that wp_posts.post_content contains a serialized array? Is this a Wordpress table? If not then the solution (as usual) is to normalize your array. Its far from clear what you wnat to do with what you describe as the "output". You didn't say if what the structure of the table is nor whether theis could be changed. Voting to close. – symcbean May 19 '17 at 15:25
  • No, you are looking too far into it. It is WordPress but that's irrelevant. They are just simple arrays with text strings and an array count in a while loop. I updated the question with an example array if that helped. – bbruman May 19 '17 at 15:30
  • You are populating this data with your own code?????!!!!!! OMG!!!!!! – symcbean May 19 '17 at 15:34
  • ? Not sure what you're getting at or implying – bbruman May 19 '17 at 15:35
  • 1
    Where does `$wp_post_array` come from? – Don't Panic May 19 '17 at 15:38
  • I didn't think it mattered. I was trying to minimize the question-- being a while loop with an array and an array count. If you really want to know `$wp_post_array` came from here : http://stackoverflow.com/questions/44027675/trim-not-working-with-array-from-mysql-fetched-string) – bbruman May 19 '17 at 15:41
  • @bbruman: XY Problem. – symcbean May 19 '17 at 15:42
  • @symcbean Okay. This is my first time attempting anything like this, and thought I'd ask and try to be clear on the issue to see if anyone had any ideas. I can delete the question if this is a problem – bbruman May 19 '17 at 15:43
  • its pretty confusing what youre saying.. also iterating 3000 would take a long time.. perhaps theres a better way of going from what you have to your desired goal so that you can achieve it with better performance. What is your end goal ? – CodeGodie May 19 '17 at 15:49
  • Sorry guys! Thought I was being clear! I'm on localhost and iterating 3,000 only takes a few seconds... but anyways - @Don't Panic provided a perfect solution below. – bbruman May 19 '17 at 15:55

1 Answers1

1

Instead of outputting your results as you are currently doing, append them to an array grouped by count.

$groups[$arraycount][] = $id;

Then you can iterate that to get the result you want.

foreach ($groups as $count => $ids) {
    echo '<br />' . 'ARRAY COUNT: ' . $count. '<br />';
    echo '<br />' . 'ID: ' . implode(', ', $ids) . '<br />';
}

I'm not really familiar with wordpress, though. This is just what the problem looks like to me from a plain PHP perspective. There may be a better way to do this with wordpress as suggested in the comments.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • Absolutely perfect! Thank you very much - and in a couple lines of code too. The output is exactly what was wanted. You probably saved me a days worth of messing around with different things for this. Much appreciated. – bbruman May 19 '17 at 15:53