1

I have associative arrays like this (stored in $array):

Array
(
    [0] => Array
        (
            [one] => some text1
            [two] => some paragraph here1.
        )

    [1] => Array
        (
            [one] => some text2
            [two] => some paragraph here2.        
        )

    [2] => Array
        (
            [one] => some text3
            [two] => some paragraph here3.        
        )

    [3] => Array
        (
            [one] => some text4
            [two] => some paragraph here4.        
        )

    [4] => Array
        (
            [one] => some text5
            [two] => some paragraph here5.        
        )

)

Now, I want to store the result of them like this:

$first = 'some text1, some text2, some text3, some text4, some text5';

$second = 'some paragraph here1. some paragraph here2. some paragraph here3. some paragraph here4. some paragraph here5.';

Struggling a lot I got a solution like this:

$first= '';
$second = '';
for($i = 0; $i<count($array); $i++){
    $first .= $array[$i]['one'] . ($i == count($array) - 1 ? '': ',');
    $second .= $array[$i]['two'];
}

echo $first;
echo $second;

But I hope there's already a built-in php function which split assoc arrays into two different arrays.

Community
  • 1
  • 1
Navin Rauniyar
  • 10,127
  • 14
  • 45
  • 68
  • You merely need to [implode while transposing your data](https://3v4l.org/iLo1d). No need to call `array_column()` twice. – mickmackusa Jul 26 '22 at 04:01

4 Answers4

6

For PHP version > 5.5. You can simply use array_column along with implode function like as

echo $first = implode(',',array_column($your_array,'one'));
echo $second = implode(',',array_column($your_array,'two'));

For lower version you can use array_map like as

echo $first = implode(',',array_map(function($v){ return $v['one'];} ,$your_array));
echo $second = implode(',',array_map(function($v){ return $v['two'];},$your_array));
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
1
foreach ( $array as $data ) {
   $first .= $data['one'].',';
   $two .= $data['two'].',';
}

$firstdata = rtrim($first,',');
$seconddata = rtrim($two, ',');
Krishna Gupta
  • 695
  • 4
  • 15
1
 $arrFirst = array_column($array,'one');
 $arrSecond = array_column($array,'two');

 $first = implode(',',$arrFirst);
 $second = implode(',',$arrSecond);

if array_column() doesn't exits use this

if (! function_exists('array_column')) {
    function array_column(array $input, $columnKey, $indexKey = null) {
        $array = array();
        foreach ($input as $value) {
            if ( ! isset($value[$columnKey])) {
                trigger_error("Key \"$columnKey\" does not exist in array");
                return false;
            }
            if (is_null($indexKey)) {
                $array[] = $value[$columnKey];
            }
            else {
                if ( ! isset($value[$indexKey])) {
                    trigger_error("Key \"$indexKey\" does not exist in array");
                    return false;
                }
                if ( ! is_scalar($value[$indexKey])) {
                    trigger_error("Key \"$indexKey\" does not contain scalar value");
                    return false;
                }
                $array[$value[$indexKey]] = $value[$columnKey];
            }
        }
        return $array;
    }
}
Ninju
  • 2,522
  • 2
  • 15
  • 21
0

Use array_column() and implode()

array_column() function works in > PHP 5.5.0

Working example:

<?php
$arr = array(
   array('one' => 'some text1', 'two' => 'some paragraph here1'),
   array('one' => 'some text2', 'two' => 'some paragraph here2'),
   array('one' => 'some text3', 'two' => 'some paragraph here3'),
   array('one' => 'some text4', 'two' => 'some paragraph here4'),
   );

$firstS = implode(', ', array_column($arr, 'one'));
$secondS = implode('. ', array_column($arr, 'two'));
print_r($firstS);
echo "\n\n";
print_r($secondS);
?>

See it working live

Explanation:

1) You need array columns.

2) Get array columns one and two in two separate arrays.

3) impode() these arrays to get desired output.

If your PHP version is less than 5.5.0, you need to write a custom function for array_column().

So that it will not return Fatal Error.

Following is the code:

<?php
if (! function_exists('array_column')) {
    function array_column(array $input, $columnKey, $indexKey = null) {
        $array = array();
        foreach ($input as $value) {
            if ( ! isset($value[$columnKey])) {
                trigger_error("Key \"$columnKey\" does not exist in array");
                return false;
            }
            if (is_null($indexKey)) {
                $array[] = $value[$columnKey];
            }
            else {
                if ( ! isset($value[$indexKey])) {
                    trigger_error("Key \"$indexKey\" does not exist in array");
                    return false;
                }
                if ( ! is_scalar($value[$indexKey])) {
                    trigger_error("Key \"$indexKey\" does not contain scalar value");
                    return false;
                }
                $array[$value[$indexKey]] = $value[$columnKey];
            }
        }
        return $array;
    }
}
Pupil
  • 23,834
  • 6
  • 44
  • 66