Remove all values => 0 from array and reverse it - php

I am a PHP beginner and have the following problem that I completely get stuck. I have an array in this format
Array (
[date] => 0
[author] => 1
[categories] => 1
[tags] => 0
[comments] => 0
[readmore] => 0
)
Now I need to remove all items that are [value] => 0 from that array and get it in this format
Array (
[0] => author
[1] => categories
)
Any input in this is much appreciated.

use array_filter and then array_keys:
$array = array ( 'date' => 0, 'author' => 1, 'categories' => 1, 'tags' => 0, 'comments' => 0, 'readmore' => 0 );
$array = array_filter($array);
print_r(array_keys($array));
output
Array
(
[0] => author
[1] => categories
)

There is a function called array_search() out there. Assuming the only possible values are 0 and 1, you can just serach the whole array for this value:
$array = [
'date' => 0,
'author' => 1,
'categories' => 1,
'tags' => 0,
'comments' => 0,
'readmore' => 0
];
$new_array = array_flip(array_search(1, $array));
So print_r($new_array) will give you your desired result.

You can try something like this:
foreach($array as $item){
if($item == "0"){
unset($item);
}
}
Or some modification with using unset function.

This is also do the job...
$dataSource = array('data' => 0,
'author' => 1,
'categories' => 1,
'tags' => 0,
'comments' => 0,
'readmore' => 0,
);
$updatedDataList = array();
foreach ($dataSource as $key => $value) {
if($value !== 0) {
$updatedDataList[] = $key;
}
}
print_r($updatedDataList);

#savethegold you can save your gold like below :D
<?php
$Array1 = array("date" => 0, "author" => 1, "categories" => 1, "tags" => 0, "comments" => 0, "readmore" => 0 );
foreach ($Array1 as $key => $value) {
if($value == 0){
unset($Array1[$key]);
}
}
$finalArr = array_keys($Array1);
print_r($finalArr);

You can try following code
$yourArray = Array ("date" => 0,"author" => 1, "categories" => 1,"tags" => 0, "comments" => 0,"readmore" => 0 );
$removeZeroFromArray = array(0);
$desiredResult = array_diff($yourArray, $removeZeroFromArray);
echo"<pre>";
print_r(array_keys($desiredResult));
echo"</pre>";
Your output will be like
Array
(
[0] => author
[1] => categories
)
Alternate way should be
$desiredResult = array_filter($yourArray, function($a) { return ($a !== 0); });
Hope it helps!

<?php
$array = array ( 'date' => 0, 'author' => 1, 'categories' => 1, 'tags' => 0, 'comments' => 0, 'readmore' => 0 );
$resutl = [];
foreach($array as $k => $v)
{
if($v != 0)
$result[] = $k;
}
print_r($result);

Related

shift multidimentional array to single array

