在JSON数组中搜索值并访问周围的键/值;输出为JSON [英] Searching JSON array for values and accessing surrounding keys/values; output as JSON

查看:90
本文介绍了在JSON数组中搜索值并访问周围的键/值;输出为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难做一些我认为应该很简单的事情.我已经阅读了许多有关访问PHP JSON数组的示例,但似乎找不到适合我的用例的示例(或者我不了解这些示例的适用性).

I am having the hardest time trying to do something that I believe should be simple. I've read many examples regarding accessing PHP JSON arrays but I can't seem to find one to fit my use case (or I don't understand how the pieces fit).

这是我在PHP中的JSON(example_data.php):

Here is my JSON in PHP (example_data.php):

<?php
$contents = array(
    1 => array(
        'contentid' => '1',
        'full' => 'Song Name by Artist Name (maininfo)',
        'aname' => 'Artist Name',
        'sname' => 'Song Name',
        'main' => 'core content #1',
        'maininfo' => 'url'
    ),
    2 => array(
        'contentid' => '2',
        'full' => 'Song Name by Artist Name (maininfo)',
        'aname' => 'Artist Name',
        'sname' => 'Song Name',
        'main' => 'core content #2',
        'maininfo' => 'url')
    );
?>

以下代码将搜索变量(q)作为输入,并将其传递以搜索JSON以查找部分或完全匹配项.

The code below takes as input a search variable (q) and passes it along to search the JSON for partial or complete matches.

<?php
$q = $_GET['q'];

include('example_data.php');

$results = array('contents' => array());

foreach ($contents as $name => $data)
{
    if (stripos($name, $q) !== false)
    {
        $results['contents'][$name] = $data;
    }
}

$final_contents = array('header' => array(), 'data' => array());
$final_contents['header'] = array('title' => 'Music', 'num' =>  count($results['contents']), 'limit' => 6);


foreach ($results['contents'] as $name => $data)
{
    $final_contents['data'][] = array('primary' => $data['sname'], 'secondary' => $data['aname']);
}

/* Output JSON */
$final = array($final_contents);
header('Content-type: application/json');
echo json_encode($final);
die();
?>

它可以正常工作,但只会针对数组名称(在此示例中为'1'或'2')进行搜索.显然,用户不会搜索"1",而是搜索艺术家姓名"或歌曲名称"或核心内容.

It works fine but it will only search against the array name ('1' or '2' in this example) for a match. Obviously users will not search for '1'--instead they will search for 'Artist Name' or 'Song Name' or the core content.

有人会告诉我如何修改此代码以使用'q'并专门针对'full'键进行搜索-如果'的值中存在部分/完全匹配,完整",我想将其返回为JSON(包括aname,sname,main,maininfo等).预期会有多种结果. JSON现在已按预期返回,但用户不会搜索"1","2"等,因此它实际上不是可用的.

Would someone be so kind to tell me how I can modify this code to take 'q' and use it as a search against the 'full' key specifically -- if there is a partial/complete match in the value of 'full', I want to return it as JSON (including the aname, sname, main, maininfo, etc.). Multiple results are to be expected. JSON is coming back now as expected but the user won't search for '1', '2', etc. so it is not really usable.

真的很感谢您的时间!

推荐答案

您需要确定是否存在匹配项,并在外循环中返回数组

You need to determine if there is a match and return the array in your outer loop

$contents = array(
    1 => array(
        'contentid' => '1',
        'full' => 'Song Name by Artist Name (maininfo)',
        'aname' => 'Artist Name',
        'sname' => 'Song Name',
        'main' => 'core content #1',
        'maininfo' => 'url'
    ),
    2 => array(
        'contentid' => '2',
        'full' => 'Song Name by Artist Name (maininfo)',
        'aname' => 'Artist Name',
        'sname' => 'Song Name',
        'main' => 'core content #2',
        'maininfo' => 'url')
    );


$a = 'song'; // value that will be searched for in all fields
$percent = NULL;
$results = array();
$match = FALSE;
echo "starting search...<br>";
foreach($contents as $name) {

	foreach($name as $key =>$val){
	echo "starting full field match...<br>";
	if (isset($q) && similar_text($val,$a ,$percent )) {
	   echo $name."[".$key."] = ".$val." vs ".$a." Percentage Match: ".$percent." % <br>";
	  ($percent >40 ? $match = TRUE : $match = FALSE);
      if($match){
	  echo "MATCHED % greater 40% match...<br>";
	      break;
	  }
	  echo " end full field match<hr>";
    }
	
	
    if (isset($a) && similar_text($val,$a ,$percent )) {
	echo "starting name match...<br>";
       echo $name."[".$key."] = ".$val." vs ".$a." Percentage Match: ".$percent." % <br>";
	  ($percent >40 ? $match = TRUE : $match = FALSE);
      if($match){
	  echo "MATCHED % greater 40% match...<br>";
	      break;
	  }
    }
	echo " end name  match<hr>";
	}
    if ($match) {
	echo "Match added to array<br>";
      //push full array
      array_push($results, $name);
    }
   
  }
  echo "search completed<hr>";
  /* Output JSON */
header('Content-type: application/json');
echo json_encode($results[0]);
die(); 

这篇关于在JSON数组中搜索值并访问周围的键/值;输出为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