-2

I have a list that gets new rows from time to time, but I get it as a string. What I want to do is first separate each row, and insert it into an associative array. I'll show you some examples.

The string I receive looks something like this:

id1 name1 lastname1 age1 birthdate1 
id2 name2 lastname2 age2 birthdate2 
id3 name3 lastname3 age3 birthdate3 
id4 name4 lastname4 age4 birthdate4 

I need to separate each "row" and "column" and insert it into an associative array. So basically something like:

array{

    id1{
        'name' => 'name1',
        'lastname' => 'lastname1',
        'age' => 'age1',
        'birthdate' => 'birthdate1'
    },

    id2{
        'name' => 'name2',
        'lastname' => 'lastname2',
        'age' => 'age2',
        'birthdate' => 'birthdate2'
    },

    id3{
        'name' => 'name3',
        'lastname' => 'lastname3',
        'age' => 'age3',
        'birthdate' => 'birthdate3'
    },

    id4{
        'name' => 'name4',
        'lastname' => 'lastname4',
        'age' => 'age4',
        'birthdate' => 'birthdate4'
    }

}

I hope the examples make my goal more understandable.

Thanks!

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Quasar
  • 39
  • 6
  • 2
    you have to show us your code effort what you have tried so far... – Alive to die - Anant Oct 16 '19 at 11:57
  • 2
    something like [`explode()`](https://www.php.net/manual/en/function.explode.php) ? – Cid Oct 16 '19 at 12:00
  • 1
    Welcome! To ask [On Topic question](https://stackoverflow.com/help/on-topic), please read [Question Check list](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist) and the [perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and how to create a [Minimal, Complete and Verifiable Example](https://stackoverflow.com/help/mcve) and [take the tour](https://stackoverflow.com/tour). **We are very willing to help you fix your code, but we don't write code for you.** – Dave Oct 16 '19 at 12:00
  • 2
    explode at line breaks, loop over result, explode at space again, access individual values & insert into your array. – 04FS Oct 16 '19 at 12:00
  • [Explode PHP string by new line](https://stackoverflow.com/questions/3997336/explode-php-string-by-new-line) – Alive to die - Anant Oct 16 '19 at 12:04

4 Answers4

0

As suggested in the comments you will need to split the input into single lines and can then use explode or str_getcsv() to split each line into single values:

<?php

$testString = <<<EOF
id1 name1 lastname1 age1 birthdate1
id2 name2 lastname2 age2 birthdate2
id3 name3 lastname3 age3 birthdate3
id4 name4 lastname4 age4 birthdate4
EOF;

foreach (explode("\n", $testString) as $lineNo => $line) {
    var_dump(
        str_getcsv($line, ' ', '')
    );
}

See:

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
feeela
  • 29,399
  • 7
  • 59
  • 71
0

You need to use explode() along with foreach(), str_getcsv(),array_shift() and array_combine()

<?php


$testString = <<<EOF
id1 name1 lastname1 age1 birthdate1
id2 name2 lastname2 age2 birthdate2
id3 name3 lastname3 age3 birthdate3
id4 name4 lastname4 age4 birthdate4
EOF;

$stringArray = explode("\n", $testString);
$finalArray = array();
foreach ($stringArray as $lineNo => $line) {
    $strArray = str_getcsv($line, ' ', '');
    $firstVal = array_shift($strArray);
    $finalArray[$firstVal] = array_combine(
        array(
            'name',
            'lastname',
            'age',
            'birthdate'
        )
        ,$strArray 
    );
}

print_r($finalArray);

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

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
0

try this out :)

$string="id1 name1 lastname1 age1 birthdate1
id2 name2 lastname2 age2 birthdate2
id3 name3 lastname3 age3 birthdate3
id4 name4 lastname4 age4 birthdate4";

$data=array();
$string=explode("n", $string);

foreach ($string as $line) {
  $line=explode(" ", $line);
  $data[$line[0]]=[
    'name' => $line[1],
'lastname' => $line[2],
'age' => $line[3],
'birthdate' => $line[4]              
    ];
}

echo"<pre>";
  print_r($data);
  echo"</pre>";

0

You can use explode with foreach

$a1 = explode('
', $string);
foreach($a1 as $v){
  $v1 = explode(' ', trim($v));
  $r[$v1[0]] = ['name'=>$v1[1],'lastname'=>$v1[2],'age'=>$v1[3],'birthdate'=>$v1[4]];
}

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

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