0

I am trying to display an array without key item. Just want to display array values without key. Here is my sample code I tried

$myList = array(
        0 =>array(
                "product_id"=> 8085
            ),
         1 =>array(
                "product_id"=> 8087
            ),
          2 =>array(
                "product_id"=> 8086
            ),
           3 =>array(
                "product_id"=> 8042
            ),
        );


  $newList = array();
foreach($myList as $listItem) {
    $newList[] = $listItem['product_id'];
}
$a=array();
$a= array_values($newList);
print_r($a);

I want my array like this

$productIds = array(8085,8087,8086,8042);

Here is my sample code link

Lemon Kazi
  • 3,308
  • 2
  • 37
  • 67
  • Do you want it look like this when you print it? Because your `look like this` part is an php code line! – JustOnUnderMillions Jan 18 '17 at 14:52
  • If you print your array with `print_r()` it will always print the array keys. You might want to use `implode()` or some simple loop with `echo`. – Rizier123 Jan 18 '17 at 14:52
  • `$newList` is already in that format. Dont understand whats wrong. – CodeGodie Jan 18 '17 at 14:52
  • Else `array(8085,8087,8086,8042);` is equal to `array(0=>8085,1=>8087,2=>8086,3=>8042);` – JustOnUnderMillions Jan 18 '17 at 14:53
  • if you want an array _"I want my array like this"_ Then you will always have keys. – CodeGodie Jan 18 '17 at 14:59
  • @CodeGodie Thanks for your suggestion. I can understand that If I print it then it will display key. Nino Škopac gave me nice answer. – Lemon Kazi Jan 18 '17 at 15:06
  • 1
    @LemonKazi awesome.. yea I was a bit thrown off initially. Glad you were able to understand. – CodeGodie Jan 18 '17 at 15:15
  • This is the ONLY way I found to do it - using eval: function create_keyless_array($array_in,$key) { $str=''; foreach($array_in as $r) { if($str) $str.=',"'.$r[$key].'"'; else $str='array("'.$r[$key].'"'; } if($str) { $str .=')'; echo $str; //exit; eval('$simple_array='.$str.';'); } else $simple_array=array(); return $simple_array; } – Ian Dec 19 '19 at 17:48

3 Answers3

1

print_r function will output the keys. even if you use array_values the array still have indexes as keys.

Just output the the array manually using echo and implode (implode will join array values into a single string using the first parameter character):

echo implode(',', $newList);
Ron Dadon
  • 2,666
  • 1
  • 13
  • 27
  • I dont think he wants the string. I believe he has the correct result already. It seems that hes confused about the keys shown when using `print_r` – CodeGodie Jan 18 '17 at 14:55
  • @CodeGodie maybe, his question was `display without key`, so I guess he wanted to display it, unless he will correct his question. – Ron Dadon Jan 18 '17 at 14:57
  • @RonDadon sorry if I put any mistake in my question. I just wanted array but implode will make it string. – Lemon Kazi Jan 18 '17 at 15:09
  • LemonKazi so has @CodeGodie said, you already have that array, there is nothing you should do – Ron Dadon Jan 18 '17 at 15:11
  • Yes but if that display like it array (0=> then it was good. Like Nino Škopac answer has. He got my point. Thanks for your suggestion. – Lemon Kazi Jan 18 '17 at 15:13
1

You're looking for array_column (which is available as of PHP 5.5):

$productIds = array_column($myList, 'product_id');

This gives you:

var_export($productIds);

array (
  0 => 8085,
  1 => 8087,
  2 => 8086,
  3 => 8042,
)

Which is exactly what you want:

var_dump($productIds === array(8085,8087,8086,8042)); // bool(true)
The Onin
  • 5,068
  • 2
  • 38
  • 55
1

Arrays will always have keys. If you want an array, you can get all the values, turn them into one comma separated string, and place that into an array:

$productIds = [implode(',', array_column($myList, 'product_id'))];
var_dump($productIds);

// RESULT:
// array (size=1)
//   0 => string '8085,8087,8086,8042' (length=19)
CodeGodie
  • 12,116
  • 6
  • 37
  • 66