StevenTompkins Posted November 13, 2014 Share Posted November 13, 2014 Hi wondering if there's a simple way to merge or update the data from one array to another . I've tried merge data but it just adds additional records rather than merging them off one common key. The array is just simply adding a name to another array the format of both is as follows Array 1 [0] => stdClass Object([name] =>[market] => Football[selection] => 7051575) ) Array 2 Array([0] => stdClass Object([name] => Something[market] =>[selection] => 7051575) ) There are other fields but I'm just trying to merge the name from Array two into Array one using the selection as a common key. Is there an easy command like merge or do I have to loop thru the arrays to add the data. Thanks Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted November 13, 2014 Share Posted November 13, 2014 As you have not posted any code I can only direct you to the documentation for array_merge. Quote Link to comment Share on other sites More sharing options...
StevenTompkins Posted November 13, 2014 Author Share Posted November 13, 2014 (edited) I mentioned I've tried array_merge but it just adds the data as additional records into one new array. What code am I supposed to post as I'm looking for the code? Here you go then array_merge($array1,$array2) gives me Array([0] => stdClass Object ([name] => Something[market] =>[selection] => 7051575) [1] => stdClass Object ([name] =>[market] => Football[selection] => 7051575 ) rather than the array I'm hoping for Array([0] => stdClass Object ([name] => Something[market] => Football[selection] => 7051575) ) Edited November 13, 2014 by StevenTompkins Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted November 13, 2014 Share Posted November 13, 2014 What code am I supposed to post as I'm looking for the code? Shouldn't you know that? Its your code is int it? I mentioned I've tried array_merge You did not mentioned you tried using that function. The only other function you could try is array_merge_recursive Quote Link to comment Share on other sites More sharing options...
Barand Posted November 13, 2014 Share Posted November 13, 2014 Do those two arrays come from two separate database queries? If so, you could/should use a joined query to get the array you want in the first place. Quote Link to comment Share on other sites More sharing options...
StevenTompkins Posted November 13, 2014 Author Share Posted November 13, 2014 They're from scraping a couple of websites and putting the data into comparable arrays , I guess php just doesn't have a simple function to merge so I'll just have to loop within a foreach loop and update one of the arrays. Thought there might have been a simple way to do it in one command Quote Link to comment Share on other sites More sharing options...
tryingtolearn Posted November 13, 2014 Share Posted November 13, 2014 If it's coming from "scrapping" different sites couldn't you just add it to the array you want instead of adding it to its own separate array and them trying to merge? Just a thought might or might not work since I don't know the process that gets it to the point you are trying to merge.. Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted November 13, 2014 Solution Share Posted November 13, 2014 Build the first array with the section code as the key, as that is the common element, then add to it instead of building the second array Quote Link to comment Share on other sites More sharing options...
StevenTompkins Posted November 14, 2014 Author Share Posted November 14, 2014 Thanks, Barand I'll probably look into that method, the first array only needs to be scraped once to get the static data. I don't use php too often but find it very useful and quick for web scraping only later you start to realise it's limitations I went with looping the array for now but if I get round to recoding I'll see if your suggestion works out better. foreach((array)$array1 as $index ){ foreach((array)$array2 as $value) {if($index->selection==$value->selection){ $value->name=$index->name;break;}}} Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.