2013-07-20 50 views
1

下面的代码如下我怎样才能改变命名的键关联数组

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

其他的解决方案似乎把重点放在改变阵列后

foreach ($js->RESULT as $item) 
    { 
     $blekkoArray[str_replace ($find, '', ($item->{'Url'}))] = array(   
     'title'=> $item->{'url_title'}, 
     'snip pet' => $item->{'snippet'} 
     ); 

    } 

这应该使数组你怎么需要它

+0

没错这个完美工作,感谢 –

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++; 
} 
0
foreach ($js->RESULT as $item) 
    { 
     $url = str_replace ($find, '', ($item->{'Url'})); 
     $blekkoArray[$url] = array("title"=>($item->{'url_title'}), "snippet"=>($item->{'snipped'}));  
    } 
1

你可以试试这个太(一个在线解决方案)

$newArray = array($old[0]['url'] => array_splice($old[0], 1)); 

DEMO.

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数组中有多少元素,因为它只会每次重复该过程。

0
foreach ($arr as $key => $value){ 
    $out[$value['url']] = array_slice($value, 1); 
}