以下代码返回一个关联数组,如下所示
Array ( [1] => Array (
[url] => example.com
[title] => Title.example
[snippet] => snippet.example
) )
$blekkoArray = array();
foreach ($js->RESULT as $item)
{
$blekkoArray[$i]['url'] = str_replace ($find, '', ($item->{'Url'}) );
$blekkoArray[$i]['title'] = ($item->{'url_title'});
$blekkoArray[$i]['snippet'] = ($item->{'snippet'});
$i++;
}
print_r ($blekkoArray);
如何修改数组,以便1,2,3
等标识数组元素,而不是由网址识别,例如。
Array ( [example.com] => Array (
[title] => Title.example
[snippet] => snippet.example
) )
答案 0 :(得分:1)
答案 1 :(得分:0)
非常简单,只需使用网址而不是$ i
foreach ($js->RESULT as $item)
{
$url = str_replace ($find, '', ($item->{'Url'}) )
$blekkoArray[$url]['title'] = ($item->{'url_title'});
$blekkoArray[$url]['snippet'] = ($item->{'snippet'});
$i++;
}
答案 2 :(得分:0)
foreach ($js->RESULT as $item)
{
$url = str_replace ($find, '', ($item->{'Url'}) );
$blekkoArray[$url] = array("title"=>($item->{'url_title'}), "snippet"=>($item->{'snipped'}));
}
答案 3 :(得分:0)
考虑您的示例如下。其中$ js是您想要修改的数组。
$js = array(
1 => array ( 'url' => 'example.com', 'title' => 'Title.example','snippet' => 'snippet.example'),
2 => array ( 'url' => 'example2.com', 'title' => 'Title.example2','snippet' => 'snippet.example2'),
3 => array ( 'url' => 'example3.com', 'title' => 'Title.example3','snippet' => 'snippet.example3'));
$blekkoArray = array();
// The lines below should do the trick
foreach($js as $rows) { // main loop begins here
foreach($rows as $key => $values) { // access what's inside in every $row by looping it again
if ($key != 'url') {
$blekkoArray[$rows['url']][$key] = $values; // Assign them
}
}
}
print_r ($blekkoArray);
$ js数组中有多少元素并不重要,因为它每次只会重复这个过程。
答案 4 :(得分:0)
foreach ($arr as $key => $value){
$out[$value['url']] = array_slice($value, 1);
}
答案 5 :(得分:0)
其他解决方案似乎专注于在
之后更改数组foreach ($js->RESULT as $item)
{
$blekkoArray[str_replace ($find, '', ($item->{'Url'}))] = array(
'title'=> $item->{'url_title'},
'snip pet' => $item->{'snippet'}
);
}
这应该使阵列成为你需要它的方式