W
W
WQP2014-07-25 17:29:41
PHP
WQP, 2014-07-25 17:29:41

How to build a multilevel array correctly?

Hello, I've been scratching my head for a very long time there is such a text (always different):

Название 1 | http://toster.ru/adsfasrew
Название 2 | http://toster.ru/eqweqsafds
...
Название 10 | http://toster.ru/gdftgrwet

How can I turn it into an array like this:
Array
(
 [0] => Array
  (
   [name] => 'Название 1'
   [link] => 'http://toster.ru/adsfasrew'
  )

 [1] => Array
  (
   [name] => 'Название 2'
   [link] => 'http://toster.ru/eqweqsafds'
  )
...
 [9] => Array
  (
   [name] => 'Название 10'
   [link] => 'http://toster.ru/gdftgrwet'
  )

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vdem, 2014-07-25
@WQP

$rows = explode("\n", $text);
$result = array();
foreach($rows as $row) {
    list($name, $link) = explode('|', $row);
    $result[] = array('name' => trim($name), 'link' => trim($link));
}

M
Mykola, 2014-07-25
@iSensetivity

$lines = file('text_file.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach($lines as $line){
$pices = explode('|', $line);
$new_array[] = array('name' => trim($pices[0]), 'link' => trim($pices[1]));
}

I put it on my knee.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question