3

I got a bit probs how to echo print_r() output array.

Array ( 
    [[email protected]] => Array ( 
        [0] => 70,80 
        [1] => 90,100 
    ) 
    [[email protected]] => Array ( 
        [0] => 10 
    ) 
)
function array_combines($arr1,$arr2) { 
   $out = array(); 
   foreach($arr1 as $key1 => $value1)    { 
    $out[$value1][] = $arr2[$key1]; 
   } 
   return $out;
} 
print_r(array_combines($a,$b));

I expecting echo:

key: [email protected]
value: 70,80 90,100

key: [email protected]
value: 10
4
  • Your expected is not array , it's json (object) Commented Jul 16, 2019 at 6:22
  • What are $a and $b? Commented Jul 16, 2019 at 6:24
  • 3
    Don't use print_r() if you want a custom output format. Commented Jul 16, 2019 at 6:32
  • $a = ['[email protected]', '[email protected]', '[email protected]']; $b = ['70,80', '10', '90,100']; Commented Jul 16, 2019 at 6:54

2 Answers 2

4

Loop the array like shown below. The key is the email, then use implode() on the value

foreach ($array as $key => $value) {
    echo "key: " , $key , PHP_EOL;
    echo "value: " , implode(' ',$value) , PHP_EOL , PHP_EOL;
}

Output:-

key: [email protected]
value: 70,80 90,100

key: [email protected]
value: 10

Demo at:
https://3v4l.org/gXJcP or https://3v4l.org/rN9LV

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

Comments

3

First format your array to expected pattern using array_walk() and finally print using implode() the array by </br> or PHP_EOL glue. Example:

array_walk($arr, function (&$item, $key) { $item = "key: {$key}</br>value: " . implode(" ", $item); });

echo implode('</br></br>', $arr);

Demo

Comments

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.