so have 2 array, first 1 like:
$myarray = [ ['id' => 1, number => 32], ['id' => 2, number => 4] ];
and other like:
$otherarray = [ ['id' => 1, 'show' => x], ['id' => 5, 'show' => x] ];
where x, want equal 'number' value of $myarray key 'id' = id.
if there no $myarray.id
equal $otherarray.id
should return 0.
i hope understand mean. tried everything, could, yet, no success.
have tried using foreach
loop here?
here quick example... phpaste snippet
<?php $firstarray = array( array( "id" => 1, "something" => "hello, world!" ), array( "id" => 3, // 3 on purpose "something" => "hello, mom?" ) ); $secondarray = array( array( "id" => 1, "thing" => null ), array( "id" => 2, "thing" => null ) ); foreach ($firstarray $key => $value) { foreach ($secondarray $k => $v) { if ($value['id'] == $v['id']) { echo "found one!\n------\n" . print_r($value, true) . "\ncontains same id as\n\n" . print_r($v, true) . "\n------\n"; // may if want // $secondarray[$k]['thing'] = $value['id']; // set "thing" (in second array) value of "id" (in first array) } } }
edit here second example, displaying how use function... phpaste snippet.
note: used old array syntax because it's easier new programmers understand.
so, doing iterating through each item in $firstarray
, comparing each item in $secondarray
, doing nested foreach
within side first foreach
if makes sense...?
here said in simple form:
go through each item in array 1 --> compare each item in array 2
you may notice use of php's lovely function, print_r()
. displays objects , arrays in slightly, clearer, form.
you can see getting values within arrays using $value['id']
, $v['id']
. these defined in foreach declaration, foreach ($firstarray $key => $value)
; $value
associative array, can value key if created array this:
$myarray = [ "id" => 1 ];
and grabbed values this:
echo $myarray['id']; // 1
hopefully helped.