1

I have 3 JSON strings coming in with POST and want to merge these into a 2 dimensional array and save in JSON format to the database. For this example I have image URL's, alt descriptions and booleans isfavorite

$url_arr = json_decode('["http://site.com/001.jpg","http://site.com/003.jpg","http://site.com/002.jpg"]');
$alt_arr = json_decode('["testing internat chars àèéìòóù stop","second description",""]'); // UTF-8 supported
$isFav_arr = json_decode('["true", "false", "false"]'); // strings need to be converted to booleans

// merge into 2 dimensional array
// $img_arr = array_merge($url_arr, $alt_arr, $isFav_arr); // doesn't work, just add's to the end
// ...

// save 2D JSON in database
$to_db = json_encode($img_arr);
FFish
  • 10,964
  • 34
  • 95
  • 136
  • you want the three arrays to be in an array, right? and you want to save the JSON of that in the database, right? – Gordon Nov 02 '10 at 14:08

3 Answers3

1

Just string concatenate:

$to_db = '[' 
       . '["http://site.com/001.jpg","http://site.com/003.jpg","http://site.com/002.jpg"]'
       . ',["testing int chars àèéìòóù stop","second description",""]'
       . ',["true", "false", "false"]'
       . ']';

Unless you want to work with the values in the Json string, you dont need any en/decoding. You can use http://www.jsonlint.com/ to validate it (removed jsonlint and print_r dumps to make some space)

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • mmm, yes I think so.. is this valid JSON? I mean I can json_decode() I need to be able to access for example 'alt for second image' ect. – FFish Nov 02 '10 at 14:15
  • I just tried echo print_r(json_decode($to_db)); but it returns 1?? Would it not be better something like [["001.jpg","descr 1",false],["002.jpg","descr 2",true]] – FFish Nov 02 '10 at 14:22
  • @FFish I am not sure I understand what you are asking. It is valid (after I added the missing commas) and if you want to shove the Json string to the DB then this should be the prefered way because decoding it is expensive. – Gordon Nov 02 '10 at 14:24
  • 1
    OK, got it now (just copied your snipped, without the comma correction) That's great! thanks so much :-) – FFish Nov 02 '10 at 14:28
1

$url_arr = json_decode('["http://site.com/001.jpg","http://site.com/003.jpg","http://site.com/002.jpg"]'); $alt_arr = json_decode('["testing int chars àèéìòóù stop","second description",""]'); // UTF-8 supported $isFav_arr = json_decode('["true", "false", "false"]'); // strings need to be converted to booleans

$img_arr = array( "urls"=>$url_arr, "alts"=>$alts_arr, "favs"=>$isFav_arr
); $results = json_encode($img_arr);

// possibly you will need to clean up numeric indexes ... before building multydim array

Conex
  • 832
  • 8
  • 17
  • this is also a good answer, but Gordon's solution saves json encoding as in my example I have them already encoded. cheers – FFish Nov 02 '10 at 17:05
0

The easy bit:

$img_arr = array();

for ($i=0; $i < sizeof($url_arr); $i++) {
    $img_arr[] = array($url_arr[$i], $alt_arr[$i], $isFav_arr[$i]);
}

Saving to the DB depends which DB type you're using. Look at examples using either mysql_query, or prepared statements (preferred).

Matt
  • 74,352
  • 26
  • 153
  • 180