0

I need help creating an array within an array using this data. This data is already in an array like this:

[0] => META>>DisplayName=Donald Trump [1] => META>>[email protected] [2] => META>>EmployeeID=E13342 [3] => CLOUD>>DisplayName=Hillary Clinton [4] => CLOUD>>[email protected] [5] => CLOUD>>EmployeeID=E13423 [6] => AD>>DisplayName=Bernie Sanders [7] => AD>>[email protected] [8] => AD>>EmployeeID=E121233 

I'm trying to turn it into something like this:

array( [meta] => Array ( [DisplayName]=>Donald Trump [EmailAddress]=>[email protected] [EmployeeID]=>E666420 [EmployeeType]=>E ) ) 

What I have so far but it's not working:

$properties = array("DisplayName", "EmailAddress", "EmployeeID", "EmployeeType") $data = array(); foreach($output as $line) { $sep = explode(">>",$line); $data[$sep[0]] = array(); for ($x=0;$x<count($properties);$x++) { $split = explode("=",$sep[1]); $data[$sep[0]][$p] = $split[1]; } } 
12
  • What you've tried so far?CommentedJun 6, 2016 at 22:06
  • Is this supposed to contain multiple people?
    – georaldc
    CommentedJun 6, 2016 at 22:07
  • where should $p come from? change that to $properties[$x] and it should work
    – Jeff
    CommentedJun 6, 2016 at 22:08
  • @HenriqueBarcelos he's tried what he's showed!? Ain't that enough?
    – Jeff
    CommentedJun 6, 2016 at 22:09
  • 1
    @DobotJr i reverted your edit - showing what you have tried is important
    – Steve
    CommentedJun 6, 2016 at 22:11

4 Answers 4

2

This can be achieve like this:

$output = [ 'META>>DisplayName=Donald Trump', 'META>>[email protected]', 'META>>EmployeeID=E666420', 'META>>EmployeeType=E', ]; $result = array(); foreach ($output as $value) { $meta = explode('>>', $value); $property = explode('=', $meta[1]); $result[$meta[0]][$property[0]] = $property[1]; } var_dump($result); 

http://ideone.com/Twq9gr

3
  • OP said (in a comment) that 'meta' isn't always meta. So you might want to change that detail!
    – Jeff
    CommentedJun 6, 2016 at 22:24
  • 1
    changed $result['meta'][$property[0]] = $property[1]; to $result[$meta[0]][$property[0]] = $property[1]; Works perfectly..thanks man!
    – DobotJr
    CommentedJun 6, 2016 at 22:30
  • This is the cleanest and easiest to understand solution for me.
    – DobotJr
    CommentedJun 6, 2016 at 22:30
0

If the data is exactly as shown in your question, you can just iterate the array, use substr to eliminate the duplicate META>> part of the line, and explode on =:

$data = []; foreach($output as $line){ list($key, $value) = explode('=', substr($line, 6)); $data['meta'][$key]=$value; } 
3
  • getting error; sorry not familiar with with "list". syntax error, unexpected ')', expecting '('
    – DobotJr
    CommentedJun 6, 2016 at 22:14
  • "If the data is exactly as shown in your question" Yeah i kind of expected there where undeclared complications. Please edit your question to provide all the detail needed, including enough sample data to show the variations (eg multiple people, unwanted data)
    – Steve
    CommentedJun 6, 2016 at 22:16
  • Re the error, i missed $ from the beginning of the $value variable - corrected
    – Steve
    CommentedJun 6, 2016 at 22:20
0

Probably there is a best solution, but it will work:

<?php $output[] = 'META>>DisplayName=Donald Trump'; $output[] = 'META>>[email protected]'; $output[] = 'META>>EmployeeID=E666420'; $output[] = 'META>>EmployeeType=E'; $output[] = 'CLOUD>>DisplayName=Hillary Clinton'; $output[] = 'CLOUD>>[email protected]'; $output[] = 'CLOUD>>EmployeeID=E13423'; $output[] = 'AD>>DisplayName=Bernie Sanders'; $output[] = 'AD>>[email protected]'; $output[] = 'AD>>EmployeeID=E121233'; $properties = array("DisplayName", "EmailAddress", "EmployeeID", "EmployeeType"); $data = array(); foreach($output as $line) { $sep = explode(">>",$line); if(!isset($data[$sep[0]])){ $data[$sep[0]] = array(); } $split = explode("=",$sep[1]); foreach ($properties as $p) { if($split[0] == $p) { $data[$sep[0]][$p] = $split[1]; } } } echo "<pre>"; var_dump($data); echo "</pre>"; 

Output:

array(3) { ["META"]=> array(4) { ["DisplayName"]=> string(12) "Donald Trump" ["EmailAddress"]=> string(17) "[email protected]" ["EmployeeID"]=> string(7) "E666420" ["EmployeeType"]=> string(1) "E" } ["CLOUD"]=> array(3) { ["DisplayName"]=> string(15) "Hillary Clinton" ["EmailAddress"]=> string(12) "[email protected]" ["EmployeeID"]=> string(6) "E13423" } ["AD"]=> array(3) { ["DisplayName"]=> string(14) "Bernie Sanders" ["EmailAddress"]=> string(14) "[email protected]" ["EmployeeID"]=> string(7) "E121233" } } 
    0
    $data = array(); foreach ($output as $line) { list($keys, $value) = explode('=', $line); list($key, $attr) = explode('>>', $keys); $data[$key][$attr] = $value; } 
    5
    • that might be working, but feels like a hacky thing (the if $key = 'EmployeeType thing)
      – Jeff
      CommentedJun 6, 2016 at 22:23
    • The problem is the OP's data sample just goes line by line, so I had to define a stopping point where I would move to a new person
      – georaldc
      CommentedJun 6, 2016 at 22:24
    • Hmm, more details keep getting added. Now I see that meta can change
      – georaldc
      CommentedJun 6, 2016 at 22:26
    • just add it after that one foreach?! $output is one person only. To be fair, it's not statet in the question how the data really looks like, so that hack might be needed. - NO, after updated question.... can't add it after foreach. sry
      – Jeff
      CommentedJun 6, 2016 at 22:27
    • I've updated my answer to take into account changing keys
      – georaldc
      CommentedJun 6, 2016 at 22:31

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.