Skip to content

Instantly share code, notes, and snippets.

@vikramIde
Created October 2, 2018 14:17
Show Gist options
  • Save vikramIde/8bb860066ae219b6f6b98ed54c27681e to your computer and use it in GitHub Desktop.
Save vikramIde/8bb860066ae219b6f6b98ed54c27681e to your computer and use it in GitHub Desktop.
$innerdata = json_decode('{
"sync_block": false,
"nest_uid":"1_1_1536657342_lyBhkbkLYT"
"contacts": [{
"con_title": "",
"con_fName": "",
"con_lName": "",
"con_job_title": "",
"emails": [{
"email": "",
"type": "",
"primary": false,
"nest_uid": "1_1_1536657342_lyBhkbkDfG",
"checked": false
},
{
"email": "",
"type": "",
"primary": false,
"nest_uid": "1_1_1536657342_lyBhkbkDfGLp",
"checked": false
}],
"phones": [{
"phone": "",
"type": "",
"primary": false,
"nest_uid": "1_1_1536657342_CQPBBUBRZN",
"checked": false
}],
"nest_uid": "1_1_1536657342_fpTI2RF3XK",
"checked": false
}]
}');
class Searcher {
private $data;
private $uid;
private $result;
public function __construct($data, $uid)
{
$this->data = $data;
$this->uid = $uid;
}
private function search($data, $path)
{
foreach ($data as $k => $v) {
if ($k === 'nest_uid' && $v === $this->uid) {
$this->result[join('/', $path)] = $data;
}
if (is_array($v) || is_object($v)) {
$subPath = $path;
$subPath[] = $k;
$this->search($v, $subPath);
}
}
}
public function getResult() {
if($this->result === null) {
$this->result = [];
$this->search($this->data, []);
}
return $this->result;
}
}
$searcher = new Searcher($innerdata, '1_1_1536657342_lyBhkbkLYT');
print_r($searcher->getResult());
@wxiaoguang
Copy link

Where is the comma of the line of first nest_uid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment