Merging arrays in php before json output

Merging arrays in php before json output

east1999east1999 Posts: 31Questions: 11Answers: 0

I'm moving to server-side processing. Usually I'd merge JSON fields with javascript after the file was loaded. I can still do that in SSP, but it breaks up search, since search works in the original fields, and not the actual data in the columns.

To give you an example, I have a column for authors. When there is no author, the output is an editor. When searching, I want to search for people (author/editor), but SSP will only search for one of the two. My solution would be an array merged before the json output, printed to a hidden column, which would be searched.

Can it be done? This is my php file, copied from your SSP example.

$columns = array(
array( 'db' => 'Author', 'dt' => 'authors'),
array( 'db' => 'Editor', 'dt' => 'editor'),
array( 'db' => 'Item Type', 'dt' => 'type'),
array( 'db' => 'Publication Year', 'dt' => 'year' ),
array( 'db' => 'Title', 'dt' => 'title' ),
array( 'db' => 'Publication Title', 'dt' => 'pubtitle' ),
array( 'db' => 'Publisher', 'dt' => 'publisher' ),
array( 'db' => 'Url', 'dt' => 'url'),
array( 'db' => 'DOI', 'dt' => 'doi' ),
array( 'db' => 'Title', 'dt' => 'title' ),
array( 'db' => 'Reviewed Author', 'dt' => 'reviewed' ),
array( 'db' => 'Abstract Note', 'dt' => 'abstract' ),
array( 'db' => 'ISBN', 'dt' => 'isbn' ),
array( 'db' => 'Tags', 'dt' => 'tags'),
array( 'db' => 'Place', 'dt' => 'place' ),
array( 'db' => 'Type', 'dt' => 'genre' ),
array( 'db' => 'Publisher', 'dt' => 'publisher' ),
array( 'db' => 'Pages', 'dt' => 'pages' ),
array( 'db' => 'Volume', 'dt' => 'volume' ),
array( 'db' => 'Issue', 'dt' => 'issue')
);

require( 'ssp.class.php' );

echo json_encode(
SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns)
);

Answers

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    Hi @east1999 ,

    This sounds like the root cause of your other thread. As you said, the search by default will only match the field it's being request to search. If you're changing those fields in the data you send back, you'll need to ensure that functionality is also included in the search too.

    Cheers,

    Colin

  • east1999east1999 Posts: 31Questions: 11Answers: 0

    Thank you again @colin. I've clarified the problems I'm facing in the other thread.

    I was looking into array_merge for this but I can't call on the nested arrays because I'm not sure how. It would be something like

    $arr = array_merge($columns["authors"],$columns["editor"]);

    Then I would add it to json_encode.

This discussion has been closed.