HI,
I got two array.
var_dump($tbDateArr);
var_dump($tbTitleArr);
output:
array
0 =>
object(stdClass)[16]
public 'eventDate' => string '5' (length=1)
1 =>
object(stdClass)[17]
public 'eventDate' => string '16' (length=2)
array
0 =>
object(stdClass)[18]
public 'eventTitle' => string 'mar' (length=10)
1 =>
object(stdClass)[19]
public 'eventTitle' => string 'tri' (length=10)
BTW,I print them as this,
print_r($tbDateArr);
echo '<br>';
print_r($tbTitleArr);
Array ( [0] => stdClass Object ( [eventDate] => 5 ) [1] => stdClass Object ( [eventDate] => 16 ) )
Array ( [0] => stdClass Object ( [eventTitle] => mar ) [1] => stdClass Object ( [eventTitle] => tri ) )
I tried to combin them,
$dataArr = array_combine($tbDateArr, $tbTitleArr);
I JUST WANT THE SIMPLY RESULT as this,
Array
(
[5] => mar
[16] => tri
)
Is there anything wrong? Appreciated for your help.
[updated with array_merge]
array
0 =>
object(stdClass)[16]
public 'eventDate' => string '5' (length=1)
1 =>
object(stdClass)[17]
public 'eventDate' => string '16' (length=2)
2 =>
object(stdClass)[18]
public 'eventTitle' => string 'fuzhou mar' (length=10)
3 =>
object(stdClass)[19]
public 'eventTitle' => string 'weihai tri' (length=10)
Assuming that $tbDateArr
and $tbTitleArr
entries only have a single property (eventDate
and eventTitle
respectively), you can do this:
$array = array_combine(
array_map('current', $tbDateArr),
array_map('current', $tbTitleArr)
);
If they have (or can have) more than a single property, you're better off using a good old foreach
, assuming they have matching keys (if they don't, just array_values
them beforehand):
$array = array();
foreach ($tbDateArr as $key => $value) {
$array[$value] = $tbTableArr[$key];
}
EDIT this works
$arr1 = array(
"5",
"16",
);
$arr2 = array(
"mar",
"tri",
);
$result = array_combine($arr1, $arr2);
print_r($result);
You are trying to combine arrays which containing objects. array_combine using its first argument for keys second argument as values.
You can't directly combine the arrays, since the values you need are stored as properties in objects -- you have to pull these values out and use them:
$keys = array_map('getEventDate', $tbDateArr);
$values = array_map('getEventDate', $tbTitleArr);
print_r(array_combine($keys, $values));
function getEventDate($o) {
return $o->eventDate;
}