Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a loop in php that returns this result :

  • name 1 => value x
  • name 2 => value y
  • name 3 => value x
  • name 1 => value y
  • name 2 => value y

I want to create an array like this :

$myarray=array('name 1' =>'value x, value y','name 2'=>'value y',name 3=>'value x')

So with this array, I can manipulate it like this :

foreach($myarray as $key=>$item){
    echo $key.'=>'.$item;
}

The result:

  • name 1 => value x,value y
  • name 2 => value y
  • name 3 => value x

How can I do this?

share|improve this question

3 Answers

It looks awful because php doesn't have native lists. You shouldn't use a hashmap (associative array) when the keys can be the same. I haven't tested the following but something like this:

<?
$arr = Array(
Array('n1',Array('x','y'))
, Array('n2',Array('x'))
, Array('n3',Array('y'))
);

foreach($arr as $value){
list($name,$values) = $value;
foreach($values as $value){
echo "$name -> $value".PHP_EOL;
}
}
?>
share|improve this answer
So you have html/string output in that loop? And want to create $myarray like shown out of it? – djot Dec 16 '11 at 17:10
yes it s string output – adokara Dec 16 '11 at 17:40

Create an empty array for the output, then for each input value check to see if the key already exists.

$result = array();

foreach($myarray AS $key=>$value) {
    if(array_key_exists($key, $result)) {
        // if the key exists in $result, add the value to the existing value
        $result[$key] += ', ' + $value;
    } else {
        // otherwise, just create it
        $result[$key] = $value;
    }
}
share|improve this answer
That doesn't handle the unique part. name 2 => value y occurs twice, so this would print "name 2 => value y, value y" which does not match the expected output. – Brian Dec 16 '11 at 17:11
$array2 = array();
foreach ( $original_array as $key => $value )
{
  // Edited to address @adam's comment
  if ( !array_key_exists( $key, $array2 ) ) $array2[$key] = array();
  $array2[$key][] = $value;
}

foreach ( $array2 as $key => $values )
{
  $values_str = join( ', ', array_unique( $values ) );
  print "$key => $values_str";
}
share|improve this answer
$array2[$key] will log a notice if the key isn't found. Use array_key_exists instead – adam Dec 16 '11 at 17:09
@adam, good point. I meant to use isset() but array_key_exists() would also work. – Brian Dec 16 '11 at 17:19
@adam your solution return me this kind of result (with print_r) : Array ( [name1] => Array ( [0] => value_x ) [name2] => Array ( [0] => value_y [1] => value_y ) ) In name2, value y is not unique – adokara Dec 16 '11 at 17:34
@adokara, I think you meant to address me instead of Adam, and you are correct if you dump $array2. The items in the array are not unique until they are printed in the second loop. So if you wanted to end up with an array instead of textual output, you could just replace the second foreach loop with $final_array = array_map( 'array_unique', $array2 );. – Brian Dec 16 '11 at 18:14
@brian oups...thank you for your answer i have to check again if it works. – adokara Dec 17 '11 at 9:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.