Following is the output of my multidimensional array $csmap_data
Array
(
[0] => Array
(
[cs_map_id] => 84
[cs_subject_id] => 1
)
[1] => Array
(
[cs_map_id] => 85
[cs_subject_id] => 5
)
[flag] => 1
)
Initially there was no [flag] => 1 key-value in the array, I added it to the array $csmap_data.
But I want to add the [flag] => 1 in the above two array elements, not as a separate array element. In short I wanted following output :
Array
(
[0] => Array
(
[cs_map_id] => 84
[cs_subject_id] => 1
[flag] => 1
)
[1] => Array
(
[cs_map_id] => 85
[cs_subject_id] => 5
[flag] => 1
)
)
The code I was trying to achieve this is as follows, but couldn't get the desired output:
if (!empty($csmap_data)) {
foreach($csmap_data as $csm) {
$chapter_csmap_details = $objClassSubjects->IsClassSubjectHasChapters($csm['cs_map_id']);
$csmap_data ['flag'] = 1;
}
}
Can anyone help me out in obtaining the desired output as I depicted? Thanks in advance.
<?
foreach($csmap_data as $key => $csm)
{
$csmap_data[$key]['flag'] = 1;
}
That should do the trick.
You can also do it using php array functions
$csmap_data = array_map(function($arr){
return $arr + ['flag' => 1];
}, $csmap_data);
UPDATE:
to use multiple variables in callback function of array_map function we can do it by use
$flagValue = 1;
$csmap_data = array_map(function($arr) use ($flagValue){
return $arr + ['flag' => $flagValue];
}, $csmap_data);
Related
I have following multi dimensional array, I'm getting this from an API via curl request.
Array
(
[0] => Array
(
[id] => 1
[name] => David Warner
[type] => batter
[country] => Aus
[age] => 33
[runs] => 11100
[wickets] => 12
[catches] => 16
[format] => Array
(
[0] => Array
(
[Domestic] => Array
(
[0] => Array
(
[ODI] => 73
[Tests] => 34
[T20] => 90
)
)
)
)
)
[1] => Array
(
[id] => 2
[name] => Mark Wood
[type] => bowler
[country] => Eng
[age] => 34
[runs] => 200
[wickets] => 120
[catches] => 2
[format] => Array
(
[0] => Array
(
[Domestic] => Array
(
[0] => Array
(
[ODI] => 40
[Tests] => 49
[T20] => 12
)
)
)
)
)
)
I'm trying to create a new array which includes only the "[T20]" values.
My desired out put should look similar to the following array
array:2 [▼
0 => "90"
1 => "12"
]
So far, I've tried following methods...
$newArr_t20 = array_column($result_3['cricketers']['player']['format']['Domestic'], "T20");
Since I'm using laravel then I tried following as well,
use \Illuminate\Support\Arr;
$newArr_t20 = Arr::pluck($result_3, 'cricketers.player.format.Domestic.T20');
But nothing is working for me...
You can try this:
$array = 'Your array...';
$t20_values = [];
foreach ($array as $value) {
foreach ($value['format'] as $format) {
foreach ($format['Domestic'] as $domestic) {
if (isset($domestic['T20'])) {
$t20_values[] = $domestic['T20'];
}
}
}
}
dd($t20_values);
Using Laravel the collection helper Collection::flatten() should be the right way;
$data = collect($apiData)->flatten(4)->pluck('T20')->all();
Explanation:
Transform your array into a collection to be able to use the Laravel collection's provided helpers:
collect($apiData)
Transform your multidimensional array into a single dimensional one, which is nothing more than the array at the n-th depth (4 here in this case):
collect($apiData)->flatten(4)
Map this array into an array having only the values of the T20 key
collect($apiData)->flatten(4)->pluck('T20')
Get the array representation of the collection
collect($apiData)->flatten(4)->pluck('T20')->all()
This question already has answers here:
How to remove duplicate values from a multi-dimensional array in PHP
(18 answers)
Closed 2 years ago.
I searched for solutions on here but didn't find one for my use case.
I have a big array which is built like this example:
Array
(
[0] => Array
(
[Template] => page.html5
)
[1] => Array
(
[Template] => page2.html5
)
[2] => Array
(
[Template] => page.html5
)
[3] => Array
(
[Template] => page2.html5
)
[4] => Array
(
[Template] => page.html5
)
[5] => Array
(
[Template] => page2.html5
)
[6] => Array
(
[id] => 27
[table] => tl_custom
[type] => text
[data] => Array
(
[fragment] => example
[previewId] => 1
[isActive] => 1
)
)
)
I would like to remove all duplicate values for the array key "Template", but besides that I want the array to stay the way it is.
So afterwards my Array should look like:
Array
(
[0] => Array
(
[Template] => page.html5
)
[1] => Array
(
[Template] => page2.html5
)
[6] => Array
(
[id] => 27
[table] => tl_custom
[type] => text
[data] => Array
(
[fragment] => example
[previewId] => 1
[isActive] => 1
)
)
)
Is there a way to achieve this without using lots of memory?
Thanks for your answers :)
You could use the following logic, which uses:
array_map() to flatten the array with index keys-values, and serialize() (stringify) the last array element so we can use
array_unique() on the result.
Then, to restore the stringified array, i.e. turn it back into an array, we use unserialize().
<?php
$newArr = array_unique(array_map(function ($el) {
return $el['Template'] ?? serialize($el);
}, $arr));
// restore the last element to array
$last = array_key_last($newArr); // (PHP 7 >= 7.3.0)*
$newArr[$last] = unserialize($newArr[$last]);
*if PHP version <7.3.0 use: end($newArr); $last = key($newArr);
Output:
Array
(
[0] => page.html5
[1] => page2.html5
[6] => Array
(
[id] => 27
[table] => tl_custom
[type] => text
[data] => Array
(
[fragment] => example
[previewId] => 1
[isActive] => 1
)
)
)
working demo
The code below loops the array, marks indexes for removal and then another loop does the removals:
$templates = array(); //This will store the remove plan
for ($index = 0; $index < count($input); $index++) {
if (isset($input[$index]["Template"])) { //Ignore items where there is no template
if (isset($templates[$input[$index]["Template"]])) { //Let's check whether we have already seen this template
$templates[$input[$index]["Template"]] = array(); //From now on we will find duplicates for this dude
} else { //Mark for removal
$templates[$input[$index]["Template"]][]=$index;
}
}
}
//Actual removals
foreach($templates => $index) {
//Removing the actual element:
unset($input[$index]["Template"]);
//Remove the parent as well if it becomes empty
if (!count($input[$index])) unset($input[$index]);
}
The memory need for this algorithm is:
average(element_size) * number_of_elements
this is my array
Array
(
[0] => Array
(
[id] => 277558
[text_value] => Jif
[response_count] => 13
[response_percentage] => 92
)
[1] => Array
(
[id] => 277559
[text_value] => Peter Pan
[response_count] => 20
[response_percentage] => 6
)
)
after completing the operation the out put should be
Array
(
[0] => Array
(
[id] => 277558
[text_value] => Jif
[response_count] => 13
[response_percentage] => 92
[encode_param]=>ds!##^(*!ggsfh8236542jsdgf82*&61327
)
[1] => Array
(
[id] => 277559
[text_value] => Peter Pan
[response_count] => 20
[response_percentage] => 6
[encode_param]=>ds!##^(*!ggsfh8236542jsdgf82*&61327
)
)
you can see a new array value encode_paramis added
in that function do some encode algorithms
i have achieve this in the foreach looping statement
but i need to do it in array maping
Can anybody help thank u in advance
$encode_func = function($elem) { // declare function to encode
return $elem['text_value'];
}
$result = array_map(function($elem) use($encode_func) {
$elem['encode_param'] = $encode_func($elem);
return $elem;
}, $array);
Hope it helps.
I have used the following code to get array and merge them.
On the print_r(arr1) I get following array. In $arr I am trying to merge array but when i print_($arr) at the end of for-each I get same array.
Am I doing wrong array merge?
How can i combine or merge it?
foreach($q1->result_array() as $row4)
{
$arr1 = $q1->result_array();
echo"<pre>";
print_r($arr1);
echo"</pre>";
$arr = array_merge($arr, $arr1);
echo "<br/>";
$id = $row4['id'];
$parent_id = $row4['parent_id'];
if(!empty($arr1))
{
$this->showreply($id);
}
}
print_r($arr);
Array which i get on print_r($arr1):
Array
(
[0] => Array
(
[id] => 69
[reply] => First reply to Reply
[parent_id] => 68
[postid] => 0
[us_id] => 41
[added_by] => Shailesh
[photo] => 9.jpg
[added_on] => 2013-04-01 16:06:13
)
)
Array
(
[0] => Array
(
[id] => 70
[reply] => Reply to Nested Reply
[parent_id] => 69
[postid] => 0
[us_id] => 41
[added_by] => Shailesh
[photo] => 9.jpg
[added_on] => 2013-04-01 16:07:24
)
)
Array
(
[0] => Array
(
[id] => 52
[reply] => Reply on demand
[parent_id] => 70
[postid] => 0
[us_id] => 50
[added_by] => swapnil
[photo] =>
[added_on] => 2013-03-29 16:27:57
)
)
$arr = array_merge($arr, $arr1);
This $arr variable was never initialized, so nothing plus $arr1 equals to $arr1.
Also why is this code inside a foreach?
try this...i think this is what you are looking for
foreach($q1->result_array() as $row4)
{
$arr1 = $q1->result_array();
$arr[]=$row4;
}
print_r($arr)
I need to split single array in to multiple arrays.
For example:
Array
(
[0] => Array
(
[pageviews] => 26
[visits] => 20
)
[1] => Array
(
[pageviews] => 9
[visits] => 4
)
[2] => Array
(
[pageviews] => 18
[visits] => 9
)
)
I need to split the array like below:
Array
(
[ga:pageviews] => 26
[ga:visits] => 20
)
Array
(
[ga:pageviews] => 9
[ga:visits] => 4
)
Array
(
[ga:pageviews] => 18
[ga:visits] => 9
)
How can i do this?
Any help will be thankful and grateful...
Thanks in advance..
Ok, so using foreach:
foreach ( $original as $item ) {
var_dump( array(
'ga:pageviews' => $item['pageviews'],
'ga:visits' => $item['visits'],
) );
}
try
$array = Array
(
[0] => Array
(
[pageviews] => 26
[visits] => 20
)
[1] => Array
(
[pageviews] => 9
[visits] => 4
)
[2] => Array
(
[pageviews] => 18
[visits] => 9
)
)
for($x=0; $x<count($array); $x++){
$newArray = $array[$x]; // that extract the second array, containing pageview and visits.
}
According to your samples, you seem to want to split one variable into multiple variables (or perhaps you used incorrect notation in the 2nd one?). If that is the case, and you know how many arrays are in the starting variable, you can do this:
list($one, $two, $three) = $originalArray;
If you don't know how many arrays are in the original array, or there is more than a handful, I have to wonder why you would want to or need to do this in the first place...