0

For example: I have sample array with dynamic values.

$sample = array('a.col1','a.col2','b.col1','c.col1','c.col2','d.col5',......);

where a,b,c,d are table alias.

How can I separate each table columns into separate individual arrays, how can I get below output ?

$a = array('col1','col2');
$b = array('col1');
$c = array('col1','col2');
$d = array('col5');

Update : no need of alias name in array values.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Rahul_Dange
  • 252
  • 2
  • 12

4 Answers4

3

You can use array_walk with explode

array_walk($sample, function($v) use (&$r){
   $arr      = explode('.', $v);
   $alias    = isset($arr[0]) ? $arr[0] : '';
   $r[$alias][] = isset($arr[1]) ? $arr[1] : '';
});

After that you can use EXTRACT to separate them in individual array

 extract($r);//// You will have $a, $b, $c ,$d with each alias

Working example :- https://3v4l.org/HSHcK

Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
2

I don't like to promote variable variables, but it does satisfy your requirement of individualized variables in a very concise way.

strstr() with a 3rd parameter of true will isolate the table alias which is then used as the array variable's name.

Code (Demo)

$sample = array('a.col1','a.col2','b.col1','c.col1','c.col2','d.col5');

foreach ($sample as $column) {
    ${strstr($column, '.', true)}[] = $column;
}

var_export($a);
var_export($b);
var_export($c);
var_export($d);

Output:

array (
  0 => 'a.col1',
  1 => 'a.col2',
)array (
  0 => 'b.col1',
)array (
  0 => 'c.col1',
  1 => 'c.col2',
)array (
  0 => 'd.col5',
)

After question update...

You can split your strings into two halves using the dot as the delimiter. I can't imagine more than one dot in the strings, but I use the 3rd parameter of explode() to improve clarity and allow the function to finish quickly.

Rather than calling list() to declare variables for the exploded values, I am using square brace syntax to spare iterated function calls.

Code: (Demo)

foreach ($sample as $column) {
    [$alias, $column] = explode('.', $column, 2);
    $$alias[] = $column;
}

Output:

array (
  0 => 'col1',
  1 => 'col2',
)array (
  0 => 'col1',
)array (
  0 => 'col1',
  1 => 'col2',
)array (
  0 => 'col5',
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • How if alias is dynamic for exmaple "cde." instead of only "c." ? – Rahul_Dange Nov 18 '19 at 07:45
  • That will make no difference to my solution. You can test my solutions by changing the input values in my demo links. The other speed posted answers do not meet your clear requirements. – mickmackusa Nov 18 '19 at 07:46
  • Normally I refuse to answer questions that fail to include a coding attempt, but because other volunteers have provided inadequate solutions, I had to adjust my principles so that you received a correct answer. – mickmackusa Nov 18 '19 at 07:53
1

Create a key with the first 2 characters of the string. Keep putting in an array and finally print the array.

if the alias is a combination of letters, either explode with . and use the first part as key. (as in implementation)

$sample = array('a.col1','a.col2','b.col1','c.col1','c.col2','d.col5');
$newArray=array();
foreach( $sample as $value){
  $key =  (explode(".",$value))[0];  // get the first part before dot.
  $newArray[$key."."][] =$value;  // not I am appending a dot
}


echo "<pre>";
print_r($newArray);

output:

Array
(
    [a.] => Array
        (
            [0] => a.col1
            [1] => a.col2
        )

    [b.] => Array
        (
            [0] => b.col1
        )

    [c.] => Array
        (
            [0] => c.col1
            [1] => c.col2
        )

    [d.] => Array
        (
            [0] => d.col5
        )

)
Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78
  • How if alias is "cde" instead of only "c" ? – Rahul_Dange Nov 18 '19 at 07:30
  • OP says: "_How can i seperate each table colummns into seperate individual arrays?_" Please see the expected result in the question. – mickmackusa Nov 18 '19 at 07:37
  • @Rahul_Dange that's just an idea, string can be broken based on `.` using strpos – Danyal Sandeelo Nov 18 '19 at 07:56
  • @mickmackusa each item of the array shows the required array right? he can further use {} to dynamically generate varibales, like `$array{$i}` if he needs the values in exact variables.. no? – Danyal Sandeelo Nov 18 '19 at 07:57
  • The point is, it is your responsibility to deliver the expected result -- if you are going to post an answer. Of course, I know how to do this bit, but we are here to educate the OP and future researchers who do not know how to do these things. – mickmackusa Nov 18 '19 at 08:02
  • true, I actually try to give some hints so that OP may start getting the idea and use his own tricks to solve the issue. We know that storing array in variables won't be that useful when you have a complete dataset ready – Danyal Sandeelo Nov 18 '19 at 08:05
0

The other way:


$sample = array('a.col1','a.col2','b.col1','c.col1','c.col2','d.col5');
$new = [];
foreach($sample as $value){
    $arr = explode(".", $value);
    if(array_key_exists($arr[0],$new)){
        array_push($new[$arr[0]], $arr[1]);
    }else{;
        $new[$arr[0]][] = $arr[1];
    }
}

Output: https://3v4l.org/eYKDc

MinIsNghia
  • 64
  • 3
  • OP says: "_How can i seperate each table colummns into seperate individual arrays?_" Please see the expected result in the question. – mickmackusa Nov 18 '19 at 07:53
  • Code-only answers are low value on Stackoverflow because they do very little to educate/empower thousands of future researchers. – mickmackusa Nov 18 '19 at 07:54