亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 PHP 搜索非對稱多維數組

使用 PHP 搜索非對稱多維數組

PHP
慕斯王 2023-09-15 10:31:16
為了使用 PHP 搜索數組,我發現了很多問題和一些很好的答案。但每次,腳本對問題的回答都過于完美,而不是全局的或所有數組都是對稱的。我的數組看起來像這樣:$data = [    'steve' => [        'id'     => [            '#text' => 1,        ],        'pseudo' => [            '#text' => 'LOL'        ],    ],    'albert' => [        'id'     => [            '#text' => 2,        ],        'pseudo' => [            '#text' => 'KILLER'        ],    ],    'john' => [        'id'     => [            '#text' => 3,        ],        'pseudo' => [            '#text' => 'NOOBS'        ],    ],];這意味著我的數組可以看起來漂亮地對稱生成,或者完全混亂并具有隨機子數組。我的目標是內部搜索并找到 AN ID 的偽值。不幸的是,我無法更改給我這個結果的網絡服務。我曾嘗試使用 array_column 或 array_search,但我成功返回的唯一結果是它是否找到了某些內容。但無法識別。而且我的腳本非常慢。就像是 :search($array, 2); //which return KILLER或者也許更有選擇?我的數組可以有很多 ID(100+)。所以我試圖找到一些優化的東西。:/
查看完整描述

2 回答

?
BIG陽

TA貢獻1859條經驗 獲得超6個贊

可能有一百萬種方法可以做到這一點,但最終您將需要一些能夠過濾值和該值的路徑的遞歸方法。為此,以下是這百萬種方法中的一種。

它使用預定義的迭代器:

  • 回調過濾器迭代器

  • 遞歸迭代器迭代器

  • 遞歸數組迭代器

與自定義PathAsKeyDecorator迭代器結合使用。

演示在這里

<?php

declare(strict_types=1);


final class PathAsKeyDecorator implements \Iterator

{

? ? private RecursiveIteratorIterator $inner;


? ? public function __construct(RecursiveIteratorIterator $inner)

? ? {

? ? ? ? $this->inner = $inner;

? ? }


? ? public function current()

? ? {

? ? ? ? return $this->inner->current();

? ? }


? ? public function next(): void

? ? {

? ? ? ? $this->inner->next();

? ? }


? ? public function key()

? ? {

? ? ? ? $path = [];

? ? ? ? for ($i = 0, $depth = $this->inner->getDepth(); $i <= $depth; $i++) {

? ? ? ? ? ? $path[] = $this->inner->getSubIterator($i)->key();

? ? ? ? }


? ? ? ? return $path;

? ? }


? ? public function valid(): bool

? ? {

? ? ? ? return $this->inner->valid();

? ? }


? ? public function rewind(): void

? ? {

? ? ? ? $this->inner->rewind();

? ? }

}


$input = [

? ? 'steve'? => [

? ? ? ? 'id' => [

? ? ? ? ? ? '#text' => 1,

? ? ? ? ],

? ? ],

? ? 'albert' => [

? ? ? ? 'id' => [

? ? ? ? ? ? '#text' => 2,

? ? ? ? ],

? ? ],

? ? 'john'? ?=> [

? ? ? ? 'profil' => [

? ? ? ? ? ? 'id' => [

? ? ? ? ? ? ? ? '#text' => 3,

? ? ? ? ? ? ],

? ? ? ? ],

? ? ],

];


// this is the filter function that should be customized given your requirements

// or create a factory function which produces these types of filter functions

$filter = static function ($current, array $path): bool {

? ? // with help from the PathAsKeyDecorator

? ? // we can decide on the path to the current value

? ? return ['id', '#text'] === array_slice($path, -2)

? ? ? ? // and the current value

? ? ? ? && 2 === $current;

};



// configure the iterator

$it = new CallbackFilterIterator(

? ? new PathAsKeyDecorator(new RecursiveIteratorIterator(new RecursiveArrayIterator($input))),

? ? $filter,

);


// traverse the iterator

foreach ($it as $path => $val) {

? ? print_r([

? ? ? ? 'path' => $path,

? ? ? ? 'val'? => $val

? ? ]);

}


查看完整回答
反對 回復 2023-09-15
?
揚帆大魚

TA貢獻1799條經驗 獲得超9個贊

完整編輯陣列結構變化的原因。json_encode 用于將數組更改為字符串。僅當每個數組切片中有一個 id 和一個偽值時,它才起作用。


$data = [

    'steve' => [

        'id'     => [

            '#text' => 1,

        ],

        'pseudo' => [

            '#text' => 'LOL'

        ],

    ],

    'albert' => [

        'id'     => [

            '#text' => 2,

        ],

        'pseudo' => [

            '#text' => 'KILLER'

        ],

    ],

    'john' => [

        'id'     => [

            '#text' => 3,

        ],

        'pseudo' => [

            '#text' => 'NOOBS'

        ],

    ],

];



$data = json_encode($data, JSON_NUMERIC_CHECK);

preg_match_all('~"id":{"#text":([^{]*)}~i', $data, $ids);

preg_match_all('~"pseudo":{"#text":"([^{]*)"}~i', $data, $pseudos);


$lnCounter = 0;

$laResult  = array();

foreach($ids[1] as $lnId) {

    $laResult[$lnId] = $pseudos[1][$lnCounter];

    $lnCounter++;

}


echo $laResult[2];


查看完整回答
反對 回復 2023-09-15
  • 2 回答
  • 0 關注
  • 117 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號