C
C
Cheizer2020-01-04 18:31:08
MongoDB
Cheizer, 2020-01-04 18:31:08

How to cut a string in a loop in PHP?

Friends, there is an array in which records of the form:
France | Paris
Japan | Tokyo
Germany | Berlin
Chech Republic | Praha-
Japan | Tokyo
Germany | Berlin
With grief, I wrote a code in half that leaves only unique records, with this order, but it took to cut the cities, so that it would turn out like this:
Chech Republick
Japan
Germany
......
And now I blunted with these arrays of cycles and Explode, tell me how to change this code to cut cities?
I tried this on the output, but $out is already an array, and it doesn't work.

$res = explode("|", $out);
trim($res[0]);

Here is the code
$rows = array();
$arr = json_decode($tv, true);
foreach($arr as $item){
  $rows[] = $item[$field];
}
$rows = array_unique($rows);

foreach($rows as $items){
    $out .= $modx->getChunk($tpl, array($field=>$items));
}
return $out;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Robur, 2019-03-11
@flakelf

Here it is described: https://docs.mongodb.com/manual/core/data-model-design/
It will suit you more precisely: https://docs.mongodb.com/manual/tutorial/model-ref...
and https://mongoosejs.com/docs/populate.html
in short - you need to link the post to the author, make the comments a separate document, and link to the post in it, then all this can be collected, for example, via populate in mongoose, to load into an array of posts for the user and an array of comments for the post.

N
Nikita Panuhin, 2020-01-04
@Cheizer

1) It's not clear what $field
is 2) Here is the code that does what you said:

$rows = array();
$arr = json_decode($tv, true);
foreach ($arr as $item){
  array_push($rows, $item[$field]);
}
$rows = array_unique($rows);

$out = array();
foreach ($rows as $item) {
  array_push($out, trim(explode("|", $item)[0]));
}

print_r($out);

Result:

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question