I have an array in php like this :
Array
(
[0] => Array
(
[915] => 1
[1295] => 1
[1090] => 1
[1315] => 0.93759357774
[128] => 0.93759357774
[88] => 0.731522789561
[1297] => 0.731522789561
[1269] => 0.525492880722
[1298] => 0.525492880722
[121] => 0.519133966069
)
[1] => Array
(
[585] => 1
[1145] => 1
[1209] => 1
[375] => 1
[1144] => 1
[913] => 1
[1130] => 0.996351158355
[215] => 0.937096401456
[1296] => 0.879373313559
[30] => 0.866473953643
[780] => 0.866473953643
[1305] => 0.866473953643
[1293] => 0.866473953643
)
)
How do I get the 1st-5th rows of sub-array for each array, like this :
Result :
Array
(
[0] => Array
(
[915] => 1
[1295] => 1
[1090] => 1
[1315] => 0.93759357774
[128] => 0.93759357774
)
[1] => Array
(
[585] => 1
[1145] => 1
[1209] => 1
[375] => 1
[1144] => 1
)
)
$multid_array = array(/* Your Multidimensional array from above*/);
$sliced_array = array(); //setup the array you want with the sliced values.
//loop though each sub array and slice off the first 5 to a new multidimensional array
foreach ($multid_array as $sub_array) {
$sliced_array[] = array_slice($sub_array, 0, 5);
}
The $sliced_array will then contain the output you wanted.
Iterate over the array.
Read the value by reference.
Delete key-values from offset 5 till
the end. You need not collect the return value because we are using the reference to the original array.
.
foreach($mainArray as $key => &$value) {
array_splice($value,5);
}
Working ideone link
You might want to look into the php function array_splice.
http://no.php.net/manual/en/function.array-slice.php
Related
I want to combine the two arrays, which have the same structure.
This my first array :
[rows] => Array (
[0] => Array (
[kdregister] => 10865701
[pagu] => 339.4157454
[real] => 328.633577646
[real2] => 328.633577646
)
)
This is the second array :
[row] => Array (
[0] => Array (
[kdregister] => 10865701
[kegiatan] => name the game
[pagu] => 0
[real] => 0
[real2] => 0
)
)
and i have same value
[kdregister] => 10865701
and I want to have results like this
[row] => Array (
[0] => Array (
[kdregister] => 10865701
[kegiatan] => name the game
[pagu] => 0
[real] => 0
[real2] => 0
[pagu] => 339.4157454
[real] => 328.633577646
[real2] => 328.633577646
)
Can you help me guys? Thanks!!
Try this man, this work for me !
$array_merge = array_merge($array1,$array2);
$array = array();
foreach ($array_merge['row'] as $key1 => $value1) {
foreach ($array_merge['rows'] as $key2 => $value2) {
if ($value1['kdregister'] == $value2['kdregister']) {
$array[$key2] = array_merge($value1,$value2);
}
}
}
The simplest approach would be array_merge_recursive() but this will not create the array you wanted as that would be impossible. It would create a usable array containing 2 occurances of the data in the rows array
// setup your example arrays
$a1['rows'][] = ['kdregister' => 10865701, 'pagu' => 339.4157454, 'real' => 328.633577646, 'real2' => 328.633577646];
$a2['rows'][] = ['kdregister' => 10865701, 'kegiatan'=> 'Name of game', 'pagu' => 0, 'real' => 0, 'real2' => 0];
$a3 = array_merge_recursive($a1,$a2);
print_r($a3);
RESULT
Array
(
[rows] => Array
(
[0] => Array
(
[kdregister] => 10865701
[pagu] => 339.4157454
[real] => 328.633577646
[real2] => 328.633577646
)
[1] => Array
(
[kdregister] => 10865701
[kegiatan] => Name of game
[pagu] => 0
[real] => 0
[real2] => 0
)
)
)
In order to merge two arrays, you need to use array_merge method and to filter out unique characters you need to do something like this:
$ab = array_unique(array_merge($a, $b));
I am working in PHP so I have an array like this, from this array I want filter take user_id to another array like I given below.
Array
(
[0] => Array
(
[user_id] => 66
[distance] => 0
)
[1] => Array
(
[user_id] => 68
[distance] => 0
)
[2] => Array
(
[user_id] => 81
[distance] => 0
)
[3] => Array
(
[user_id] => 65
[distance] => 0.00010218008081861118
)
)
I want an array like this,
$user_id=array(66,68,81,65);
Use array_column()
Returns an array of values representing a single column from the input array.
<?php
$user_array = array(
0 => array('user_id' => 1, 'name' => 'Bob'),
1 => array('user_id' => 2, 'name' => 'John'),
2 => array('user_id' => 3, 'name' => 'Mary')
);
$users = array_column($user_array, 'user_id');
print_r($users);
Output :
Array
(
[0] => 1
[1] => 2
[2] => 3
)
Where $array is the multidimensional array you provided above:
$data = array();
foreach ($array as $item) {
$data[] = $item['user_id'];
}
print_r($data);
I have two arrays like the following:
Array1
Array ( [price] => 117.00 [recurring_profile] => [use_config_gift_message_available] => 1 [stock_data] => Array ( [use_config_manage_stock] => 1 [original_inventory_qty] => 100 [qty] => 100 [use_config_min_qty] => 1 [use_config_min_sale_qty] => 1 [use_config_max_sale_qty] => 1 [is_qty_decimal] => 0 [is_decimal_divided] => 0 [use_config_backorders] => 1 [use_config_notify_stock_qty] => 1 [use_config_enable_qty_increments] => 1 [use_config_qty_increments] => 1 [is_in_stock] => 1 ) [website_ids] => Array ( [0] => 1 ) [can_save_configurable_attributes] => [can_save_custom_options] => [can_save_bundle_selections] => [type_has_options] => [type_has_required_options] => )
Array2
Array ( [price] => 118.0000 )
I use
$newarr =array_intersect_assoc($oldValues, $newValues);
but $newarr will be blank, any ideas?
My expected results:
$newArray1 = Array ( [price] => 117.00 );
It's blank, because price has different values 117.00 and 118.0000
You can try array_intersect_key
array_intersect_assoc() will only return items where BOTH the key and value match.
I am working on a php project. I am stuck at this point.
Here is the array that I have.
[text_numeric] => Array
(
[text] => Numeric field fillable by user
[parameters] => Array
(
[prefix] => 1
[price] => 1
[sku] =>
[quantity] =>
[weight] =>
[min_value] => 1
[max_value] => 1
)
[operand] => Array
(
[op_fix_discount] => 1
[op_fix_recharge] => 1
[op_per_unit] => 1
[op_percentage] =>
)
)
[checkbox] => Array
(
[text] => Checkbox attributes
[parameters] => Array
(
[prefix] => 1
[price] => 1
[sku] => 1
[quantity] => 1
[weight] => 1
[min_value] =>
[max_value] =>
)
[operand] => Array
(
[op_fix_discount] => 1
[op_fix_recharge] => 1
[op_per_unit] =>
[op_percentage] => 1
)
)
I want to get all the values from this array with key value "text" in another array.
Like this:
Array
(
[0]=>Numeric field fillable by user
[1]=>Checkbox attributes
)
It could have been great if you had show us your efforts to achieve that but since you are new here is the codez. You can simply get it using a foreach loop,
$new_array = array();
foreach($your_array as $k=>$row){
$new_array[$k] = $row['text'];
}
print_r($new_array);
You can also use array_map,
function getTextField($a) {
return $a['text'];
}
$texts = array_map('getTextField', $your_array);
print_r($texts);
I am working in PHP with array iteration.
I have a multidimensional array like this:
Array
(
[1] => stdClass Object
(
[id] => 1
[comments] => Testing the Data
[stream_context_id] => 5
[stream_entity_id] =>
[class_id] => 1
[parent_id] => 0
[learnt_count] =>
[rating_count] =>
[abused_count] =>
[created_by] => 1
[created_datetime] =>
[stream_context] => comments
[name] =>
[upload_path] =>
[uploadby] =>
[upload_time] =>
)
[2] => stdClass Object
(
[id] => 2
[comments] => Testing the Data
[stream_context_id] => 5
[stream_entity_id] =>
[class_id] => 1
[parent_id] => 0
[learnt_count] =>
[rating_count] =>
[abused_count] =>
[created_by] => 1
[created_datetime] =>
[stream_context] => comments
[name] =>
[upload_path] =>
[uploadby] =>
[upload_time] =>
)
)
Here the first index values i.e. 1 and 2 are the ids mentioned in their corresponding arrays.
I want the same multidimensional array with index values a 0 and 1 and so on.. i.e. the usual format of an array.
Don't know if this is what you meant, but maybe...:
$reindexedArray = array_values($yourArray);
If you also want to convert the stdClass objects to arrays, try:
$reindexedAndArrayifiedArray = array_values(array_map(function ($entry) {
return (array)$entry;
}, $yourArray));
Using array_merge() with a blank array - will renumber numeric indexes:
$result = array_merge(Array(), $your_array_here) ;
This does look like a multidimentional array as you have a named array holding objects.
Your array is currently:
$a = array('1'=>Object, '2'=>Object);
Instead of:
$a = array('1'=>array('id'=>'1', 'comment'=>'some comment'), '2'=>array());
$b = array();
foreach($a as $key=>$val){
$b[] = $val;
}