I want to remove key 0 from parent array and set child array as parent.
Here I will get single value so one array is ok for me.
My current array looks like this
Array
(
[0] => Array
(
[id] => 3
[api_key] => acount266
[auth_domain] => Tester26
[database_url] => vcc.test.acc+27#gmail.com
[project_id] => 12345
[storage_bucket] =>
[secret_key_path] =>
[fcm_server_key] => 1
[messaging_sender_id] => 0
[key_phrase] =>
[disable] => 0
[created] =>
[updated] =>
)
)
I want it like below. expected result
Array
(
[id] => 3
[api_key] => acount266
[auth_domain] => Tester26
[database_url] => vcc.test.acc+27#gmail.com
[project_id] => 12345
[storage_bucket] =>
[secret_key_path] =>
[fcm_server_key] => 1
[messaging_sender_id] => 0
[key_phrase] =>
[disable] => 0
[created] =>
[updated] =>
)
For this I tried like below but no success.
$new = array();
foreach ($data as $v){
$new = array_merge($new , array_values($v)) ;
}
but in my code it's removed key e.g id,api_key, etc....
I need key name also in my new array. please suggest
Remove the array_values
Solution
<?php
$test = array(
array
(
'id' => 3,
'api_key' => 'acount266'
)
);
$new = array();
foreach($test as $v){
$new = array_merge($new, $v);
}
var_dump($new);
Result
array(2) {
["id"]=>
int(3)
["api_key"]=>
string(9) "acount266"
}
According to documentation of PHP as mentioned
reset() function returns the value of the first array element, or
FALSE if the array is empty.
$array = array(
array(
'id' => 3,
'api_key' => 'acount266',
'auth_domain' => 'Tester26',
'database_url' => 'vcc.test.acc+27#gmail.com',
'project_id' => '12345',
'storage_bucket' => '',
'secret_key_path' => '',
'fcm_server_key' => 1,
'messaging_sender_id' => 0,
'key_phrase' => '',
'disable' => 0,
'created' => '',
'updated' => ''
)
);
print_r(reset($test));
I tried this:
$arr = array();
foreach ($examples as $example) {
foreach ($example as $e) {
array_push($arr, $e);
}
}
Don't overcomplicate this, reassigning the first element to the parent array is quick and easy:
<?php
$array =
array (
0 =>
array (
'first' => 'Michael',
'last' => 'Thompson'
)
);
$array = $array[0];
var_export($array);
Output:
array (
'first' => 'Michael',
'last' => 'Thompson',
)
Or:
$array = array_shift($array);

php - how to sum values of the multidimensional array

