1

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?

3 Answers 3

0

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;
}
}
?>
1
  • So you have html/string output in that loop? And want to create $myarray like shown out of it? Commented Dec 16, 2011 at 17:10
0

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;
    }
}
1
  • 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. Commented Dec 16, 2011 at 17:11
0
$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";
}
5
  • $array2[$key] will log a notice if the key isn't found. Use array_key_exists instead Commented Dec 16, 2011 at 17:09
  • @adam, good point. I meant to use isset() but array_key_exists() would also work. Commented Dec 16, 2011 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 Commented Dec 16, 2011 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 );. Commented Dec 16, 2011 at 18:14
  • @brian oups...thank you for your answer i have to check again if it works. Commented Dec 17, 2011 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.