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 ?

3
  • you mean 2 new arrays right? Commented Nov 1, 2017 at 10:33
  • Two strings . "June", "May", "April", "August", "july" 98765 , 45678 , 213456 , 23456 , 12376 Commented Nov 1, 2017 at 11:34
  • @NishankMagoo please mark the right answer. the one you marked as an answer to your question will not work at all. Commented Aug 24, 2023 at 5:48

4 Answers 4

8

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

Sign up to request clarification or add additional context in comments.

Comments

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] 

2 Comments

I dont think so its wrost. I just give output as he wants. And don't judge me @Alive . Thanks
You have misrepresented the asker's input array structure.
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]

Comments

-2

You can simply do this by doing:

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

Hope this helps!

3 Comments

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 I suppose Nishank said Associative array! If multi-dimensional then sure you have to use array_column() and then implode it into string.
"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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.