Jump to content

Replace array value


sandy1028

Recommended Posts

The output of the array1 is looks as below.

 

Array ( [num_found] => 60 [articles] => 
Array ( [0] => 
Array ( [aut] => Array ( [0] => ANI ) [by] => ANI [id] => 278 [key] => Array ( [0] => trans fats [1] => junk food [2] => food labels [3] => miscarriage [4] => hydrogenated ) [norm] => true [body] => Gorging  [cat] => Array ( [0] => food & drink )
[pm] => ANI|P#M#D| [pm] => Beware! [seo] => junk [pmT] => Array ( [0] => trans fats [1] => junk food [2] => food labels [3] => miscarriage [4] => hydrogenated ) [pid] => 72 [Date] => 2011-06-02T00:00:00Z [section] => Health [sd] => 3506926 [ti] => Beware! Junk food can cause miscarriage [ur] => riage [kick] => [subtitle] => [image] => [Tag] => ) 

[1] => Array ( [aut] => Array ( [0] => URVASHI ASHER ) [by] => URVASHI [id] => 277 [key] =>
Array ( [0] => film [1] => design [2] => biography [3] => amitabh bachchan [4] => cinema ) [norm] => true  [body] => division. [cat] => Array ( [0] => entertainment ) [pm] => URVASHI ASHER|P#M#D|TNN [pm] => booked [seo] => film [pmT] => Array ( [0] => film [1] => design [2] => biography [3] => amitabh bachchan [4] => cinema ) [pid] => 72 [Date] => 2011-06-02T00:00:00Z [section] => News & Interviews [sd] => 3921796 [ti] => Amitabh booked [uri] => film [kick] => [subtitle] => [image] => [Tag] => )

 

I have a array2 similar to this. 

Array ( [num_found] => 70 [articles] => 
Array ( [0] => 
Array ( [aut] => Array ( [0] => ANI ) [by] => RNI [id] => 643 [key] => Array ( [0] => trans fats [1] => junk food [2] => food labels [3] => miscarriage [4] => hy ) [norm] => true [body] => Test [cat] => Array ( [0] => drink )
[pm] => ANI|P#M#D| [pm] => Beware! [seo] => junk [pmT] => Array ( [0] => trans fats [1] => junk food [2] => food labels [3] => miscarriage [4] => hydrogenated ) [pid] => 73 [Date] => 2011-06-02T00:00:00Z [section] => Health [sd] => 8434 [ti] => Beware [ur] => riage [kick] => [subtitle] => [image] => [Tag] => ) 

[1] => Array ( [aut] => Array ( [0] => URVASHI ASHER ) [by] => URVASHI [id] => 345 [key] =>
Array ( [0] => film [1] => design [2] => biography [3] => bachchan [4] => cinema ) [norm] => true  [body] => division. [cat] => Array ( [0] => entertainment ) [pm] => URVASHI|P#M#D|TNN [pm] => booked [seo] => film [pmT] => Array ( [0] => film [1] => design [2] => biography [3] => amitabh bachchan [4] => cinema ) [pid] => 72 [Date] => 2011-06-02T00:00:00Z [section] => News & Interviews [sd] => 43543543 [ti] => booked [uri] => film [kick] => [subtitle] => [image] => [Tag] => )


 

Please tell me how to add the 1st value from array2 and replace in array1.

 

The output should look like

 

 

 

Array ( [num_found] => 60 [articles] => 
Array ( [0] => 
Array ( [aut] => Array ( [0] => ANI ) [by] => RNI [id] => 643 [key] => Array ( [0] => trans fats [1] => junk food [2] => food labels [3] => miscarriage [4] => hy ) [norm] => true [body] => Test [cat] => Array ( [0] => drink )
[pm] => ANI|P#M#D| [pm] => Beware! [seo] => junk [pmT] => Array ( [0] => trans fats [1] => junk food [2] => food labels [3] => miscarriage [4] => hydrogenated ) [pid] => 73 [Date] => 2011-06-02T00:00:00Z [section] => Health [sd] => 8434 [ti] => Beware [ur] => riage [kick] => [subtitle] => [image] => [Tag] => ) 

[1] => Array ( [aut] => Array ( [0] => URVASHI ASHER ) [by] => URVASHI [id] => 277 [key] =>
Array ( [0] => film [1] => design [2] => biography [3] => amitabh bachchan [4] => cinema ) [norm] => true  [body] => division. [cat] => Array ( [0] => entertainment ) [pm] => URVASHI ASHER|P#M#D|TNN [pm] => booked [seo] => film [pmT] => Array ( [0] => film [1] => design [2] => biography [3] => amitabh bachchan [4] => cinema ) [pid] => 72 [Date] => 2011-06-02T00:00:00Z [section] => News & Interviews [sd] => 3921796 [ti] => Amitabh booked [uri] => film [kick] => [subtitle] => [image] => [Tag] => )

 

where array[0] is the value from array2.

Link to comment
Share on other sites

Thank you. BUt this solution doesn't work :(

 

where array[0] is the value from array2.

 

Because you didn't specify the above correctly. The "method" I provided will work. YOU should have been able to modify it for your particular needs. Looking at the structure a little closer I think this is what you need. If not, YOU need to take some responsibility to understand the structure and modify it accordingly.

 

$array1['articles'][0] = $array2['articles'][0];

Link to comment
Share on other sites

Thanks.

$array1['articles'][0] = $array2['articles'][0];

 

Please tell me how to replace the value of "$array2['articles'][0];" in any of  top 6 values in $array1 randomly.

 

What do you mean by "in any of  top 6 values in $array1"? Do you mean: $array1['articles'][0], $array1['articles'][1] . . . $array1['articles'][5]?

 

If so, then

//Create a random index 0-5 (or max of those avail in array1)
$randIdx = rand(0, max(count($array1['articles']), 5));
$array1['articles'][$randIdx] = $array2['articles'][0];

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.