1

I have this array

[0] => Array
        (
            [0] => My name
            [1] => is
            [2] => John
        )

[1] => Array
        (
            [0] => My name is Jane
        )

how to achieve this kind of output see below. If the array count is greater than one I want to combine them as one.

[0] => Array
        (
            [0] => My name is John
        )

[1] => Array
        (
            [0] => My name is Jane
        )

here is my code but it didn't work

foreach ($myArr as $key => $value) {
    if (count($myArr[$key]) > 1) {
        foreach ($value as $k => $v) {
            $myArr[$key] .= $v; 
        }
    }
}

thanks

3
  • What is it returning right now? Commented Feb 10, 2018 at 4:08
  • I got this error @AmitMerchant Notice: Array to string conversion Commented Feb 10, 2018 at 4:10
  • $myArr[$key] is still a reference to the inner array. Commented Feb 10, 2018 at 4:11

3 Answers 3

2

Why not use implode?

$data = [['my name', 'is', 'John'],['my name is Jane']];
$results = [];
foreach ($data AS $id=>$datum)
    if (count($datum) > 1)
        $results[$id] = implode($datum, ' ');
    else
        $results[$id] = $datum[0];

results:

array(2) {
  [0]=>
  string(15) "my name is John"
  [1]=>
  string(15) "my name is Jane"
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thumbs up for using implode().
Nice, should work with normal array too, does seem like associative array is redundant.
``` <?php $data = [['my nameJohn'],['my name is Jassadasdsadsadane']]; $results = []; foreach ($data AS $datum) if (count($datum) > 1) $results[] = implode($datum, ' '); else $results[] = $datum[0]; echo "<pre>"; print_r($results); echo "</pre>";
My code was just storing the results in the same key as the source. Because the source is a numerically-indexed array, you could theoretically ditch the [$id] assignment, but it's no slower and will inherently respect arbitrary key values, which could be relevant if you had filtered the source array for some reason to remove some entries, or had string keys
$results is still the same as in OP. Op did not use any associative array
|
1

I suppose it would be like this:

foreach ($myArr as $key => $value) {
    if (count($myArr[$key]) > 1) {
        $myArr[$key][0] = '';  
        foreach ($value as $k => $v) {
            $myArr[$key][0] .= $v; 
        }
        array_splice($myArr[$key], 1, count($myArr[$key])-1);
    }
}

5 Comments

the output would be the same
Did you check it?
Yes I checked it
<?php $data = [['my nameJohn'],['my name is Jassadasdsadsadane']]; $results = []; foreach ($data AS $datum) if (count($datum) > 1) $results[] = implode($datum, ' '); else $results[] = $datum[0]; echo "<pre>"; print_r($results); echo "</pre>";
1

I suppose it would be like this:

$ary[0] = Array("My name","is","John");
$ary[1] = Array( "My name is Jane" );
$i=0;       
foreach ($ary as $ar_item)  {
    $merged="";
foreach  ($ar_item as $ar_subitem)  
{
    $merged=$merged.$ar_subitem." ";
}
$ary[$i]=$merged;
$i++;
}
var_dump($ary);

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.