0

I've have the following arrays:

Array ( [0] => Array ( [0] => Bob 
                       [1] => Freddy 
                     ) 
        [1] => Array ( [0] => IT Dev 
                       [1] => Programmer 
                     ) 
        [2] => Array ( [0] => 123 
                       [1] => 23423 
                     ) 
      )

I'm trying to join the arrays together so it would look like the following:

Bob - IT Dev - 123

Freddy - Programmer - 23423

I've been messing with implode functions but in all reality I have no idea how to achieve this in PHP

Any help would be greatly appreciated.

Many thanks

Max

4 Answers 4

1

If your array is called $myArray, then:

foreach ($myArray as $row)
{
  $string1 .= $row[0]."-";
  $string2 .= $row[1]."-";
}

echo $string1."<br>";
echo $string2."<br>";
Sign up to request clarification or add additional context in comments.

3 Comments

ACTUALLY what if there were more than 2 arrays e.g. [0],[1],[2],[3] etc... is there away to loop through them?
would that be like 3 arrays inside 1 array? or 1 array inside 1 array inside 1 array? If you want, you can edit your answer with the new situation and ill take a look at it
@MaxThorley oh and if this answer is the one that worked for you, please accept is (with a green checkmark) so that future users know it worked for you :)
0

Let's say this is your array:

$array = array(
    array('Bob', 'Freddy'),
    array('IT Dev', 'Programmer'),
    array('123', '23423')
);

You'll have to loop through each array, to join them into a sentence.

$result = array();
foreach($array as $data){
    foreach($data as $index => $value){
        if(!isset($result[$index]))
            $result[$index] = $value;
        else
            $result[$index] .= " - " . $value;
    }
}

Now if you var_dump the $result:

array(2) {
    [0]=>
        string(18) "Bob - IT Dev - 123"
    [1]=>
        string(27) "Freddy - Programmer - 23423"
}

If you want to access an individual sentence, you can do:

echo $result[0]; // Bob - IT Dev - 123

Comments

0

Just use array_column and implode to get the output.

Array:

$arr = array(
    array('Bob', 'Freddy'),
    array('IT Dev', 'Programmer'),
    array('123', '23423')
);

mechanism:

echo implode(" - ", array_column($arr, 0)); //Bob - IT Dev - 123
echo implode(" - ", array_column($arr, 1)); //Freddy - Programmer - 23423

Comments

0

I have two solid methods for you. The first is a "variadic approach" (... splat operator) and the second is a classic foreach loop leveraging array_column().

Code: (Demo)

$array=[
    ['Bob','Freddy'],
    ['IT Dev','Programmer'],
    [123,23423]
];

// Use this to see that both methods are flexible and produce the same result,
// so long as all subarrays have equal length.
// If there are differences in length, depending where the hole is, the results
// between the two methods will vary slightly.
// (The 1st method best preserves the intended output structure.)
/*$array=[
    ['Bob','Freddy','Jane'],
    ['IT Dev','Programmer','Slacker'],
    [123,23423,0],
    ['more1','more2','more3']
];*/

// this one requires php5.6 or better
$imploded_columns=array_map(function(){return implode(' - ',func_get_args());},...$array);
var_export($imploded_columns);

echo "\n\n";
unset($imploded_columns);

// or you can go with a sub-php5.6 version:
foreach($array[0] as $k=>$v){
    $imploded_columns[]=implode(' - ',array_column($array,$k));
}
var_export($imploded_columns);

Output from both methods:

// variadic method
array (
  0 => 'Bob - IT Dev - 123',
  1 => 'Freddy - Programmer - 23423',
)

// foreach method
array (
  0 => 'Bob - IT Dev - 123',
  1 => 'Freddy - Programmer - 23423',
)

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.