0

This for an open source plugin I'm adding a feature to allow using a template to be able to generate output using Handlebars, instead of using PHP for handlebars i want to use js so I need the JSON output like below. You can see this file on GitHub here.

How to get the array setup correctly?

The problem is JSON is output for the resourcify_sources, it's not actually outputting as an array of objects, it outputs the JSON with the actual index and then the values are array of index

So here's what I need the JSON output to look like:

{
  "resourcify_count": 3,
  "resourcify_count_title": null,
  "resourcify_sources": [
    {
      "source_type": "source",
      "source_title": "Source Title",
      "source_url": "http://what.com"
    },
    {
      "source_type": "resource",
      "source_title": "Source Resource!",
      "source_url": "http://sourceresource.com"
    },
    {
      "source_type": "quote",
      "source_title": "Quote Source",
      "source_url": "http://quotesource.com"
    }
  ]
}

Here's a var_dump of the $sources:

array (size=5)
  'source_type' => 
    array (size=5)
      0 => string 'source' (length=6)
      1 => string 'resource' (length=8)
      3 => string 'source' (length=6)
      4 => string 'resource' (length=8)
      5 => string 'quote' (length=5)
  'source_title' => 
    array (size=5)
      0 => string 'Source Title' (length=12)
      1 => string 'Resource' (length=8)
      3 => string 'SourceTWO' (length=9)
      4 => string 'ResourceTWO' (length=11)
      5 => string 'QuoteTWO' (length=8)
  'source_url' => 
    array (size=5)
      0 => string 'http://what.com' (length=15)
      1 => string 'resource.com' (length=12)
      3 => string 'sourcetwo.com' (length=13)
      4 => string 'resourcetwo.com' (length=15)
      5 => string 'quotetwo.com' (length=12)

And here's the for loop I am using:

for ($i = 0; $i < $total_sources; $i++){
    $source_type = $sources['source_type'][$i];
    $source_title = $sources['source_title'][$i];
    $source_url = $sources['source_url'][$i];

    if ($source_url){
        $source_url = esc_url_raw($source_url);

        if (!$source_title) $source_title = $source_url;

        $source_json['resourcify_sources'][$i]['source_type'] = $source_type;
        $source_json['resourcify_sources'][$i]['source_title'] = $source_title;
        $source_json['resourcify_sources'][$i]['source_url'] = $source_url;
    }
}
1
  • Your output doesn't match what's in the array, little confusing. Commented Apr 3, 2014 at 3:03

2 Answers 2

1

You set $total_sources with total number of the array, so your loop will be like:

Loop   Dependency
0      $sources['source_url'][0]
1      $sources['source_url'][1]
2      $sources['source_url'][2] -> undefined
3      $sources['source_url'][3]
4      $sources['source_url'][4]

so the loop times will be incorrect if there's any gap between the indexes (In your case you could not get $sources['source_url'][5]). It's recommended that you add a if-condition to pass the undefined value. You should obtain the last index number of $sources['source_title'] for $total_sources

em.. what does $source_url = $source_url; used for..?

If you don't want the actual index so just simply sort($source_json['resourcify_sources']) to produce new indexes.

For example:

<?php

end($sources['source_title']);

$total_sources = key($sources['source_title']);

for ($i = 0; $i <= $total_sources; $i++){

    if(!$sources['source_title']) continue;

    $source_type = $sources['source_type'][$i];
    $source_title = $sources['source_title'][$i];
    $source_url = $sources['source_url'][$i];

    if ($source_url){

        $source_url = esc_url_raw($source_url);

        if (!$source_title) $source_title = $source_url;

        $source_json['resourcify_sources'][$i]['source_type'] = $source_type;
        $source_json['resourcify_sources'][$i]['source_title'] = $source_title;
        $source_json['resourcify_sources'][$i]['source_url'] = $source_url;

    }

}

sort($source_json['resourcify_sources']);

?>
Sign up to request clarification or add additional context in comments.

3 Comments

Ah that's exactly what it was, there was a missing index and that's got to be what's causing it. Let me give this a try and i'll report back.
The $source_url = esc_url_raw($source_url) uses internal WP function to escape and format the URL correctly (the $source_url value is coming from user)
Worked perfectly, thank you so much! The problem was if someone has left a blank metabox in the middle of the sources, like you said it does have one index missing, after I modified the code like above it works just as expected, thanks so much!!!
0

Don't know exatcly what you had going wrong, But I did this to get the results you're looking for...

$sources = array (


'source_type' => 
    array (
        'source' ,
        'resource' ,
      'source' ,
        'resource' ,
        'quote' 
      ),
  'source_title' => 
    array (
      'Source Title' ,
       'Resource' ,
       'SourceTWO' ,
       'ResourceTWO' ,
       'QuoteTWO' 
      ),
  'source_url' => 
    array (
      'http://what.com' ,
       'resource.com' ,
       'sourcetwo.com' ,
       'resourcetwo.com' ,
       'quotetwo.com' 
      )
);
$source_json = array();
$source_json['resourcify_count'] =count($sources['source_type']);
$source_json["resourcify_count_title"] = 'null';

for ($i = 0; $i < count($sources['source_type']); $i++){
    $source_type = $sources['source_type'][$i];
    $source_title = $sources['source_title'][$i];
    $source_url = $sources['source_url'][$i];

    if ($source_url){
        $source_url = $source_url;

        if (!$source_title) $source_title = $source_url;

        $source_json['resourcify_sources'][$i]['source_type'] = $source_type;
        $source_json['resourcify_sources'][$i]['source_title'] = $source_title;
        $source_json['resourcify_sources'][$i]['source_url'] = $source_url;
    }
}
var_dump(json_encode($source_json));

Maybe all you needed was the json_encode ?

1 Comment

Thanks for the response, it does output correctly but it seems the problem i was having was with a missing index, but thanks for your input!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.