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;
}
Related
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'm looking for a solution to compare multiplay arrays with each of them i want to unset in all arrays if one key is empty. So as a example if [keywords] is empty i want to unset in all arrays [keywords]. Here are my arrays that i print_r.
Array
(
[0] => Array
(
[id] => 1
[pid] => 3
[sorting] => 128
[tstamp] => 1503039725
[title] => test
[alias] => test-3
[author] => 1
[inColumn] => main
[keywords] =>
[showTeaser] =>
[teaserCssID] =>
[teaser] =>
[printable] =>
[customTpl] =>
[protected] =>
[groups] =>
[guests] =>
[cssID] =>
[space] =>
[published] => 1
[start] =>
[stop] =>
)
[1] => Array
(
[id] => 2
[pid] => 3
[sorting] => 256
[tstamp] => 1503045056
[title] => test 2
[alias] => test-2
[author] => 1
[inColumn] => main
[keywords] =>
[showTeaser] =>
[teaserCssID] => a:2:{i:0;s:0:"";i:1;s:0:"";}
[teaser] =>
[printable] =>
[customTpl] =>
[protected] =>
[groups] =>
[guests] =>
[cssID] => a:2:{i:0;s:0:"";i:1;s:0:"";}
[space] => a:2:{i:0;s:0:"";i:1;s:0:"";}
[published] => 1
[start] =>
[stop] =>
)
)
What i have tried so far is
print_r($arrResult);
foreach($arrResult as $Result)
{
foreach ($Result as $arrKey => $arrField)
{
if(!empty($arrField))
{
$arrAllowedField[$arrKey] = $arrKey;
}
}
}
That build a array that contains the key which has values. But the problem is, that it also add empty fields of a other array.
Thanks
// remove empty entries in each array
$ar = array_map('array_filter', $ar);
// find keys having not empty value at least in one array
$temp = array_intersect_key(...$ar);
// save only keys from temp array
foreach($ar as &$item) {
$item = array_intersect_key($item, $temp);
}
demo
It removes the all the empty values from the array
$arrResult = array_map('array_filter', $arrResult);
$arrResult = array_filter( $arrResult );
echo "<pre>";
print_r($arrResult);
Output
Array
(
[0] => Array
(
[id] => 1
[pid] => 3
[sorting] => 128
[tstamp] => 1503039725
[title] => test
[alias] => test-3
[author] => 1
[inColumn] => main
[published] => 1
)
)
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 want to merge two arrays into one array as follows,
Array1:
Array
(
[0] => Array
(
[id] => 3
[sku] => KOG456
[cart_id] => 2
[name] => Young Money
[slug] => young-money
[route_id] => 47
[description] =>
This is test song
[excerpt] =>
[saleprice] => 90.00
[related_products] =>
[images] => {"1c6b0883fc94c5f644497ec488cdf8cb":{"filename":"1c6b0883fc94c5f644497ec488cdf8cb.jpg","alt":"Test","caption":"","primary":true}}
[seo_title] =>
[meta] =>
[enabled] => 1
)
)
Array2:
Array
(
[0] => Array
(
[filename] => Beethovens_Symphony_No._9_(Scherzo).wma
[title] => Young Money
[size] => 599.26
)
)
Expected array result is:
Array
(
[0] => Array
(
[id] => 3
[sku] => KOG456
[cart_id] => 2
[name] => Young Money
[slug] => young-money
[route_id] => 47
[description] =>
This is test song
[excerpt] =>
[saleprice] => 90.00
[related_products] =>
[images] => {"1c6b0883fc94c5f644497ec488cdf8cb":{"filename":"1c6b0883fc94c5f644497ec488cdf8cb.jpg","alt":"Test","caption":"","primary":true}}
[seo_title] =>
[meta] =>
[enabled] => 1
[filename] => Beethovens_Symphony_No._9_(Scherzo).wma
[title] => Young Money
[size] => 599.26
)
)
How to merge these array elements into one array element ?
foreach ($origArray as $key => &$subArray)
$subArray += $arrayToBeAdded[$key];
Where $origArray is your array which is to be merged into and $arrayToBeAdded the array you merge into.
User array_merge_recursive():
$final = array_merge_recursive($array1, $array2);
Try this little known overload of the + operator for arrays:
$result = $array1[0] + $array2[0]
Use function array_merge($array1[0], $array2[0]) . Following is the example for the same
$array1 = array(0=>array('1'=>1,'2'=>2,'3'=>3));
$array2 = array(0=>array('4'=>4,'5'=>5,'6'=>6));
$result[0] = array_merge($array1[0],$array2[0]);
echo '<pre>';
print_r($result);
Since you have unique keys, you could use something as simple as the + operator (union)...
For example:
$arr1 = [1=>'testing',2=>'stack',3=>'overflow'];
$arr2 = [4=>'something',5=>'else',6=>'here'];
$arr3 = $arr1 + $arr2;
print_r($arr3);
Results:
Array ( [1] => testing [2] => stack [3] => overflow [4] => something [5] => else [6] => here )
For this php has multiple functions. You can use $arrays = array_combine($array1, $array2);.
PHP.net - array_combine
Hope it helped!
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