2

i have an array like this

array 1

array(3) { 
   [0]=> string(2) "47" 
   [1]=> string(2) "48" 
   [2]=> string(2) "49" 
} 

i have plan to giving array with name, the array name is number

array 2

array(3) { 
[0]=> object(stdClass)#18 (2) { 
      ["address"]=> string(9) "Address 1" 
      ["price"]=> string(16) "120000" } 
[1]=> object(stdClass)#21 (2) { 
      ["address"]=> string(9) "Address 2"      
      ["price"]=> string(16) "150000" } 
[2]=> object(stdClass)#20 (2) { 
      ["address"]=> string(9) "Address 3" 
      ["price"]=> string(16) "180000" } 
}

I want to inserting array 1 into array 2 which same array key

I want to insert array 1 data into array 2 accordance with the key array . so i I was expecting to be joined both of arrays and became joined array like this

array(3) { 
[0]=> object(stdClass)#18 (2) { 
      ["address"]=> string(9) "Address 1" 
      ["price"]=> string(16) "120000" 
      ["number"]=> string(2) "47" } 
[1]=> object(stdClass)#21 (2) { 
      ["address"]=> string(9) "Address 2"      
      ["price"]=> string(16) "150000" 
      ["number"]=> string(2) "48"} 
[2]=> object(stdClass)#20 (2) { 
      ["address"]=> string(9) "Address 3" 
      ["price"]=> string(16) "180000" 
      ["number"]=> string(2) "49"} 
} 

is there any way to create or manipulate into an array like that ? my array is dynamically so number of array can be changed anytime.

I would greatly appreciate it if you could help me

2
  • would seem trivial to do with a foreach() loop. why not give it a go. Commented Jun 3, 2015 at 1:49
  • get you explain how looping do you mean ? I do not understand how the implementation of the concept of the array foreach loop @Dagon Commented Jun 3, 2015 at 1:59

1 Answer 1

2

Read up on the basic language control structures and foreach in particular.

foreach ($array2 as $index => $object) {
    if (isset($array1[$index])) {
        $object->number = $array1[$index];
    }
}

Outcome:

array(3) {
  [0]=>
  object(stdClass)#1 (3) {
    ["address"]=>
    string(9) "Address 1"
    ["price"]=>
    string(6) "120000"
    ["number"]=>
    string(2) "47"
  }
  [1]=>
  object(stdClass)#2 (3) {
    ["address"]=>
    string(9) "Address 2"
    ["price"]=>
    string(6) "150000"
    ["number"]=>
    string(2) "48"
  }
  [2]=>
  object(stdClass)#3 (3) {
    ["address"]=>
    string(9) "Address 3"
    ["price"]=>
    string(6) "180000"
    ["number"]=>
    string(2) "49"
  }
}

Here is a Codepad demo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.