在PHP中的单个对象数组中获取多个JSON对象数组

时间:2018-05-05 09:30:13

标签: javascript php jquery json codeigniter

我在json创建了php数组结果数组我得到了以下结果:

[{
    "05-10-2018": "Seeing dads differently"
}, {
    "05-10-2018": "Extraordinary ordinary Britain"
}, {
    "05-10-2018": " Roll up for the Social Science Market!"
}, {
    "05-10-2018": "Why do we use it?"
}, {
    "05-10-2018": "Extraordinary ordinary Britain"
}]

但我想要以下格式的结果

{
    '12-14-2018': '<a href="http://google.com" target=_blank>Amet Temporibus ad quod enim dolor doloribus sequi!</a>',
    '09-30-2018': '<a href="http://google.com" target=_blank>Cupiditate blanditiis autem at obcaecati libero laborum.</a>',
    '09-22-2018': '<a href="http://google.com" target=_blank>Quo accusamus itaque esse aliquid error reprehenderit!</a>',
    '09-16-2018': '<a href="http://google.com" target=_blank>Quia magni aperiam nam asperiores animi enim?</a>',
    '08-21-2018': '<a href="http://google.com" target=_blank>Quia quibusdam nemo nobis rerum. Dolorem, ipsa?</a>',
    '08-09-2018': '<a href="http://google.com" target=_blank>At minima unde cum alias maiores corrupti quas.</a>',
    '07-23-2018': '<a href="http://google.com" target=_blank>Blanditiis maiores odio cumque eligendi facilis iure.</a>',
    '07-16-2018': '<a href="http://google.com" target=_blank>Dolorum, iusto quisquam distinctio dolore quo aperiam reiciendis.</a>',
    '06-28-2018': '<a href="http://google.com" target=_blank>Aperiam odio voluptatibus quae sunt unde itaque.</a>',
    '06-15-2018': '<a href="http://google.com" target=_blank>Tempore asperiores et possimus inventore vero ab.</a>',
    '06-02-2018': '<a href="http://google.com" target=_blank>Possimus asperiores perferendis recusandae debitis omnis consectetur aut!</a>',
    '05-30-2018': '<a href="http://google.com" target=_blank>Minus aliquid maxime atque praesentium rerum dolores sint?</a>',
    '05-18-2018': '<a href="http://google.com" target=_blank>Corporis accusantium assumenda facilis fugiat ut nostrum.</a>',
    '05-09-2018': '<a href="http://google.com" target=_blank>Sequi ad sint sunt quasi veniam cum.</a>',
};

我是怎么做到的?

1 个答案:

答案 0 :(得分:0)

试试这个

$json = '[{"05-10-2018":"Seeing dads differently"},{"05-10-2018":"Extraordinary ordinary Britain"},{"05-10-2018":" Roll up for the Social Science Market!"},{"05-10-2018":"Why do we use it?"},{"05-10-2018":"Extraordinary ordinary Britain"}]';

$json_decoded = json_decode($json, true);
$results = array();
array_walk($json_decoded, function($v) use (&$results){
    $key = array_keys($v);
    $value = array_values($v);
    $results[] = "'{$key[0]}' : '<a href='http:www.google.com/' target='_blank'>{$value[0]}</a>'";
});

输出 $ results

Array
(
    [0] => '05-10-2018' : '<a href='http:www.google.com/' target='_blank'>Seeing dads differently</a>'
    [1] => '05-10-2018' : '<a href='http:www.google.com/' target='_blank'>Extraordinary ordinary Britain</a>'
    [2] => '05-10-2018' : '<a href='http:www.google.com/' target='_blank'> Roll up for the Social Science Market!</a>'
    [3] => '05-10-2018' : '<a href='http:www.google.com/' target='_blank'>Why do we use it?</a>'
    [4] => '05-10-2018' : '<a href='http:www.google.com/' target='_blank'>Extraordinary ordinary Britain</a>'
)

输出 $ result_code

$result_code = '{'.implode(',', $results).'}';

{
'05-10-2018' : '<a href='http:www.google.com/' target='_blank'>Seeing dads differently</a>',
'05-10-2018' : '<a href='http:www.google.com/' target='_blank'>Extraordinary ordinary Britain</a>',
'05-10-2018' : '<a href='http:www.google.com/' target='_blank'> Roll up for the Social Science Market!</a>',
'05-10-2018' : '<a href='http:www.google.com/' target='_blank'>Why do we use it?</a>',
'05-10-2018' : '<a href='http:www.google.com/' target='_blank'>Extraordinary ordinary Britain</a>'
}