在JSON数组中搜索值并访问周围的键/值;输出为JSON

时间:2015-03-22 22:28:38

标签: php json

我正在努力做一些我认为应该简单的事情。我已经阅读了很多关于访问PHP JSON数组的例子,但我似乎找不到适合我的用例(或者我不明白这些部分是如何适合的)。

这是我在PHP中的JSON(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以进行部分或完全匹配。

<?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” - 而是搜索“艺术家姓名”或“歌曲名称”或核心内容。

有人会如此友善地告诉我如何修改此代码以获取'q'并将其用作特别针对'完整'键的搜索 - 如果'的值存在部分/完全匹配' '我想把它作为JSON(包括aname,sname,main,maininfo等)返回。预计会有多重结果。 JSON现在按预期返回,但用户不会搜索“1”,“2”等,因此它实际上并不可用。

非常感谢您提前的时间!

1 个答案:

答案 0 :(得分:0)

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

$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();