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? Commented Jun 6, 2016 at 22:06
  • Is this supposed to contain multiple people? Commented Jun 6, 2016 at 22:07
  • where should $p come from? change that to $properties[$x] and it should work Commented Jun 6, 2016 at 22:08
  • @HenriqueBarcelos he's tried what he's showed!? Ain't that enough? Commented Jun 6, 2016 at 22:09
  • 1
    @DobotJr i reverted your edit - showing what you have tried is important Commented Jun 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

Sign up to request clarification or add additional context in comments.

3 Comments

OP said (in a comment) that 'meta' isn't always meta. So you might want to change that detail!
changed $result['meta'][$property[0]] = $property[1]; to $result[$meta[0]][$property[0]] = $property[1]; Works perfectly..thanks man!
This is the cleanest and easiest to understand solution for me.
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 Comments

getting error; sorry not familiar with with "list". syntax error, unexpected ')', expecting '('
"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)
Re the error, i missed $ from the beginning of the $value variable - corrected
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"
  }
}

Comments

0
$data = array();
foreach ($output as $line) {
    list($keys, $value) = explode('=', $line);
    list($key, $attr) = explode('>>', $keys);
    $data[$key][$attr] = $value;
}

5 Comments

that might be working, but feels like a hacky thing (the if $key = 'EmployeeType thing)
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
Hmm, more details keep getting added. Now I see that meta can change
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
I've updated my answer to take into account changing keys

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.