6

This is my associative array .

Array ( [month] => June [sale] => 98765 ) 
Array ( [month] => May [sale] => 45678 ) 
Array ( [month] => April [sale] => 213456 ) 
Array ( [month] => August [sale] => 23456 ) 
Array ( [month] => July [sale] => 12376 )

I want to convert it into two strings, like this ["June", "May", "April", "August", "july"]

and another one like this [98765 , 45678 , 213456 , 23456 , 12376 ]

I have used Implode function but I think I am missing something. Can anybody please help ?

Manish Patel
  • 3,648
  • 1
  • 14
  • 22
Nishank Magoo
  • 301
  • 4
  • 12

4 Answers4

7

Simple,Use array_column():-

$month_array = array_column($array,'month');
$sale_array = array_column($array,'sale');

Output:- https://3v4l.org/ancBB

Note:- If you want them as strings then do like below:-

echo implode(',',array_column($array,'month'));

echo implode(',',array_column($array,'sale'));

Output:- https://3v4l.org/F17AP

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
0

You can look into the following code:-

$arr['month'] = array('June','July'); 

$arr['sale'] = array('123','234'); 

$strMonth = '["'.(implode('","', $arr['month'])).'"]';


$strSale = '['.(implode(', ', $arr['sale'])).']';


print_r($strMonth );

print_r($strSale );

And Output:-

["June","July"]
[123, 234] 
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
0

You are effectively asking for array columns to be json_encoded.

To improve efficiency, use a foreach() to populate both arrays in one pass. This has half the time complexity of making two separate array_column() calls. I'll use a body-less loop with destructuring syntax for fun.

Code: (Demo)

$array = [
    ['month' => 'June', 'sale' => 98765],
    ['month' => 'May', 'sale' => 45678],
    ['month' => 'April', 'sale' => 213456],
    ['month' => 'August', 'sale' => 23456],
    ['month' => 'July', 'sale' => 12376],
];

$months = [];
$sales = [];
foreach ($array as ['month' => $months[], 'sale' => $sales[]]);
echo json_encode($months);
echo "\n---\n";
echo json_encode($sales);

Output:

["June","May","April","August","July"]
---
[98765,45678,213456,23456,12376]
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
-2

You can simply do this by doing:

$strMonth = implode(', ', $arrVarName['month']);
$strSale = implode(', ', $arrVarName['sale']);

Hope this helps!

  • 5
    This wont work as it's multi-dimensional, you have to use `array_column` Like this `[0=>['month'=>'June','sale'=>98765],1=>[...]]`, I suppose you could do it with a loop too, but why bother. – ArtisticPhoenix Nov 01 '17 at 10:36
  • @ArtisticPhoenix I suppose Nishank said Associative array! If multi-dimensional then sure you have to use array_column() and then implode it into string. – Khattu Developer Nov 01 '17 at 10:46
  • "_I suppose you could do it with a loop too, but why bother._" @ArtisticPhoenix [Because using a single loop to populate two arrays cuts the time complexity in half.](https://stackoverflow.com/a/72232194/2943403) – mickmackusa May 15 '22 at 07:54