0

I am getting the first array by: // $data is fetched information from database // like select bookId from book;

$json  = json_encode($data);
$data = json_decode($json, true);

I have an array like this: Array of Associative array.

0 => 
    array (size=1)
      'bookId' => int 3
1 => 
    array (size=1)
      'bookId' => int 4
2 => 
    array (size=1)
      'bookId' => int 5
3 => 
    array (size=1)
      'bookId' => int 6

What i need is simple array:

0 => 3      
1 => 5
2 => 6
3 => 6

I do want to simple use a for loop. Is there any other good way to do this?

Parixit
  • 3,829
  • 3
  • 37
  • 61
Al-Alamin
  • 1,438
  • 2
  • 15
  • 34
  • `$simpleArray = array_column($originalArray, 'bookId');` – Mark Baker Dec 22 '15 at 08:04
  • @MarkBaker only for PHP 5.5+ – Mihai Matei Dec 22 '15 at 08:07
  • @MateiMihai which should be a minimum version PHP That you're running anyway these days... if poster doesn't specifically mention a version, then I have to assume they're running a supported version – Mark Baker Dec 22 '15 at 08:08
  • It should.. but it doesn't :) 5.3 and 5.4 are more than 60% https://wordpress.org/about/stats/ – Mihai Matei Dec 22 '15 at 08:10
  • foreach ($data as $key => $value) {$newArr[] = $value['bookis'];} try – devpro Dec 22 '15 at 08:11
  • Mark Baker thanks. This is what i needed. – Al-Alamin Dec 22 '15 at 08:24
  • @MateiMihai - so you're saying that I have to provide a solution that will work for all versions of PHP? How far back do I have to go? PHP 5.0? earlier? I can't suggest solutions that use functions/syntax that is available in all current versions of PHP, but not in earlier versions? – Mark Baker Dec 22 '15 at 09:08
  • @MateiMihai - IMO, if poster is running an earlier version of PHP than any of the supported versions, then the onus is on them to indicate that fact – Mark Baker Dec 22 '15 at 09:08
  • No.. Please don't understand me wrongly.. All I wanted to say was that the most frequent PHP versions are lower than 5.5 and I've just noticed that if there is no explicitly tag with the real version used, then the version is lower than 5.5. I really like your solution and for sure I am using it as well.. What I've commented first was just an addition for others to know in which version your solution works.. – Mihai Matei Dec 22 '15 at 09:10

0 Answers0