How to deal with array of multidimensional arrays

70 views Asked by At

I have the below array containing arrays and arrays. This is just one entry; imagine now that I have 20 sources and each source contains at least 10 entries like the one below. This is a nightmare I want to solve.

Can anyone advice on how I can parse this efficiently in PHP and be able to get each of the values to be able to insert them into a DB for further processing!! Thank you.

Array ( 
    [id] => id 
    [direction] => ltr 
    [updated] => 1407220315278 
    [title] => Syria | The Guardian 
    [alternate] => Array ( [0] => Array ( 
                                [href] => href 
                                [type] => text/html ) ) 
    [continuation] => 147a4ddf48e:28d98:bda086f 
    [items] => Array ( [0] => Array ( 
                                [keywords] => Array ( 
                                                [0] => Global development 
                                                [1] => Refugees 
                                                [2] => Syria 
                                                [3] => Middle East and North Africa 
                                                [4] => World news 
                                                [5] => UK news 
                                                [6] => Scotland 
                                                [7] => Conflict and development 
                                                [8] => Immigration and asylum ) 
                                [fingerprint] => 47075b4f 
                                [originId] => originId 
                                [id] => Te6w3H2VbpKKFda+uYdqytnutphL/kktv0RL7gq9jjU=_147a4ddf48e:28d98:bda086f 
                                [title] => A Syrian refugee in Scotland: 'I'm one of the lucky ones' video 
                                [published] => 1407218400000 
                                [crawled] => 1407220315278 
                                [alternate] => Array ( 
                                                [0] => Array ( 
                                                        [href] => href
                                                        [type] => text/html ) ) 
                                [author] => Produced by Claudine Spera and Ellie Violet Bramley 
                                [origin] => Array ( 
                                                [streamId] => streamId 
                                                [title] => Syria | The Guardian 
                                                [htmlUrl] => htmlUrl ) 
                                                [summary] => Array ( 
                                                                [direction] => ltr 
                                                                [content] => Ayman is one of about 3 million Syrian refugees living outside his homeland. After nine of his friends were killed in Damascus, Ayman used his student visa to flee to the UK, leaving his wife and twin boys behind. 'We didn't expect civil war in Syria, never, ever,' he says. But now, with the help of the Red Cross, the family are reunited and building a life in Edinburgh Continue reading... ) 
                                                [visual] => Array ( 
                                                                [url] => imagUrl
                                                                [height] => 252 
                                                                [width] => 600 
                                                                [contentType] => image/jpeg ) 
                                                [unread] => 1 
                                                [categories] => Array ( 
                                                                    [0] => Array ( 
                                                                                [id] => user/5312f2fe-1adc-476f-93da-8b1db5a63180/category/عربي 
                                                                                [label] => عربي ) ) 
                                                [engagement] => 4 [engagementRate] => 0.33 ) ) )
1

There are 1 answers

1
singin_whale On

If there are always the same keys in the array it should be relatively easy to iterate with something like this:

foreach($yourarray as $key1=>$item1){
  //dosomething
  if($key1=="alternate"){
     foreach($item1 as $key2=>$item2){
        //and so on
     }
  }

}

As to saving it in a database I admit its not easy and I am not that experienced with databases but if I were you I'd save them in multiple tables with "1 to n" references.