Explodes with name indexes (associative)

Asked

Viewed 426 times

8

How can I make a explode in a variable where I can then access it through nominal index, and nay for numerical index?

Follow the example to be clearer:

<?php

// Aqui acesso pelo índice
$user = "Diego:25:RJ";
$info = explode(":",$user);
echo "O nome é ".$info[0];

?>

Instead of using:

echo "O nome é ".$info[0];

I would like to use:

echo "O nome é ".$info["nome"];

In my real example, I would solve a certain situation where the insertion or removal of certain information could be bad in a use with the use of numerical index.

  • 1

    it’s funny, I try to understand why the question was negative. I think it was more by a taste criterion than by the policy of the site.

  • I confess that I also did not understand why, it seems to me a valid question, trying to simplify a process, although there is no built-in explode(). I already spent my vows today, otherwise I’d give +1

  • 1

    @Dichrist If you want to put the values in variables, da to do so: http://ideone.com/OV9QMs

  • 2

    @stderr I think this solution is very good too, I should put it as an answer. taking into account that is exactly this (facilitate the process) that the AP seems to want

5 answers

10

Can do with array_combine to associate each key to the corresponding value:

$user = "Diego:25:RJ";
$keys = array('nome', 'idade', 'estado');
$info = array_combine($keys, explode(":",$user));
print_r($info); // Array ( [nome] => Diego [idade] => 25 [estado] => RJ ) 

This way you must be sure you have the same number of keys ($keys) and values (explode(":",$user))

7

It is the same logic of @Miguel, (who already received my +1), I only posted to propose a rearrangement in the structure, in case will vary the amount of data depending on the occasion:

$user = 'Diego:25:RJ';
$keys = 'Nome:Idade:Estado';

$info = array_combine( explode( ':', $keys ), explode( ':', $user ) );
print_r($info);

See working on IDEONE.


Note that if you restructure your data as JSON, it can get much simpler:

$json = '{"Nome":"Diego","Idade":25,"Estado":"RJ"}';
print_r( json_decode( $json, true ) );

Exit

Array
(
    [Nome] => Diego
    [Idade] => 25
    [Estado] => RJ
)

See working on IDEONE.

6


In addition to the alternatives with explode, you can use preg_match. Simply inform the nominal indices in the rule itself.

$string = 'Diego:25:RJ';
preg_match('/(?P<nome>\w+):(?P<idade>\d+):(?P<estado>\w+)/', $string , $matches );


echo $matches['nome'];
echo $matches['idade'];
echo $matches['estado'];

Example in the ideone

  • 2

    +1 this tb is good.

  • 1

    this looks very good! I will test!

  • 1

    Although I ordered it with a bang, it was the alternative that seemed to suit me better.

  • @Dichrist, I saw the explode as something generic, with the intention of dividing the parts independently of the form, so brought another alternative. :)

4

3

You can do it using a array()

$user = "Diego:25:RJ";
$info = explode(":",$user);
echo "O nome é ".$info[0];

$infoArr = array('nome'=>$info[0], 'idade'=>$info[1], 'estado'=>$info[2]);

print_r($infoArr); 
echo $infoArr['nome'];

Return:

Array
(
    [nome] => Diego
    [idade] => 25
    [estado] => RJ
)
O nome e Diego
  • I don’t think that would solve my problem. Imagine if I go to the user variable and enter information before age. It would change the information. The idea would be to not use the numeric indexes.

  • 1

    So try to be more specific.

  • André, that’s already in the question. I said I didn’t want to use numerical indexes.

  • 1

    But if you don’t define what the numeric index would be, I don’t think I can tell what position he’s in

  • 2

    And I also understood that this is exactly what @Dichrist doesn’t want.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.