how can I sum values of the key 'NILAI_ANGGARAN'? Note that NILAI_ANGGARAN key is dynamic.
array :
[1350] => Array
(
[495] => Array
(
[NILAI_ANGGARAN] => 11000000
[NILAI_PPN] => 1000000
[PFK] => 0
[TAPERUM] => 0
[LAIN_LAIN] => 0
[NILAI_PPH21] => 500000
[NILAI_PPH22] => 0
[NILAI_PPH23] => 0
[NILAI_PPH4_2] => 0
[DENDA] => 0
[NILAI_BERSIH] => 10500000
)
)
[1300] => Array
(
[488] => Array
(
[NILAI_ANGGARAN] => 15000000
[NILAI_PPN] => 1500000
[PFK] => 0
[TAPERUM] => 0
[LAIN_LAIN] => 0
[NILAI_PPH21] => 0
[NILAI_PPH22] => 450000
[NILAI_PPH23] => 300000
[NILAI_PPH4_2] => 0
[DENDA] => 0
[NILAI_BERSIH] => 15750000
)
)
I've tried solution from How to sum values of the array of the same key? but it getting this error.
Undefined offset: 1350
Update :
This is my desidred result :
Array
(
[NILAI_ANGGARAN] => 26000000
[NILAI_PPN] => 2500000
[PFK] => 0
[TAPERUM] => 0
[LAIN_LAIN] => 0
[NILAI_PPH21] => 500000
[NILAI_PPH22] => 450000
[NILAI_PPH23] => 300000
[NILAI_PPH4_2] => 0
[DENDA] => 0
[NILAI_BERSIH] => 26250000
)
And this is the code I use :
$bruto = array();
foreach($array as $data => $key) {
foreach($key as $k => $value) {
foreach($value as $v => $isi) {
$bruto[$k]+=$value;
}
}
}
print_r($bruto);
Can anyone help me with another solution?
Thx
You can try this
Array:
$multi_dimentional_array = array (
'1350' => array
(
'495' => array
(
'NILAI_ANGGARAN' => 11000000,
'NILAI_PPN' => 1000000,
'PFK' => 0,
'TAPERUM' => 0,
'LAIN_LAIN' => 0,
'NILAI_PPH21' => 500000,
'NILAI_PPH22' => 0,
'NILAI_PPH23' => 0,
'NILAI_PPH4_2' => 0,
'DENDA' => 0,
'NILAI_BERSIH' => 10500000
)
),
'1300' => array
(
'488' => array
(
'NILAI_ANGGARAN' => 15000000,
'NILAI_PPN' => 1500000,
'PFK' => 0,
'TAPERUM' => 0,
'LAIN_LAIN' => 0,
'NILAI_PPH21' => 0,
'NILAI_PPH22' => 450000,
'NILAI_PPH23' => 300000,
'NILAI_PPH4_2' => 0,
'DENDA' => 0,
'NILAI_BERSIH' => 15750000
)
)
);
Get sum values of the key 'NILAI_ANGGARAN' Code:
$NILAI_ANGGARAN_TOTAL = 0;
foreach( $multi_dimentional_array as $fkey=>$smarray )
{
foreach ($smarray as $skey => $value) {
// Empty check
if ( !empty( $value['NILAI_ANGGARAN'] ) ){
$NILAI_ANGGARAN_TOTAL += $value['NILAI_ANGGARAN'];
}
}
}
echo "Sum of NILAI_ANGGARAN is :{$NILAI_ANGGARAN_TOTAL}";
Result:Sum of NILAI_ANGGARAN is :26000000
Let's consider the multi-dimensional array to be $array.
You simply need to use nested loop here.
Try this:
$sum = 0;
foreach ($array as $arr) {
foreach ($arr as $a) {
$sum += $a['NILAI_ANGGARAN'];
}
}
echo $sum;
Hope this helps.
Peace! xD
You also need to check if value with index 'NILAI_ANGGARAN' exists, otherwise PHP shows undefined offset error!
<?php
$array = array (
'1350' => array
(
'495' => array
(
'NILAI_ANGGARAN' => 11000000,
'NILAI_PPN' => 1000000,
'PFK' => 0,
'TAPERUM' => 0,
'LAIN_LAIN' => 0,
'NILAI_PPH21' => 500000,
'NILAI_PPH22' => 0,
'NILAI_PPH23' => 0,
'NILAI_PPH4_2' => 0,
'DENDA' => 0,
'NILAI_BERSIH' => 10500000
)
),
'1300' => array
(
'488' => array
(
'NILAI_ANGGARAN' => 15000000,
'NILAI_PPN' => 1500000,
'PFK' => 0,
'TAPERUM' => 0,
'LAIN_LAIN' => 0,
'NILAI_PPH21' => 0,
'NILAI_PPH22' => 450000,
'NILAI_PPH23' => 300000,
'NILAI_PPH4_2' => 0,
'DENDA' => 0,
'NILAI_BERSIH' => 15750000
)
)
);
$sum = 0;
foreach($array as $key => $subarray)
{
foreach($subarray as $subsubarrray)
{
if(isset($subsubarrray['NILAI_ANGGARAN'])) //check if isset
$sum += $subsubarrray['NILAI_ANGGARAN'];
}
}
var_dump($sum);
you can use advance php for the same. Using RecursiveArrayIterator and RecursiveIteratorIterator it can be done easily:
$multi_dimentional_array = array (
'1350' => array
(
'495' => array
(
'NILAI_ANGGARAN' => 11000000,
'NILAI_PPN' => 1000000,
'PFK' => 0,
'TAPERUM' => 0,
'LAIN_LAIN' => 0,
'NILAI_PPH21' => 500000,
'NILAI_PPH22' => 0,
'NILAI_PPH23' => 0,
'NILAI_PPH4_2' => 0,
'DENDA' => 0,
'NILAI_BERSIH' => 10500000
)
),
'1300' => array
(
'488' => array
(
'NILAI_ANGGARAN' => 15000000,
'NILAI_PPN' => 1500000,
'PFK' => 0,
'TAPERUM' => 0,
'LAIN_LAIN' => 0,
'NILAI_PPH21' => 0,
'NILAI_PPH22' => 450000,
'NILAI_PPH23' => 300000,
'NILAI_PPH4_2' => 0,
'DENDA' => 0,
'NILAI_BERSIH' => 15750000
)
)
);
$sum = 0;
$k_value = 'NILAI_ANGGARAN';
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($multi_dimentional_array));
foreach ($iterator as $key => $value) {
if($key == $k_value){
$sum +=$value;
}
}
echo "SUM of $k_value is $sum";
Output
SUM of NILAI_ANGGARAN is 26000000
use this code :
$sum_arr = array();
foreach($main as $m_item){
foreach($sub as $s_item){
foreach($s_item as $skey => $value){
$sum_arr[$skey] += $value;
}
}
}
use this code it will in $sum_arr is array of all element and its also in array format
try this it will work 100% :)

