0

I have an array:

array(
 array('w' => '100', 'h'=>'100'),
 array('w' => '200', 'h'=>'200'),
 array('w' => '300', 'h'=>'300')
)

I ned to create a string from this array that looks like:

[100, 100], [200, 200], [300, 300]

I've looked at:

array_values()

I use it by looping through each array to remove the key, but what would be the best way to make the entire string, with square brackets?

2
  • @panthro If you add a surrounding [...], it's perfectly fine JSON. Commented Jun 26, 2014 at 8:54
  • @deceze That's true, its only the removed surrounding [] Commented Jun 26, 2014 at 9:07

5 Answers 5

2

You can use implode to format along with some basic string concat like this:

$string='';
foreach($mainArray as $v)
{
    $string.='['.(implode(',',$v)).'],';
}
$string=substr($string,0,-1);

The last bit wioll remove the trailing comma from the foreach statement.

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

Comments

1

You just need to use json_encode():

$values = array(
     array('w' => '100', 'h'=>'100'),
     array('w' => '200', 'h'=>'200'),
     array('w' => '300', 'h'=>'300'),
);

$new_values = array();
foreach ($values as $value) {
    // put cast int @Tash:
    $new_values[] = json_encode(array((int)$value['w'], (int)$value['h']));
}

echo implode(',', $new_values); // [100,100],[200,200],[300,300]

6 Comments

This is not correct, your commented output is different from actual output, the actual output has quotes ["100","100"],["200","200"] how can I remove these?
@deceze thank you array values is much suitable, and for online code
Cast the the values to int like so: ... array('w' => (int) 100, 'h' => (int) 100), ... //etc
This answer is not correct as quotes appear around each string. Json encode is not the correct way to do this.
@user3729576 its okay now since tash pointed out to cast it to int, so it removes quotes
|
0
$tmp = array(
    array('w' => 100, 'h' => 100),
    array('w' => 200, 'h' => 200),
    array('w' => 300, 'h' => 300)
);
$str = '';
foreach ($tmp as $i => $value) {
    if ($i == 0)
        $str = '[' . implode(',', $value) . ']';
    else
        $str.= ', [' . implode(',', $value) . ']';
}
var_dump($str);

Comments

0

That is Json.

Try this, even though not so clean.

<?php
$ace=array(
 array('w' => 100, 'h'=>100),
 array('w' => 200, 'h'=>200),
 array('w' => 300, 'h'=>300)
);
$json="";
foreach($ace as $key=>$value){
    $json.= json_encode(array($value['w'],$value['h'])) . ",";
}
echo substr($json,0,-1);//remove last comma

2 Comments

It's not JSON! JSON would have quotes around each string
Try this and you will see its a json. <?php $ace=array( array('w' => 100, 'h'=>100), array('w' => 200, 'h'=>200), array('w' => 300, 'h'=>300) ); foreach($ace as $key=>$value){ $arr[]=array($value['w'],$value['h']); } echo json_encode($arr); The values of 'w' and 'h' are integers so they are not quoted.
0
$array=array(
 array('w' => 100, 'h'=>100),
 array('w' => 200, 'h'=>200),
 array('w' => 300, 'h'=>300)
);
$str='';
foreach ($array as $key=>$val) {
    $str .= '['.$val['w'].','.$val['h'].'],';
}
$str = substr($str,0,-1);
echo $str;

2 Comments

do please explain how this piece of code does the work, so it will be helpful to community. see How to Answer
all you need to add to put your array in it, and rest you can see, i can polish it a bit so that you can understand better

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.