-1

I have cart system which is saved in json array in database. I want to select all orders then all product_id's inside this array and show on page products which has most sells like bestsellers.

So far I'm able to take the array out and it's looks like this

array(2) { 
    [224]=> array(6) { 
        // data in which I'm not interested
    } 
}
array(2) { 
    [23]=> array(6) { 
        // data in which I'm not interested
    } 
}
array(2) { 
    [1]=> array(6) { 
        // data in which I'm not interested
    } 
}
array(2) { 
    [1231]=> array(6) { 
        // data in which I'm not interested
    } 
}

Each array(2) {...} represent one order. I need this [224],[23].. because those are products ID. My question is how to know what property to select in my loop?

If it was like this

array(2) { 
    ["id"]=> string(13) "32" 
}

I will make something like $data->id to get 32

This is how I got the array

foreach ($products as $item) {
      $data = json_decode($item->details, true);
      echo var_dump($data);
}

Any help is appreciated.

Update: with print_r($data); they're looking like this

Array ( 
    [224] => Array ( 
        // data 
    ) 
)
Array ( 
    [23] => Array ( 
        // data 
    ) 
)
....

To me this is completely different because I don't know the name of column therefore I can't use array_column()

2 Answers2

1

If always the key of $data is product id, then in foreach loop get the key by using php function key().

foreach ($products as $item) {
      $data = json_decode($item->details, true);
      echo key($data)."</br>";
      //OR Create an array to store all the product ids
      $product_ids[] = key($data);
}
echo "<pre>";
print_r($product_ids);

// to count each product id
$product_ids_count = array_count_values($product_ids);
 //get the most used product id
echo $most_used_product_id = array_search(max($product_ids_count),$product_ids_count);
Ravinder Reddy
  • 3,869
  • 1
  • 13
  • 22
  • Yes, I have them now in `Array ( [0] => 224 [1] => 23 ... )`. Since I have them now is it possible to make count and show the most used ID? –  Jul 19 '17 at 14:43
0

You can use array_keys which will list all keys in the array.
http://php.net/manual/en/function.array-keys.php

 $products = array_keys($array);

And the $products array will hold your values 224, 23 and so on.

https://3v4l.org/PEjfH

With this $products array you can get values from your original array like this:

$array[$product[0]]["cost"]

I just made up the cost but you get the idea.
The above would be the same as $array["224"]["cost"]

Andreas
  • 23,610
  • 6
  • 30
  • 62
  • Thanks for the answer is good to have alternatives but this showing me also some other values on the output. –  Jul 19 '17 at 14:57
  • @user5996816 values does it show? It should only give you the keys of the array. Are you sure you have the array set up as in question? – Andreas Jul 19 '17 at 15:00
  • Yes, it is exactly as in the question but I've got others values too. Which I'm not interested in them –  Jul 19 '17 at 15:32
  • Ok try this just to see if it's due to duplicate names or something `$products1234 = array_keys($array);` and var dump thar array and post the values – Andreas Jul 19 '17 at 15:36
  • Array keys does not make up values. It grabs the keys from the array. As you can see from the link in my answer it works. Something must be wrong with you original array or I don't know what. Also var dump your array. And let's see the values. – Andreas Jul 19 '17 at 15:39
  • It is probably because inside of each array there is another array –  Jul 19 '17 at 15:49
  • @user5996816 no, as I said array keys does not make up values. See here: https://3v4l.org/QWnIR – Andreas Jul 19 '17 at 16:02
  • This array is not like mine. Mine is as in the question `array(2) { [224]=> array(6) { [value] => something here } } –  Jul 19 '17 at 16:18
  • @user5996816 as I said then, the array in question **is not as your in real life** as you say now "array(2)" but you only list 1 in the question. This is your fault and your question is wrongfully written. For that you earn a downvote. Don't take up people's time with incorrect data in your questions. – Andreas Jul 19 '17 at 16:25
  • wow, its clearly written `// data in which I'm not interested`. What you think is this, cars? Don't make excuses please. –  Jul 19 '17 at 16:33
  • The data you are not interested in are **in the subarray** of your question not in the main array. If you are unsure of your array just post it as it is. Don't try and mask values. – Andreas Jul 19 '17 at 16:37
  • I've clearly posted the what I need to know -> How to get key from **main** array. There is no need to post whole array. Anyway, thanks for the help. –  Jul 19 '17 at 16:41
  • Yes there is https://stackoverflow.com/help/mcve notice **complete** and **Verifiable** – Andreas Jul 19 '17 at 16:54