ben_eire
ben_eire

Reputation: 35

How can I change the named keys in an associative array

The following code returns an associative array as follows

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);

How can I modify the array so that instead of the array element been identified by 1,2,3 etc it would be identified by the url eg.

Array ( [example.com] => Array ( 
   [title] => Title.example
   [snippet] => snippet.example 
) )

Upvotes: 1

Views: 88

Answers (6)

exussum
exussum

Reputation: 18550

The other solutions seem to focus on changing the array after

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

    }

That should make the array how you need it

Upvotes: 0

Expedito
Expedito

Reputation: 7795

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

Upvotes: 0

Considering your example as follows. Where $js is the array you would want to modify.

$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);

It doesn't matter how many elements there are in your $js array as it will only repeat the process each time.

Upvotes: 0

The Alpha
The Alpha

Reputation: 146191

You may try this too (one line solution)

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

DEMO.

Upvotes: 1

Nikola R.
Nikola R.

Reputation: 1173

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

Upvotes: 0

zoran404
zoran404

Reputation: 2946

Very simple, just use the url instead of $i

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

Upvotes: 0

Related Questions