I am try to loop through the following stdClass Object array.
Array
(
[0] => stdClass Object
(
[key] => 49
[values] => Array
(
[0] => Kansas City Chiefs
[1] => -3
[2] => Denver Broncos
[3] => 3
[4] => 41
[5] => 2015-09-18 00:25:00
)
)
[1] => stdClass Object
(
[key] => 50
[values] => Array
(
[0] => Carolina Panthers
[1] => -3.5
[2] => Houston Texans
[3] => 3.5
[4] => 40
[5] => 2015-09-20 17:00:00
)
)
[2] => stdClass Object
(
[key] => 51
[values] => Array
(
[0] => New Orleans Saints
[1] => -10
[2] => Tampa Bay Buccaneers
[3] => 10
[4] => 47
[5] => 2015-09-20 17:00:00
)
)
[3] => stdClass Object
(
[key] => 52
[values] => Array
(
[0] => Pittsburgh Steelers
[1] => -6
[2] => San Francisco 49ers
[3] => 6
[4] => 45
[5] => 2015-09-20 17:00:00
)
)
[4] => stdClass Object
(
[key] => 53
[values] => Array
(
[0] => Minnesota Vikings
[1] => -3
[2] => Detroit Lions
[3] => 3
[4] => 43
[5] => 2015-09-20 17:00:00
)
)
[5] => stdClass Object
(
[key] => 54
[values] => Array
(
[0] => Buffalo Bills
[1] => 1
[2] => New England Patriots
[3] => -1
[4] => 45
[5] => 2015-09-20 17:00:00
)
)
[6] => stdClass Object
(
[key] => 55
[values] => Array
(
[0] => Chicago Bears
[1] => 2
[2] => Arizona Cardinals
[3] => -2
[4] => 45
[5] => 2015-09-20 17:00:00
)
)
[7] => stdClass Object
(
[key] => 56
[values] => Array
(
[0] => Cleveland Browns
[1] => 1
[2] => Tennessee Titans
[3] => -1
[4] => 41
[5] => 2015-09-20 17:00:00
)
)
[8] => stdClass Object
(
[key] => 57
[values] => Array
(
[0] => Cincinnati Bengals
[1] => -3.5
[2] => San Diego Chargers
[3] => 3.5
[4] => 46
[5] => 2015-09-20 17:00:00
)
)
[9] => stdClass Object
(
[key] => 58
[values] => Array
(
[0] => Washington Redskins
[1] => 3.5
[2] => St. Louis Rams
[3] => -3.5
[4] => 41
[5] => 2015-09-20 17:00:00
)
)
[10] => stdClass Object
(
[key] => 59
[values] => Array
(
[0] => New York Giants
[1] => -2.5
[2] => Atlanta Falcons
[3] => 2.5
[4] => 51
[5] => 2015-09-20 17:00:00
)
)
[11] => stdClass Object
(
[key] => 60
[values] => Array
(
[0] => Oakland Raiders
[1] => 6
[2] => Baltimore Ravens
[3] => -6
[4] => 43
[5] => 2015-09-20 20:05:00
)
)
[12] => stdClass Object
(
[key] => 61
[values] => Array
(
[0] => Jacksonville Jaguars
[1] => 6
[2] => Miami Dolphins
[3] => -6
[4] => 41
[5] => 2015-09-20 20:05:00
)
)
[13] => stdClass Object
(
[key] => 62
[values] => Array
(
[0] => Philadelphia Eagles
[1] => -5
[2] => Dallas Cowboys
[3] => 5
[4] => 55
[5] => 2015-09-20 20:25:00
)
)
[14] => stdClass Object
(
[key] => 63
[values] => Array
(
[0] => Green Bay Packers
[1] => -3.5
[2] => Seattle Seahawks
[3] => 3.5
[4] => 49
[5] => 2015-09-21 00:30:00
)
)
[15] => stdClass Object
(
[key] => 64
[values] => Array
(
[0] => Indianapolis Colts
[1] => -7
[2] => New York Jets
[3] => 7
[4] => 47
[5] => 2015-09-22 00:30:00
)
)
)
When I use the following syntax it prints one line correctly and one line blank
foreach($tableData as $obj)
{
foreach ($obj as $tr)
{
echo '<tr><td>'.$mysqldate = date( 'g:ia - D', $phpdate = strtotime($tr[5])- 60 * 60 * 5).'</td><td><strong>'.($i += 4).'</strong> '.$tr[0].'<br><br><strong>'.($iii += 4).'</strong> Over</td><td>'.$tr[1].'<br><br>'.$tr[4].'</td><td><strong>'.($ii += 4).'</strong> '.$tr[2].'<br><br><strong>'.($iiii += 4).'</strong> Under</td><td>'.$tr[3].'<br><br>'.$tr[4].'</td></tr>';
}
}
My guess is that its looping through the key, when is where it comes up blank, than it goes through the values where i get the results I want. I've tried a few things that does seem to work. How do I loop through avoiding the [key]?
You need to access the object's values property instead, like this:
foreach($tableData as $obj)
{
$tr = $obj->values;
echo ....
}
Try this:
foreach($tableData as $key => $val){
$val2 = $val->values;
echo $val[0];
}
Related
I started using Blade template engine with codeigniter, and Google Api
i use blade to develope an old project created by another developers for this reason i have to keep the same blade data structure.
this is the Blade code written by a different developpers (i need to keep it ):
#foreach ($analytics['countries'] as $country)
['{{$country['country']}}', {{$country['visits_percent']}}],
#endforeach
this is my PHP code
$this->blade->view()->make('analytics/analytics', ['analytics' => $analytics]);
and this is my $analtics array :
Array
(
[0] => Array
(
[0] => Canada
[1] => 3367
)
[1] => Array
(
[0] => United States
[1] => 202
)
[2] => Array
(
[0] => Malaysia
[1] => 34
)
[3] => Array
(
[0] => Mexico
[1] => 31
)
[4] => Array
(
[0] => Peru
[1] => 23
)
[5] => Array
(
[0] => Brazil
[1] => 21
)
[6] => Array
(
[0] => United Kingdom
[1] => 21
)
[7] => Array
(
[0] => Netherlands
[1] => 17
)
[8] => Array
(
[0] => Nepal
[1] => 14
)
[9] => Array
(
[0] => India
[1] => 12
)
[10] => Array
(
[0] => Belarus
[1] => 11
)
[11] => Array
(
[0] => France
[1] => 9
)
[12] => Array
(
[0] => Ireland
[1] => 9
)
[13] => Array
(
[0] => Germany
[1] => 7
)
[14] => Array
(
[0] => Philippines
[1] => 6
)
[15] => Array
(
[0] => Singapore
[1] => 5
)
[16] => Array
(
[0] => Bangladesh
[1] => 4
)
[17] => Array
(
[0] => Italy
[1] => 4
)
[18] => Array
(
[0] => Serbia
[1] => 4
)
[19] => Array
(
[0] => Australia
[1] => 3
)
[20] => Array
(
[0] => Belgium
[1] => 3
)
[21] => Array
(
[0] => Indonesia
[1] => 3
)
[22] => Array
(
[0] => Iran
[1] => 3
)
[23] => Array
(
[0] => Jordan
[1] => 3
)
[24] => Array
(
[0] => Morocco
[1] => 3
)
[25] => Array
(
[0] => China
[1] => 2
)
[26] => Array
(
[0] => Colombia
[1] => 2
)
[27] => Array
(
[0] => Moldova
[1] => 2
)
[28] => Array
(
[0] => Pakistan
[1] => 2
)
[29] => Array
(
[0] => Poland
[1] => 2
)
[30] => Array
(
[0] => Romania
[1] => 2
)
[31] => Array
(
[0] => South Africa
[1] => 2
)
[32] => Array
(
[0] => Thailand
[1] => 2
)
[33] => Array
(
[0] => Turkey
[1] => 2
)
[34] => Array
(
[0] => Argentina
[1] => 1
)
[35] => Array
(
[0] => El Salvador
[1] => 1
)
[36] => Array
(
[0] => Hong Kong
[1] => 1
)
[37] => Array
(
[0] => Japan
[1] => 1
)
[38] => Array
(
[0] => Kenya
[1] => 1
)
[39] => Array
(
[0] => Latvia
[1] => 1
)
[40] => Array
(
[0] => New Zealand
[1] => 1
)
[41] => Array
(
[0] => Russia
[1] => 1
)
[42] => Array
(
[0] => Saudi Arabia
[1] => 1
)
[43] => Array
(
[0] => South Korea
[1] => 1
)
)
my question is how can i produce the same array structure for blade
thank you
I hope this code useful for you :
Route::get('/test', function (Request $request) {
$analytics=[
[
'Canada',
3367
],
[
'United States',
202
],
[
'Malaysia',
34
],
];
return view('welcome',compact('analytics'));
})
and you can use this code in view :
#foreach( $analytics as $analytic)
<p>
country = {{$analytic[0]}}
<br>
code = {{$analytic[1]}}
<tr>
</p>
#endforeach
hello i have this type of array,
Array
(
[1] => Array
(
[0] => FGC00092
[splcode] => FGC00092
[1] => 2016-05-02,2016-05-09
[tran_date] => 2016-05-02,2016-05-09
[2] => 3,40
[unit] => 3,40
[3] => 300,320
[value] => 300,320
)
[2] => Array
(
[0] => FGC00085
[splcode] => FGC00085
[1] => 2016-05-06
[tran_date] => 2016-05-06
[2] => 2
[unit] => 2
[3] => 400
[value] => 400
)
[3] => Array
(
[0] => FGC00099
[splcode] => FGC00099
[1] => 2016-05-06
[tran_date] => 2016-05-06
[2] => 3
[unit] => 3
[3] => 600
[value] => 600
)
[4] => Array
(
[0] => FGC00092
[splcode] => FGC00092
[1] => 2016-05-09
[tran_date] => 2016-05-09
[2] => 40
[unit] => 40
[3] => 320
[value] => 320
)
[5] => Array
(
[0] => FGC00115
[splcode] => FGC00115
[1] => 2016-05-10
[tran_date] => 2016-05-10
[2] => 476
[unit] => 476
[3] => 3808
[value] => 3808
)
[6] => Array
(
[0] => FGC00062
[splcode] => FGC00062
[1] => 2016-05-25
[tran_date] => 2016-05-25
[2] => 10
[unit] => 10
[3] => 80
[value] => 80
)
[7] => Array
(
[0] => FGC00059
[splcode] => FGC00059
[1] => 2016-05-30
[tran_date] => 2016-05-30
[2] => 50
[unit] => 50
[3] => 400
[value] => 400
)
[8] => Array
(
[0] => FGC00117
[splcode] => FGC00117
[1] => 2016-05-31
[tran_date] => 2016-05-31
[2] => 50
[unit] => 50
[3] => 400
[value] => 400
)
)
i want to combine the them which have same ID. like for FGC00092, array structure must be,
Array
(
[1] => Array
(
[0] => FGC00092
[splcode] => FGC00092
[1] => 2016-05-02
[tran_date] => 2016-05-02
[2] => 3
[unit] => 3
[3] => 300
[value] => 300
)
[2] => Array
(
[0] => FGC00085
[splcode] => FGC00085
[1] => 2016-05-06
[tran_date] => 2016-05-06
[2] => 2
[unit] => 2
[3] => 400
[value] => 400
)
[3] => Array
(
[0] => FGC00099
[splcode] => FGC00099
[1] => 2016-05-06
[tran_date] => 2016-05-06
[2] => 3
[unit] => 3
[3] => 600
[value] => 600
)
[4] => Array
(
[0] => FGC00115
[splcode] => FGC00115
[1] => 2016-05-10
[tran_date] => 2016-05-10
[2] => 476
[unit] => 476
[3] => 3808
[value] => 3808
)
[5] => Array
(
[0] => FGC00062
[splcode] => FGC00062
[1] => 2016-05-25
[tran_date] => 2016-05-25
[2] => 10
[unit] => 10
[3] => 80
[value] => 80
)
[6] => Array
(
[0] => FGC00059
[splcode] => FGC00059
[1] => 2016-05-30
[tran_date] => 2016-05-30
[2] => 50
[unit] => 50
[3] => 400
[value] => 400
)
[7] => Array
(
[0] => FGC00117
[splcode] => FGC00117
[1] => 2016-05-31
[tran_date] => 2016-05-31
[2] => 50
[unit] => 50
[3] => 400
[value] => 400
)
)
You can use foreach and in_array to solve your issue as below.
$tempArray = $outArray = array();
$count = 1;
foreach($yourArray as $aKey=>$aVal){
if (in_array($aVal['splcode'], $aVal)){
//do something
continue; // it will skip the current loop if the condition satisfies
}
$outArray[$count] = $aVal;
$tempArray[] = $aVal['splcode'];
$count++;
}
//You will get the result in the variable $outArray
unset($tempArray); // clear the temp array
The array_unique function will do this for you. You just needed to add the SORT_REGULAR flag:
$items_thread = array_unique($items_thread, SORT_REGULAR);
I am new to PHP multi-dimentional arrays, So I need 100% working answer please...
I had tried it by using muli-sort and usort function but they are not working for me, honestly i didn't know how to use them in case of multi-dimentional arrays!
In my case (how I am implementing multi-sort), it is only sorting the first day's array
Array (
[0] => Array (
[0] => Array (
[0] => Cycling
[1] => 03:30 PM-04:00 PM
[2] => Criterium International
[3] => 0.5
[4] => SN1
[5] => 100
[6] => 1459524600
)
[1] => Array (
[0] => Rugby
[1] => 03:00 PM-05:15 PM
[2] => Super League Rugby - Wolves v Warriors
[3] => 2.25
[4] => SNOverflow
[5] => 100
[6] => 1459522800
)
[2] => Array (
[0] => Rugby
[1] => 05:00am-07:30 AM
[2] => National Rugby League Titans vs Broncos
[3] => 2.5
[4] => SNWorld
[5] => 100
[6] => 1459485000
)
)
[1] => Array (
[0] => Array (
[0] => Rugby
[1] => 09:45 AM-12:00 PM
[2] => Super League Rugby - Red Devils v E
[3] => 2.25
[4] => SNOverflow
[5] => 100
[6] => 1459590300
)
[1] => Array (
[0] => Rugby
[1] => 12:00 PM-02:00 PM
[2] => Super League Rugby - Vikings v Dragons
[3] => 2
[4] => SNOverflow
[5] => 100
[6] => 1459598400
)
[2] => Array (
[0] => BPL Soccer
[1] => 07:30 AM-09:45 AM
[2] => Aston Villa v Chelsea
[3] => 2.25
[4] => SNRegions
[5] => 100
[6] => 1459582200
)
)
[2] => Array (
[0] => Array (
[0] => The Wheel Highlights
[1] => 11:00 PM-12:30 AM
[2] =>
[3] => 1.5
[4] => Sportsnet 360
[5] => 100
[6] => 1459724400
)
[1] => Array (
[0] => The Wheel Highlights
[1] => 10:00 PM-11:00 PM
[2] =>
[3] => 1
[4] => Sportsnet 360
[5] => 100
[6] => 1459720800
)
[2] => Array (
[0] => CHL Hockey
[1] => 06:00 PM-09:00 PM
[2] => Brandon # Edmonton
[3] => 3
[4] => Sportsnet 360
[5] => 100
[6] => 1459706400
)
)
)
My desired output is like this:
Array (
[0] => Array (
[0] => Array (
[0] => Rugby
[1] => 05:00am-07:30 AM
[2] => National Rugby League Titans vs Broncos
[3] => 2.5
[4] => SNWorld
[5] => 100
[6] => 1459485000
)
[1] => Array (
[0] => Rugby
[1] => 03:00 PM-05:15 PM
[2] => Super League Rugby - Wolves v Warriors
[3] => 2.25
[4] => SNOverflow
[5] => 100
[6] => 1459522800
)
[2] => Array (
[0] => Cycling
[1] => 03:30 PM-04:00 PM
[2] => Criterium International
[3] => 0.5
[4] => SN1
[5] => 100
[6] => 1459524600
)
)
[1] => Array (
[0] => Array (
[0] => BPL Soccer
[1] => 07:30 AM-09:45 AM
[2] => Aston Villa v Chelsea
[3] => 2.25
[4] => SNRegions
[5] => 100
[6] => 1459582200
)
[1] => Array (
[0] => Rugby
[1] => 09:45 AM-12:00 PM
[2] => Super League Rugby - Red Devils v E
[3] => 2.25
[4] => SNOverflow
[5] => 100
[6] => 1459590300
)
[2] => Array (
[0] => Rugby
[1] => 12:00 PM-02:00 PM
[2] => Super League Rugby - Vikings v Dragons
[3] => 2
[4] => SNOverflow
[5] => 100
[6] => 1459598400
)
)
[2] => Array (
[0] => Array (
[0] => CHL Hockey
[1] => 06:00 PM-09:00 PM
[2] => Brandon # Edmonton
[3] => 3
[4] => Sportsnet 360
[5] => 100
[6] => 1459706400
)
[1] => Array (
[0] => The Wheel Highlights
[1] => 10:00 PM-11:00 PM
[2] =>
[3] => 1
[4] => Sportsnet 360
[5] => 100
[6] => 1459720800
)
[2] => Array (
[0] => The Wheel Highlights
[1] => 11:00 PM-12:30 AM
[2] =>
[3] => 1.5
[4] => Sportsnet 360
[5] => 100
[6] => 1459724400
)
)
)
Probably your code has failed due to multi levels array. To sort items of each first level you have to perform a foreach on the first level and process it by reference, otherwise the sort result doesn't affect main array:
# ↓ “&” means “by reference”
foreach( $array as &$sub )
{
usort ( $sub, function( $a, $b ) { return $a[6] - $b[6]; } );
}
eval.in demo
I am using MultipleIterator() to iterate through two different arrays and get each element.
My code
$d = new MultipleIterator ();
$d->attachIterator ( new ArrayIterator ( $tbl_one_data ) );
$d->attachIterator ( new ArrayIterator ( $tbl_two_data ) );
foreach ( $d as $data ) {
print_r($data);
}
Which generates the following :
My question is how do I loop through the array and return each element? For example I would like to return 2014-11-06 11:31:58.781018. Tried using $data[0][0] but this returns all elements in the first index but I only want one element.
EDIT
print_r($d);
MultipleIterator Object ( [storage:SplObjectStorage:private] => Array ( [00000000583bd67b000000000ac6c449] => Array ( [obj] => ArrayIterator Object ( [storage:ArrayIterator:private] => Array ( [0] => Array ( [0] => 1 [1] => 2014-11-06 11:31:58.781018 [2] => NONE [3] => NONE ) [1] => Array ( [0] => 2 [1] => 2014-11-06 11:31:58.799436 [2] => MANAGER [3] => 500 ) [2] => Array ( [0] => 3 [1] => 2014-11-06 11:31:58.841035 [2] => MANAGER [3] => 501 ) [3] => Array ( [0] => 4 [1] => 2014-11-06 11:33:00.741873 [2] => MANAGER [3] => 500 ) [4] => Array ( [0] => 5 [1] => 2014-11-06 11:33:00.802389 [2] => MANAGER [3] => 501 ) [5] => Array ( [0] => 6 [1] => 2014-11-06 13:15:49.457646 [2] => MANAGER [3] => 500 ) [6] => Array ( [0] => 7 [1] => 2014-11-06 13:37:16.259128 [2] => NONE [3] => NONE ) [7] => Array ( [0] => 8 [1] => 2014-11-06 13:37:16.275201 [2] => NONE [3] => 500 ) [8] => Array ( [0] => 9 [1] => 2014-11-06 13:37:27.682873 [2] => NONE [3] => NONE ) [9] => Array ( [0] => 10 [1] => 2014-11-06 13:37:27.690863 [2] => NONE [3] => 500 ) [10] => Array ( [0] => 11 [1] => 2014-11-06 13:52:21.108003 [2] => MANAGER [3] => 500 ) [11] => Array ( [0] => 12 [1] => 2014-11-06 14:17:01.266769 [2] => NONE [3] => NONE ) [12] => Array ( [0] => 13 [1] => 2014-11-06 14:17:01.279507 [2] => node1-1415283420.0 [3] => 500 ) [13] => Array ( [0] => 14 [1] => 2014-11-06 14:17:02.527183 [2] => node1-1415283420.0 [3] => 500 ) [14] => Array ( [0] => 15 [1] => 2014-11-06 14:17:23.775279 [2] => node1-1415283442.1 [3] => 500 ) ) ) [inf] => ) [00000000583bd67a000000000ac6c449] => Array ( [obj] => ArrayIterator Object ( [storage:ArrayIterator:private] => Array ( [0] => Array ( [0] => NONE [1] => QUEUESTART [2] => [3] => [4] => [5] => [6] => ) [1] => Array ( [0] => Local/120#disc-agents/n [1] => ADDMEMBER [2] => [3] => [4] => [5] => [6] => ) [2] => Array ( [0] => Local/120#disc-agents/n [1] => ADDMEMBER [2] => [3] => [4] => [5] => [6] => ) [3] => Array ( [0] => Local/120#disc-agents/n [1] => REMOVEMEMBER [2] => [3] => [4] => [5] => [6] => ) [4] => Array ( [0] => Local/120#disc-agents/n [1] => REMOVEMEMBER [2] => [3] => [4] => [5] => [6] => ) [5] => Array ( [0] => Local/120#disc-agents/n [1] => ADDMEMBER [2] => [3] => [4] => [5] => [6] => ) [6] => Array ( [0] => Local/120#disc-agents/n [1] => PAUSEALL [2] => [3] => [4] => [5] => [6] => ) [7] => Array ( [0] => Dunc Test [1] => PAUSE [2] => [3] => [4] => [5] => [6] => ) [8] => Array ( [0] => Local/120#disc-agents/n [1] => UNPAUSEALL [2] => [3] => [4] => [5] => [6] => ) [9] => Array ( [0] => Dunc Test [1] => UNPAUSE [2] => [3] => [4] => [5] => [6] => ) [10] => Array ( [0] => Local/120#disc-agents/n [1] => REMOVEMEMBER [2] => [3] => [4] => [5] => [6] => ) [11] => Array ( [0] => NONE [1] => QUEUESTART [2] => [3] => [4] => [5] => [6] => ) [12] => Array ( [0] => NONE [1] => ENTERQUEUE [2] => [3] => 363 [4] => 1 [5] => [6] => ) [13] => Array ( [0] => NONE [1] => ABANDON [2] => 1 [3] => 1 [4] => 1 [5] => [6] => ) [14] => Array ( [0] => NONE [1] => ENTERQUEUE [2] => [3] => 363 [4] => 1 [5] => [6] => ) ) ) [inf] => ) ) )
if this is really output by print_r then $data[0][1] must be '2014-11-06 11:31:58.781018' . I test it by code:
$data = array(array('1','2014-11-06 11:31:58.781018'));
echo '<pre>';
print_r($data); echo '<br>';
echo 'what we want: '.$data[0][1].'<br>';
otput:
Array
(
[0] => Array
(
[0] => 1
[1] => 2014-11-06 11:31:58.781018
)
)
what we want: 2014-11-06 11:31:58.781018
If you have the $data as an array you can access the value: 2014-11-06 11:31:58.781018 by using,
$date = $data[0][1];
been woking on a project of mine for a few days now, and essentialy what i'm triing to do in this project is a comparison of csv file that a user normaly does in excel, to do it in php automaticly every night.
I got the info from the CSV's intro arrays, but i'm having trouble combinig them to get the right info for every book, hence the folowing exemple and question.
I have the folowing array (array1) from a csv file :
Array
(
[0] => Array
(
[0] => book1
[1] => description1
[2] => category1
[3] => code1
[4] => editor1
[5] => 0
[6] => eur
[7] => out of stoc
)
[1] => Array
(
[0] => book2
[1] => description2
[2] => category2
[3] => code2
[4] => editor2
[5] => 0
[6] => curr2
[7] => out of stoc
)
[2] => Array
(
[0] => book3
[1] => description3
[2] => category3
[3] => code3
[4] => editor3
[5] => 0
[6] => curr3
[7] => out of stoc
)
[3] =>
)
and another array (array2) from a second csv file :
Array
(
[0] => Array
(
[0] => book1
[1] => description_from_array2
[2] => category_from_array2
[3] => code_from_array2
[4] => editor_from_array2
[5] => 12
[6] => eur
[7] => in stoc
)
[1] => Array
(
[0] => book2
[1] => description_from_array2
[2] => category_from_array2
[3] => code_from_array2
[4] => editor_from_array2
[5] => 13
[6] => eur
[7] => in stoc
)
[2] => Array
(
[0] => book4
[1] => description_from_array2
[2] => category_from_array2
[3] => code_from_array2
[4] => editor_from_array2
[5] => 14
[6] => usd
[7] => in stoc
)
[3] => Array
(
[0] => book5
[1] => description_from_array2
[2] => category_from_array2
[3] => code_from_array2
[4] => editor_from_array2
[5] => 16
[6] => usd
[7] => in stoc
)
)
I would like to know how to get the values form array2 intro array1 for the books of array1 found in array2.
Ex:
Array
(
[0] => Array
(
[0] => book1
[1] => description2_from_array2
[2] => category2_from_array2
[3] => code2_from_array2
[4] => editor2_from_array2
[5] => 12
[6] => eur
[7] => in stoc
)
[1] => Array
(
[0] => book2
[1] => description_from_array2
[2] => category_from_array2
[3] => code_from_array2
[4] => editor_from_array2
[5] => 13
[6] => curr_from_array2
[7] => in stoc
)
[2] => Array
(
[0] => book3
[1] => description3
[2] => category3
[3] => code3
[4] => editor3
[5] => 0
[6] => curr3
[7] => out of stoc //because book3 is not found in array2
)
[3] =>
)
Any help for this question would be greatly appreciated, belive me!
//Add keys to array 2 to aid lookup
$array2Tmp = array();
foreach($array2 as $item){
$array2Tmp[$item[0]] = $item;
}
$array3 = array();
foreach($array1 as $item){
//Use item from second array if it exists
if(array_key_exists($item[0],$array2Tmp)){
$array3[] = $array2Tmp[$item[0]];
}else{
$array3[] = $item;
}
}