Extract a row by value from php array

This is my array:
Array (
[0] => Array ( [SocketID] => 1 [SocketName] => Name [SocketDecimal] => 0 [SocketHex] => 00 [SocketAtt] => 1 [Category] => 1 [Value] => 100 [Procentage] => 0 )
[1] => Array ( [SocketID] => 2 [SocketName] => Name2 [SocketDecimal] => 50 [SocketHex] => 32 [SocketAtt] => 1 [Category] => 1 [Value] => 800 [Procentage] => 0 )
[2] => Array ( [SocketID] => 3 [SocketName] => Name3 [SocketDecimal] => 100 [SocketHex] => 64 [SocketAtt] => 1 [Category] => 1 [Value] => 60 [Procentage] => 0 )
)
How can I extract a row by SocketDecimal?
For example: I want to extract row where SocketDecimal = 50 and make new an array only with that row.
foreach($array as $entry) {
if($entry['SocketDecimal'] == 50)
$newArr[] = $entry;
}
$newArr will contain the desired "row". Of course you can manipulate the if-statement depending on which "row" (I'd just call it array entry) you want to extract.
It's not the best way for big data! It's easy for deep multiarrays.
$arr = array(
array('socket_id'=>1,'name'=>'test1'),
array('socket_id'=>2,'name'=>'test2'),
array('socket_id'=>3,'name'=>'test3'),
array('socket_id'=>2,'name'=>'test4')
);
$newArr = array();
foreach($arr as $row){
foreach($row as $key=>$r){
if($key == 'socket_id' && $r==2)
$newArr[] = $row;
}
}
print_r($newArr);
$result = array();
foreach($input as $i){
if($i['SocketDecimal']==50)
$result[]=$i;
}
You can do it by this method
foreach ($yourarray as $key => $value){
$newarray = array("SocketDecimal"=>$value["SocketDecimal"];
}
print_r($newarray);
If your result array is like given below
$arr = array(
array( 'SocketID' => 1, 'SocketName' => 'Name', 'SocketDecimal' => 0, 'SocketHex' => 0, 'SocketAtt' => 1, 'Category' => 1, 'Value' => 100, 'Procentage' => 0 ),
array ( 'SocketID' => 2, 'SocketName' => 'Name2', 'SocketDecimal' => 50, 'SocketHex' => 32, 'SocketAtt' => 1, 'Category' => 1, 'Value' => 800, 'Procentage' => 0 ),
array ( 'SocketID' => 3, 'SocketName' => 'Name3', 'SocketDecimal' => 100, 'SocketHex' => 64, 'SocketAtt' => 1, 'Category' => 1, 'Value' => 60, 'Procentage' => 0 )
);
print_r($arr);
Get row for SocketDecimal=50 by following loop:
<pre>
$resultArr = '';
foreach($arr as $recordSet)
{
if($recordSet['SocketDecimal'] == 50)
{
$resultArr[] = $recordSet;
break;
}
}
</pre>
print_r($resultArr);
break foreach loop so that it will not traverse for all the array when SocketDecimal(50) founded.
You can use array_column + array_search combo
$array = Array (
"0" => Array ( "SocketID" => 1, "SocketName" => "Name", "SocketDecimal" => 0, "SocketHex" => 00, "SocketAtt" => 1, "Category" => 1, "Value" => 100, "Procentage" => 0 ) ,
"1" => Array ( "SocketID" => 2, "SocketName" => "Name2", "SocketDecimal" => 50, "SocketHex" => 32, "SocketAtt" => 1, "Category" => 1, "Value" => 800, "Procentage" => 0 ),
"2" => Array ( "SocketID" => 3, "SocketName" => "Name3", "SocketDecimal" => 100, "SocketHex" => 64, "SocketAtt" => 1, "Category" => 1, "Value" => 60 ,"Procentage" => 0 )
);
var_dump($array[array_search(50,array_column($array,'SocketDecimal'))]);

Group array rows by one column and only create a subarray from another column if more than one value

How can I group row data from a two-dimensional array and only create a subarray of another column if the respective group contains more than one value?
In other words, I need to group rows by id and conditionally restructure an array of arrays to have a variable depth of either 2 or 3 levels.
Input:
[
['id' => 567, 'value' => 780],
['id' => 676, 'value' => 743],
['id' => 676, 'value' => 721],
['id' => 234, 'value' => 766],
['id' => 234, 'value' => 680]
]
Desired output:
[
['id' => 567, 'value' => 780],
['id' => 676, 'value' => [743, 721]],
['id' => 234, 'value' => [766, 680]]
]
Are you sure you want to have the value as an integer when there is one value and an array when there are more?
<?php
$array = array(
array('id' => 567, 'value' => 780),
array('id' => 676, 'value' => 743),
array('id' => 676, 'value' => 721),
array('id' => 234, 'value' => 766),
array('id' => 234, 'value' => 680)
);
foreach ($array as $item) {
$result[$item['id']][] = $item['value'];
}
foreach ($result as $id => $value) {
if (count($value) > 1) {
$output[] = array(
'id' => $id,
'value' => $value
);
} else {
$output[] = array(
'id' => $id,
'value' => $value[0]
);
}
}
echo '<pre>';
print_r($output);
echo '</pre>';
?>
If not
<?php
$array = array(
array('id' => 567, 'value' => 780),
array('id' => 676, 'value' => 743),
array('id' => 676, 'value' => 721),
array('id' => 234, 'value' => 766),
array('id' => 234, 'value' => 680)
);
foreach ($array as $item) {
$result[$item['id']][] = $item['value'];
}
foreach ($result as $id => $value) {
$output[] = array(
'id' => $id,
'value' => $value
);
}
echo '<pre>';
print_r($output);
echo '</pre>';
?>
try this one
$array['key1'] = array(
0=>array('id'=>567, 'value'=>780),
1=>array('id'=>676, 'value'=>743),
2=>array('id'=>676, 'value'=>721),
3=>array('id'=>234, 'value'=>766),
4=>array('id'=>234, 'value'=>780)
);
foreach($array['key1'] as $subarray){
$group_id = $subarray['id'];
if(!isset($return[$group_id]))
$return[$group_id] = $subarray;
else{
if(is_array($return[$group_id]['value']))
array_push($return[$group_id]['value'], $subarray['value']);
else
$return[$group_id]['value'] = array($subarray['value'], $return[$group_id]['value']);
}
}
// reset grouping keys into 0,1...
$return = array_values($return);
print_r($return);
There, all the work done for you. How easy is that!
//create the array as you have now
$array[0] = ['id'=>567, 'value'=>780];
$array[1] = ['id'=>676, 'value'=>743];
$array[2] = ['id'=>676, 'value'=>721];
$array[3] = ['id'=>234, 'value'=>766];
$array[4] = ['id'=>234, 'value'=>780];
print_r($array);
print chr(10).chr(10);
//create a new array with the values combined on key
$concat = array();
foreach($array as $val) {
$i = $val['id'];
$v = $val['value'];
if (!is_array($concat[$i]))
$concat[$i] = array();
$concat[$i][] = $v;
}
print_r($concat);
print chr(10).chr(10);
//create a new array to show the data as you want.
$newarray = array();
foreach($concat as $key=>$val) {
$t = array();
$t['id'] = $key;
if (count($val)==1)
$t['value'] = $val[0];
else {
$t['value'] = array();
foreach($val as $v)
$t['value'][] = $v;
}
$newarray[] = $t;
}
print_r($newarray);
print chr(10).chr(10);
Result:
Array
(
[0] => Array
(
[id] => 567
[value] => 780
)
[1] => Array
(
[id] => 676
[value] => 743
)
[2] => Array
(
[id] => 676
[value] => 721
)
[3] => Array
(
[id] => 234
[value] => 766
)
[4] => Array
(
[id] => 234
[value] => 780
)
)
Array
(
[567] => Array
(
[0] => 780
)
[676] => Array
(
[0] => 743
[1] => 721
)
[234] => Array
(
[0] => 766
[1] => 780
)
)
Array
(
[0] => Array
(
[id] => 567
[value] => 780
)
[1] => Array
(
[id] => 676
[value] => Array
(
[0] => 743
[1] => 721
)
)
[2] => Array
(
[id] => 234
[value] => Array
(
[0] => 766
[1] => 780
)
)
)
<?php
$arr['key1'] = array(
array(
'id' => 567,
'value' => 780,
),
array(
'id' => 676,
'value' => 743,
),
array(
'id' => 676,
'value' => 721,
),
array(
'id' => 234,
'value' => 766,
),
array(
'id' => 234,
'value' => 780,
),
);
/* handle element merge */
function array_internal_merge_func($a, $b) {
if ( is_array($a['value']) )
$a['value'][] = $b['value'];
else
$a['value'] = array($a['value'], $b['value']);
return $a;
}
/* handle array merge */
function array_internal_merge($array, $key, $merge_func) {
$hashed = array();
$result = array();
foreach ( $array as $idx => $ele )
$hashed[$ele[$key]][] = $idx;
foreach ( $hashed as $key => $idxies ) {
reset($idxies);
$idx0 = current($idxies);
$result[$idx0] = $array[$idx0];
while ( $idx = next($idxies) )
$result[$idx0] = $merge_func($result[$idx0], $array[$idx]);
}
return $result;
}
print_r(array_internal_merge($arr['key1'], 'id', 'array_internal_merge_func'));
This task can certainly be done concisely with one loop.
Use id column values as temporary first level keys. This makes identifying duplicates most efficient and easy.
While iterating:
if a row's id is new to the result array, then push the whole row (in its original, flat structure) into the result array; or
if a row's id was encountered before, then cast the stored row's value element as an array before pushing the current row's value into that subarray.
If you do not want your result to have id values as the first level keys, then call array_values() to re-index the result.
The special action done implemented below is that when casting a scalar or null data type to an array, the value becomes the lone element of the newly formed array. If an array is explicitly cast as an array, then there is no effect on the data structure at all. This is why (array) can be unconditionally applied in the else branch.
Code: (Demo)
foreach ($array as $row) {
if (!isset($result[$row['id']])) {
$result[$row['id']] = $row;
} else {
$result[$row['id']]['value'] = array_merge(
(array) $result[$row['id']]['value'],
[$row['value']]
);
}
}
var_export(array_values($result));
An alternative without array_merge(): (Demo)
foreach ($array as $row) {
if (!isset($result[$row['id']])) {
$result[$row['id']] = $row;
} else {
$result[$row['id']]['value'] = (array) $result[$row['id']]['value'];
$result[$row['id']]['value'][] = $row['value'];
}
}
var_export(array_values($result));
An alternative with array_reduce(): (Demo)
var_export(
array_values(
array_reduce(
$array,
function($result, $row) {
if (!isset($result[$row['id']])) {
$result[$row['id']] = $row;
} else {
$result[$row['id']]['value'] = (array) $result[$row['id']]['value'];
$result[$row['id']]['value'][] = $row['value'];
}
return $result;
}
)
)
);

Join 2 multidimensional array

$items = array(
array(
'id' => 0,
'name' => 'Simple Sword',
'type' => 'weapon',
'price' => 200,
'value1' => 5,
'value2' => 10,
'value3' => 0,
'value4' => 0,
'value5' => 0
),
array(
'id' => 1,
'name' => 'Iron Sword',
'type' => 'weapon',
'price' => 500,
'value1' => 0,
'value2' => 0,
'value3' => 0,
'value4' => 0,
'value5' => 0
)
);
$inventory = array(
array(
'item' => 0,
'slot' => 1,
'value1' => 0,
'value2' => 0,
'value3' => 0,
'value4' => 0,
'value5' => 0,
'equipped' => 0
),
array(
'item' => 1,
'slot' => 2,
'value1' => 0,
'value2' => 0,
'value3' => 0,
'value4' => 0,
'value5' => 0,
'equipped' => 1
)
);
What I need is to join these 2 multidimensional arrays, or take the values, keys etc from the "Items" array and put it in the Inventory array where the "item" id matches the id from the Items array. Similiar to a INNER JOIN statement in SQL. How? I can't figure it out.
Secondly, I am trying to print out the $inventory array, I tried the following, but It didn't work:
foreach ($inventory as $a) {
foreach ($a as $b) {
echo $b['item'];
}
}
It gives me no output.
a little help with your second problem:
foreach ($inventory as $a => $b) {
echo $b['item'];
}
}
As for first question Zulkhaery Basrul gave good answer. I would also consider putting break statement if relation is one-to-one:
if($val['item'] == $items[$key]['id']) {
$newarr[] = array_merge($items[$key],$val);
break;
}
As for second question:
foreach ($inventory as $invKey => $aInventoryItem) {
echo $aInventoryItem['item'] . "\n";
}
I think you can just do this in order to echo the item from inventory:
foreach($inventory as $inv) {
echo $inv['item'];
}
foreach($inventory as $key=>$val)
{
if($val['item'] == $items[$key]['id'])
{
$newarr[] = array_merge($items[$key],$val);
}
}
check $newarr[] using print_r($newarr), here the output :
Array
(
[0] => Array
(
[id] => 0
[name] => Simple Sword
[type] => weapon
[price] => 200
[value1] => 0
[value2] => 0
[value3] => 0
[value4] => 0
[value5] => 0
[item] => 0
[slot] => 1
[equipped] => 0
)
[1] => Array
(
[id] => 1
[name] => Iron Sword
[type] => weapon
[price] => 500
[value1] => 0
[value2] => 0
[value3] => 0
[value4] => 0
[value5] => 0
[item] => 1
[slot] => 2
[equipped] => 1
)
)
Second question, to print out the $inventory array:
foreach ($inventory as $a)
{
echo $a['item'];
echo $a['slot'];
echo $a['value1'];
//...etc
}
I believe this function is what you're looking for:
// Join Arrays on Keys
function array_join($original, $merge, $on) {
if (!is_array($on)) $on = array($on);
foreach ($merge as $right) {
foreach ($original as $index => $left) {
foreach ($on as $from_key => $to_key) {
if (!isset($original[$index][$from_key])
|| !isset($right[$to_key])
|| $original[$index][$from_key] != $right[$to_key])
continue 2;
}
$original[$index] = array_merge($left, $right);
}
}
return $original;
}
Usage for your scenario:
print_r(array_join($items, $inventory, array('id' => 'item')));
I've asked the community for help with optimizing the LEFT JOIN version of the function here (the only differences are $remove unset and array_merge): How can I optimize my array_join (simulates a LEFT JOIN) function?

Categories