How to convert a associative array into a string using implode function [duplicate]
Asked Answered
O

4

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 ?

Opaline answered 1/11, 2017 at 10:32 Comment(3)
you mean 2 new arrays right?Rileyrilievo
Two strings . "June", "May", "April", "August", "july" 98765 , 45678 , 213456 , 23456 , 12376Opaline
@NishankMagoo please mark the right answer. the one you marked as an answer to your question will not work at all.Kelantan
C
-2

You can simply do this by doing:

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

Hope this helps!

Chambers answered 1/11, 2017 at 10:35 Comment(3)
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.Yates
@Yates I suppose Nishank said Associative array! If multi-dimensional then sure you have to use array_column() and then implode it into string.Chambers
"I suppose you could do it with a loop too, but why bother." @Yates Because using a single loop to populate two arrays cuts the time complexity in half.Adelaideadelaja
K
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

Kelantan answered 1/11, 2017 at 10:34 Comment(0)
C
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] 
Charitacharitable answered 1/11, 2017 at 10:45 Comment(2)
I dont think so its wrost. I just give output as he wants. And don't judge me @Alive . ThanksCharitacharitable
You have misrepresented the asker's input array structure.Adelaideadelaja
A
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]
Adelaideadelaja answered 13/5, 2022 at 15:42 Comment(0)
C
-2

You can simply do this by doing:

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

Hope this helps!

Chambers answered 1/11, 2017 at 10:35 Comment(3)
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.Yates
@Yates I suppose Nishank said Associative array! If multi-dimensional then sure you have to use array_column() and then implode it into string.Chambers
"I suppose you could do it with a loop too, but why bother." @Yates Because using a single loop to populate two arrays cuts the time complexity in half.Adelaideadelaja

© 2022 - 2024 — McMap. All rights reserved.