I am trying to set the values of an empty array with another array (based on rows returned from a database).
I have tried a few things like array_merge but I just end up adding to the first array.
Just curious if there is a way to do this or would I need to iterate thought each array and merge them?
The second array can have between 1 and 3 arrays in it, while the first (the "empty") array has 3 elements always.
Empty array
(
[0] => Array
(
[id] =>
[quote_id] =>
...
)
[1] => Array
(
[id] =>
[quote_id] =>
...
)
[2] => Array
(
[id] =>
[quote_id] =>
...
)
)
Array I want to copy data from
Array
(
[0] => Array
(
[id] => 1
[quote_id] => 1
...
)
[1] => Array
(
[id] => 2
[quote_id] => 1
...
)
)
What I want to achieve
Array
(
[0] => Array
(
[id] => 1
[quote_id] => 1
...
)
[1] => Array
(
[id] => 2
[quote_id] => 1
...
)
[2] => Array
(
[id] =>
[quote_id] =>
...
)
)
You can make use of the array union operatorDocs:
$combined = $rows + $empty;
The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.
You could use array_replace(), but you'll have to be very careful with the keys, see the example in the PHP manual.
You can try
$empty = array(
"0" => Array("id" => null,"quote_id" => null,"position" => null,"type" => null,"number" => null,"cost" => null,"total" => null),
"1" => Array("id" => null,"quote_id" => null,"position" => null,"type" => null,"number" => null,"cost" => null,"total" => null),
"2" => Array("id" => null,"quote_id" => null,"position" => null,"type" => null,"number" => null,"cost" => null,"total" => null))
;
$content = Array(
"0" => Array("id" => 1,"quote_id" => 1,"position" => 1,"type" => "dwdwdw","number" => 22,"cost" => 33.00,"total" => 726.00),
"1" => Array("id" => 2,"quote_id" => 1,"position" => 2,"type" => "dwdw","number" => 22,"cost" => 22.00,"total" => 484.00));
var_dump(array_merge($content,array_unique($empty)));
Output
array
0 =>
array
'id' => int 1
'quote_id' => int 1
'position' => int 1
'type' => string 'dwdwdw' (length=6)
'number' => int 22
'cost' => float 33
'total' => float 726
1 =>
array
'id' => int 2
'quote_id' => int 1
'position' => int 2
'type' => string 'dwdw' (length=4)
'number' => int 22
'cost' => float 22
'total' => float 484
2 =>
array
'id' => null
'quote_id' => null
'position' => null
'type' => null
'number' => null
'cost' => null
'total' => null
Try this,
<?php
$result=mysql_query($query);
$i=0;
while($row=mysql_fetch_assoc($result)){
$youArray[$i]['id']=$row['id'];
$youArray[$i]['quote_id']=$row['quote_id'];
//remaining values
$i++;
}
?>
Related
I have an array with the following structure:
Array
(
[DigitalAssets] => Array
(
[0] => Array
(
[PartNumber] => 0276S-4
[Link] => https://1ddf4b1b856a39e33863-d785dc0e3b62b5e0ef07f55db00b0659.ssl.cf2.rackcdn.com/Holley/0576s-4.jpg
[AssetTypeCode] => P04
[FileName] => 0576s-4.jpg
[RecordModifiedDate] => 2020-05-13T18:59:10.28
)
[1] => Array
(
[PartNumber] => 0437S-4
[Link] => https://1ddf4b1b856a39e33863-d785dc0e3b62b5e0ef07f55db00b0659.ssl.cf2.rackcdn.com/Holley/0437s-4.jpg
[AssetTypeCode] => P04
[FileName] => 0437s-4.jpg
[RecordModifiedDate] => 2020-05-13T18:59:11.687
)
[2] => Array
(
[PartNumber] => 0574S-4
[Link] => https://1ddf4b1b856a39e33863-d785dc0e3b62b5e0ef07f55db00b0659.ssl.cf2.rackcdn.com/Holley/0574s-4.jpg
[AssetTypeCode] => P04
[FileName] => 0574s-4.jpg
[RecordModifiedDate] => 2020-05-13T18:59:12.593
)
I want to count the total Indexes of array so that I will run loop accordingly. I used Count($array) and Count ($array,RECURSIVE) but it cannot return the correct total number of indexes.
Can only guide that how to do this?
Thanks
If You want to count DigitalAssets direct children You can do this
count($array['DigitalAssets'])
Here are some tests how count should work.
Code is:
$test = array
(
'DigitalAssets' => array
(
0 => array(
'PartNumber' => '0276S-4',
'Link' => 'https://1ddf4b1b856a39e33863-d785dc0e3b62b5e0ef07f55db00b0659.ssl.cf2.rackcdn.com/Holley/0576s-4.jpg',
'AssetTypeCode' => 'P04',
'FileName' => '0576s-4.jpg',
'RecordModifiedDate' => '2020-05-13T18:59:10.28'
),
1 => array(
'PartNumber' => '0437S-4',
'Link' => 'https://1ddf4b1b856a39e33863-d785dc0e3b62b5e0ef07f55db00b0659.ssl.cf2.rackcdn.com/Holley/0437s-4.jpg',
'AssetTypeCode' => 'P04',
'FileName' => '0437s-4.jpg',
'RecordModifiedDate' => '2020-05-13T18:59:11.687'
),
2 => array
(
'PartNumber' => '0574S-4',
'Link' => 'https://1ddf4b1b856a39e33863-d785dc0e3b62b5e0ef07f55db00b0659.ssl.cf2.rackcdn.com/Holley/0574s-4.jpg',
'AssetTypeCode' => 'P04',
'FileName' => '0574s-4.jpg',
'RecordModifiedDate' => '2020-05-13T18:59:12.593'
)
)
);
echo count($test['DigitalAssets'])." ".count($test['DigitalAssets'], 0)." ".count($test['DigitalAssets'], 1);
exit();
Result in my case:
3 3 18
This means taht in my case mode in count is set to 0 by default so i will get only first level counted. If i set mode to 1 i will get all nested items counted as well. This should clear things up for You.
I have tried using this example here Return index of highest value in an array
But that does not go into a multi dimensional Array
I have tried ARRAY_COLUMN(), array_values(), array_shift, Max and Min but they are not getting the info i need
I want to be able to loop thru and get to:
[pricing]
then Check pricing to see which is the highest and lowest in the nested array
so like below which total was the Highest and lowest trades on these dates
[2017-09-22]
[2017-09-23]
obviously [2017-09-23] is a simple one, i just dint want to add much more code for ppl helping to go thru.
The Array i create looks like this:
Array
(
[2017-09-23] => Array
(
[0] => Array
(
[timestamp] => 1506169387000
[pricing] => 9.5470
[qty] => 25
[total] => 238.675
[date] => 2017-09-23
)
)
[2017-09-22] => Array
(
[0] => Array
(
[timestamp] => 1506093083000
[pricing] => 9.6300
[qty] => 25
[total] => 240.75
[date] => 2017-09-22
)
[1] => Array
(
[timestamp] => 1506077220000
[pricing] => 8.7190
[qty] => 13
[total] => 113.347
[date] => 2017-09-22
)
[2] => Array
(
[timestamp] => 1506077109000
[pricing] => 8.6800
[qty] => 83
[total] => 720.44
[date] => 2017-09-22
)
[3] => Array
(
[timestamp] => 1506065258000
[pricing] => 8.7100
[qty] => 25
[total] => 217.75
[date] => 2017-09-22
)
)
in the example above i would like it to Create a new array with only the following
date -> last timestamp -> Highest pricing -> Lowest lowest -> Total of all Totals -> first Timestamp
EDIT: the last and first Timestamp are basically the first and last index so in this case:
[timestamp] => 1506093083000 and [timestamp] => 1506065258000
or Index [0] and index [3]
The array from your question:
$array = array (
'2017-09-23' =>
array (
0 =>
array (
'timestamp' => '1506169387000',
'pricing' => '9.5470',
'qty' => '25',
'total' => '238.675',
'date' => '2017-09-23',
),
),
'2017-09-22' =>
array (
0 =>
array (
'timestamp' => '1506093083000',
'pricing' => '9.6300',
'qty' => '25',
'total' => '240.75',
'date' => '2017-09-22',
),
1 =>
array (
'timestamp' => '1506077220000',
'pricing' => '8.7190',
'qty' => '13',
'total' => '113.347',
'date' => '2017-09-22',
),
2 =>
array (
'timestamp' => '1506077109000',
'pricing' => '8.6800',
'qty' => '83',
'total' => '720.44',
'date' => '2017-09-22',
),
3 =>
array (
'timestamp' => '1506065258000',
'pricing' => '8.7100',
'qty' => '25',
'total' => '217.75',
'date' => '2017-09-22',
),
),
);
To get the values maybe you can use array_map and with this the function array_column is the key of all, try with this:
$values = array_map(function($dates) {
$timestamps = array_column($dates, 'timestamp');
$pricings = array_column($dates, 'pricing');
return [
'max_pricing' => max($pricings),
'lowest_pricing' => min($pricings),
'total_of_totals' => array_sum(array_column($dates, 'total')),
'first_timestamp' => reset($timestamps),
'last_timestamp' => end($timestamps),
];
}, $array);
$values is an array with the filter values that you need:
Array
(
[2017-09-23] => Array
(
[max_pricing] => 9.5470
[lowest_pricing] => 9.5470
[total_of_totals] => 238.675
[first_timestamp] => 1506169387000
[last_timestamp] => 1506169387000
)
[2017-09-22] => Array
(
[max_pricing] => 9.6300
[lowest_pricing] => 8.6800
[total_of_totals] => 1292.287
[first_timestamp] => 1506093083000
[last_timestamp] => 1506065258000
)
)
EDIT
Get the pricing of the [first_timestamp] and [last_timestamp]
$values = array_map(function($dates) {
$timestamps = array_column($dates, 'timestamp');
$pricings = array_column($dates, 'pricing');
return [
'max_pricing' => max($pricings),
'lowest_pricing' => min($pricings),
'total_of_totals' => array_sum(array_column($dates, 'total')),
'first_timestamp' => [
'value' => reset($timestamps),
'pricing' => $pricings[each($timestamps)['key']]
],
'last_timestamp' => [
'value' => end($timestamps),
'pricing' => $pricings[each($timestamps)['key']]
]
];
}, $array);
I added an array to both timestamps with the value of these and the pricing.
Array
(
[2017-09-23] => Array
(
[max_pricing] => 9.5470
[lowest_pricing] => 9.5470
[total_of_totals] => 238.675
[first_timestamp] => Array
(
[value] => 1506169387000
[pricing] => 9.5470
)
[last_timestamp] => Array
(
[value] => 1506169387000
[pricing] => 9.5470
)
)
[2017-09-22] => Array
(
[max_pricing] => 9.6300
[lowest_pricing] => 8.6800
[total_of_totals] => 1292.287
[first_timestamp] => Array
(
[value] => 1506093083000
[pricing] => 9.6300
)
[last_timestamp] => Array
(
[value] => 1506065258000
[pricing] => 8.7100
)
)
)
I have one array. I want to find unique value of fk_section_id and other fields(section_name & fk_section_id) associated with it. I also want to sort this array by fk_section_id. How to get this array from the original one?
Array
Array
(
[0] => Array
(
[fk_field_id] => 1
[fk_section_id] => 1
[section_order] => 1
[field_order] => 1
[field_name] => Title
[section_name] => Your Detail
[fk_field_type_id] => 4
)
[1] => Array
(
[fk_field_id] => 2
[fk_section_id] => 1
[section_order] => 1
[field_order] => 2
[field_name] => Name
[section_name] => Your Detail
[fk_field_type_id] => 1
)
[2] => Array
(
[fk_field_id] => 3
[fk_section_id] => 2
[section_order] => 2
[field_order] => 1
[field_name] => Road
[section_name] => Address For Correspondence
[fk_field_type_id] => 1
)
)
Expected Output
Array
(
[0] => Array
(
[fk_section_id] => 1
[section_order] => 1
[section_name] => Your Detail
)
[1] => Array
(
[fk_section_id] => 2
[section_order] => 2
[section_name] => Address For Correspondence
)
)
I tried below
$i=0;
foreach($formFields as $val){
$i++;
$allSections[$i]['section_name'] = $val['section_name'];
$allSections[$i]['fk_section_id'] = $val['fk_section_id'];
$allSections[$i]['section_order'] = $val['section_order'];
}
And it gives
Array
(
[0] => Array
(
[section_name] => Your Detail
[fk_section_id] => 1
[section_order] => 1
)
[1] => Array
(
[section_name] => Your Detail
[fk_section_id] => 1
[section_order] => 1
)
[2] => Array
(
[section_name] => Address For Correspondence
[fk_section_id] => 2
[section_order] => 2
)
)
You can try this:
$data = array(
array(
'fk_field_id' => 1,
'fk_section_id' => 1,
'section_order' => 1,
'field_order' => 1,
'field_name' => 'Title',
'section_name' => 'Your Detail',
'fk_field_type_id' => 4,
),
array(
'fk_field_id' => 2,
'fk_section_id' => 1,
'section_order' => 1,
'field_order' => 2,
'field_name' => 'Name',
'section_name' => 'Your Detail',
'fk_field_type_id' => 1,
),
);
$temp = array();
foreach($data as $key => $value) { //Make an new array using fk_field_id as key
if (!in_array($value['fk_field_id'], $temp)) {
$temp[$value['fk_field_id']] = $value;
}
}
ksort($temp); //Sort the array by key
var_dump($temp);
Result:
array (size=2)
1 =>
array (size=7)
'fk_field_id' => int 1
'fk_section_id' => int 1
'section_order' => int 1
'field_order' => int 1
'field_name' => string 'Title' (length=5)
'section_name' => string 'Your Detail' (length=11)
'fk_field_type_id' => int 4
2 =>
array (size=7)
'fk_field_id' => int 2
'fk_section_id' => int 1
'section_order' => int 1
'field_order' => int 2
'field_name' => string 'Name' (length=4)
'section_name' => string 'Your Detail' (length=11)
'fk_field_type_id' => int 1
Also now you can use:
echo $temp[1]['field_name'];
Result:
Title
I have 2 multidimensional arrays that I am working with:
$arr1 =
Array
([type] => characters
[version] => 5.6.7.8
[data] => Array
([Char1] => Array
([id] => 1
[name] =>Char1
[title] =>Example
[tags] => Array
([0] => DPS
[1] => Support))
[Char2] => Array
([id] => 2
[name] =>Char2
[title] =>Example
[tags] => Array
([0] => Tank
[1] => N/A)
)
)
etc...
$arr2=
Array
([games] => Array
([gameId] => 123
[gameType => Match
[char_id] => 1
[stats] => Array
([damage] => 55555
[kills] => 5)
)
([gameId] => 157
[gameType => Match
[char_id] => 2
[stats] => Array
([damage] => 12642
[kills] => 9)
)
etc...
Basically, I need almost all the data in $arr2... but only the Char name from $arr1. How could I merge or add the $arr1['name'] key=>value into $arr2 where $arr1['id'] is equal to $arr2['char_id'] as the "id" field of each array is the same number.
I've attempted using array_merge and array_replace, but I haven't come up with any working solutions. This is also all data that I am receiving from a 3rd party, so I have no control on initial array setup.
Thanks for any help or suggestions!
Actually, this is quite straighforward. (I don't think there a built-in function that does this.)
Loop $arr2 and under it loop also $arr1. While under loop, just add a condition that if both ID's match, add that particular name to $arr2. (And use some referencing & on $arr2)
Consider this example:
// your data
$arr1 = array(
'type' => 'characters',
'version' => '5.6.7.8',
'data' => array(
'Char1' => array(
'id' => 1,
'name' => 'Char1',
'title' => 'Example',
'tags' => array('DPS', 'Support'),
),
'Char2' => array(
'id' => 2,
'name' => 'Char2',
'title' => 'Example',
'tags' => array('Tank', 'N/A'),
),
),
);
$arr2 = array(
'games' => array(
array(
'gameId' => 123,
'gameType' => 'Match',
'char_id' => 1,
'stats' => array('damage' => 55555, 'kills' => 5),
),
array(
'gameId' => 157,
'gameType' => 'Match',
'char_id' => 2,
'stats' => array('damage' => 12642, 'kills' => 9),
),
),
);
foreach($arr2['games'] as &$value) {
$arr2_char_id = $value['char_id'];
// loop and check against the $arr1
foreach($arr1['data'] as $element) {
if($arr2_char_id == $element['id']) {
$value['name'] = $element['name'];
}
}
}
echo '<pre>';
print_r($arr2);
$arr2 should look now like this:
Array
(
[games] => Array
(
[0] => Array
(
[gameId] => 123
[gameType] => Match
[char_id] => 1
[stats] => Array
(
[damage] => 55555
[kills] => 5
)
[name] => Char1 // <-- name
)
[1] => Array
(
[gameId] => 157
[gameType] => Match
[char_id] => 2
[stats] => Array
(
[damage] => 12642
[kills] => 9
)
[name] => Char2 // <-- name
)
)
)
Iterate over $arr2 and add the data to it from the matching $arr1 array value:
$i = 0;
foreach($arr2['games'] as $arr2Game){
$id = $arr2Game['char_id'];
$arr2['games'][$i]['name'] = $arr1['data'][$id]['name'];
$i++;
}
Have not tested this code.
If I'm understanding you correctly, you want to add a name index to each of the arrays within the $arr2['games'] array.
foreach($arr2['games'] as $key => $innerArray)
{
$arr2['games'][$key]['name'] = $arr1['data']['Char'.$innerArray['char_id']]['name'];
}
I am trying to filter two arrays to get a final result with user ids from my mysql database
I have two arrays the first one:
print_r($arr_partner_id);
Array (
[0] => Array ( [id] => 335 [id_partner] => 0 )
[1] => Array ( [id] => 469 [id_partner] => 1 )
[2] => Array ( [id] => 457 [id_partner] => 1 )
[3] => Array ( [id] => 339 [id_partner] => 0 )
[4] => Array ( [id] => 361 [id_partner] => 0 ) )
and the second one:
print_r($arr_member_id);
Array (
[0] => 457
[1] => 469
[2] => 339
[3] => 361 )
now i want compare these two only with their ids and delete the ids that are not included in the "$arr_member_id" Array. This my "reference Array" that means i only need the ids (457,469,339,361)
for the final result it should be looking like this:
print_r($arr_partner_final_id);
Array (
[0] => Array ( [id] => 469 [id_partner] => 1 )
[1] => Array ( [id] => 457 [id_partner] => 1 )
[2] => Array ( [id] => 339 [id_partner] => 0 )
[3] => Array ( [id] => 361 [id_partner] => 0 ) )
i tryed it with foreach
foreach ($arr_partner_id as $key => $usr_ids) {
if($arr_partner_id[$key]['id'] == $arr_member_id[$key]) {
// do something
}
}
but the "keys" are different this should not working...
making it as simple, and using just one loop to loop through the array and checkin if the id is present in another set of array using in_array()
try this
for($i=0;$i<count($arr_partner_id);$i++){
if(!in_array($arr_partner_id[$i]['id'],$arr_member_id)){
unset($arr_partner_id[$i]);
}
}
print_r($arr_partner_id);
try it here
AND yes!! if you want seperate arrays for that then simply modify the code..create new array and push the elements that is present in array
$finalArray=array();
for($i=0;$i<count($arr_partner_id);$i++){
if(in_array($arr_partner_id[$i]['id'],$arr_member_id)){
$finalArray[]=$arr_partner_id[$i];
}
}
print_r($finalArray);
Try this (Working example : http://codepad.org/ApFcA3Zo)
<?php
$arr_partner_id=array (
'0' => array ( 'id' => 335, 'id_partner' => 0 ) ,
'1' => array ( 'id' => 469, 'id_partner' => 1 ) ,
'2' => array ( 'id' => 457, 'id_partner' => 1 ) ,
'3' => array ( 'id' => 339, 'id_partner' => 0 ) ,
'4' => array ( 'id' => 361, 'id_partner' => 0 ) ) ;
$arr_member_id=array (
'0' => 457 ,
'1' => 469 ,
'2' => 339 ,
'3' => 361 ) ;
$final =array();
foreach($arr_partner_id as $arr)
{
foreach($arr_member_id as $parr)
{
if($arr['id'] == $parr)
{
$final[]=$arr;
}
}
}
print_r($final);
?>
Maybe something like:
foreach ($arr_member_id as $usr_id){
foreach ($arr_partner_id as $partner){
if ($usr_id == $partner['id']) {$arr_partner_final_id[]=$partner;break;
}
}
Another solution (without explicit looping):
$arr_partner_id=array (
'0' => array( 'id' => 335, 'id_partner' => 0 ),
'1' => array( 'id' => 469, 'id_partner' => 1 ),
'2' => array( 'id' => 457, 'id_partner' => 1 ),
'3' => array( 'id' => 339, 'id_partner' => 0 ),
'4' => array( 'id' => 361, 'id_partner' => 0 ));
$arr_member_id=array (
'0' => 457,
'1' => 469,
'2' => 339,
'3' => 361);
function compare($v){global $arr_member_id;return in_array($v['id'], $arr_member_id);}
var_dump($arr_partner_id = array_filter($arr_partner_id, 'compare'));
better you use mysql itself do this task. but if you need to continue with this use in array function to check the second array as given below,
foreach ($arr_partner_id as $key => $usr_ids) {
if(in_array($usr_ids["id"], $arr_member_id)) {
// do something
}
}