I have a array variable $data which has the following array structure:
Array
(
[0] => Array
(
[rowdata] => Array
(
[0] => Array
(
[wiecont_03] =>
[tevrc_04] =>
[opmerkTXT] => Overall NPS
[0] => Array-----------------------
( |
[0] => rapport |
[1] => npsorg |
[3] => npsdetbe | i want this
part of the array
)-------------------------------
)
[1] => Array
(
[DOELGROEP] => 2
[KANTOOR] => 2
[OBLIGOCATEGORIE] => 3
[NAAM] => dfdf
[WEIGHT] => 0.95
[opmerkTXT] => Overall NPS
[0] => Array
(
[1] => npsorg
[3] => npsdetbe
)
)
)
[reason] => column values are not correct
)
)
from the above array structure i want to fetch the sub array if any as depicted by the dotted line.
Desired Output:
[0] => Array
(
[0] => rapport
[1] => npsorg
[3] => npsdetbe
)
and
[0] => Array
(
[1] => npsorg
[3] => npsdetbe
)
What i have tried: i tried array_slice() and array_chunks() but none of them worked in my case.
Thanks in advance
You also can do it(in short) like this:
Demo: https://eval.in/86588
<?php
$arr = array
(
0 => array
(
"rowdata" => array
(
"0" => array
(
"wiecont_03" => "",
"tevrc_04" => "",
"opmerkTXT" => "Overall NPS",
"0" => array
(
"0" => "rapport",
"1" => "npsorg",
"3" => "npsdetbe"
)
),
"1" => array
(
"DOELGROEP" => 2,
"KANTOOR" => 2,
"OBLIGOCATEGORIE" => 3,
"NAAM" => "dfdf",
"WEIGHT" => 0.95,
"opmerkTXT" => "Overall NPS",
"0" => array
(
"1" => "npsorg",
"3" => "npsdetbe",
)
)
)
)
);
$re = array();
//print_r($arr[0]['row_data']);
$i=0;
foreach($arr[$i]['rowdata'] as $a){
if(is_array($a[0])){
array_push($re,$a[0]);
}
$i =$i+1;
}
print_r($re);
TRY THIS:
Demo : https://eval.in/86560
$arr = array
(
"0" => array
(
"rowdata" => array
(
"0" => array
(
"wiecont_03" => "",
"tevrc_04" => "",
"opmerkTXT" => "Overall NPS",
"0" => array
(
"0" => "rapport",
"1" => "npsorg",
"3" => "npsdetbe"
)
),
"1" => array
(
"DOELGROEP" => 2,
"KANTOOR" => 2,
"OBLIGOCATEGORIE" => 3,
"NAAM" => "dfdf",
"WEIGHT" => 0.95,
"opmerkTXT" => "Overall NPS",
"0" => array
(
"1" => "npsorg",
"3" => "npsdetbe",
)
)
)
)
);
$re = array();
foreach($arr as $a){
foreach($a as $b){
foreach($b as $c){
array_push($re,$c[0]);
}
}
}
print_r($re);
OUTPUT
Array
(
[0] => Array
(
[0] => rapport
[1] => npsorg
[3] => npsdetbe
)
[1] => Array
(
[1] => npsorg
[3] => npsdetbe
)
)
Your pattern is same in all case then this function is fine
function getMyArray($array){
$rowdata = $array[0]['rowdata'];
$resultArray = array();
foreach($rowdata as $val){
$resultArray[] = $val[0];
}
return $resultArray;
}
Usage:-
$array = "YOUR ARRAY HERE";
$result = getMyArray($array);
foreach($data[0]['rowdata'] as $value){
print_r($value[0]);
}
Related
I have twomultidimensional array in below format
$skillInfo = Array
(
"Mechanical" => Array
(
"0" => 100
"1" => 400
)
"Understanding" => Array
(
"0" => 200
)
"Application/Appreciation" => Array
(
"0" => 300
)
);
$skillMaster = [
["skillID" => 1, "skillName" => "Mechanical"],
["skillID" => 2, "skillName" => "Understanding"],
["skillID" => 3, "skillName" => "Application/Appreciation"]
];
$skillMaster skillName is the same as $skillInfo keys, so we can replace $skillInfo keys from $skillMaster skillID.
Expected output
Array
(
[1] => Array
(
[0] => 100
[1] => 400
)
[2] => Array
(
[0] => 200
)
[3] => Array
(
[0] => 300
)
)
my code
foreach($skillInfo as $key1 => $val1){
$skillInfo[$key1][$skillMaster[$val1["skillID"]]] = $skillMaster[$val1["skillID"]] ?? [];
}
above code is not working as expected output, kindly anyone help me out.
foreach($skillMaster as $item){
if(array_key_exists($item['skillName'], $skillInfo)){
$skillInfo[$item['skillID']] = $skillInfo[$item['skillName']];
unset($skillInfo[$item['skillName']]);
}
}
I have two array as bellow :
First array :
Array
(
[0] => Array
(
[0] => Array
(
[name] => one
[number] => 051
)
[1] => Array
(
[name] => two
[number] => 052
)
[2] => Array
(
[name] => three
[number] => 053
)
)
[1] => Array
(
[0] => Array
(
[name] => four
[number] => 061
)
[1] => Array
(
[name] => five
[number] => 062
)
)
)
I want to make output from first array above
[0] => 051, 052, 053.
[1] => 061, 062.
Array
(
[0] => Array
(
[0] => Array
(
[name] => book
[number] => 41
)
[1] => Array
(
[name] => pencil
[number] => 42
)
)
[1] => Array
(
[name] => eraser
[number] => 71
)
)
I want to make output from second array above
[0] => 41, 42.
[1] => 71.
Please advise. Thank you.
You can make a try like this way with two foreach() loop.
$numbers = [];
foreach ($array as $k => $v) {
$num = [];
foreach ($v as $k2 => $v2) {
$num[] = $v2['number'];
}
$numbers[$k] = implode(',',$num).'.';
}
print_r($numbers);
DEMO: https://3v4l.org/mEeO7
you can try something like this
$arr = Array (
Array (
Array (
"name" => "one",
"number" => "051"
),
Array (
"name" => "two",
"number" => "052"
),
Array (
"name" => "three",
"number" => "053"
)
),
Array (
Array (
"name" => "four",
"number" => "061"
),
Array (
"name" => "five",
"number" => "062"
)
)
);
foreach ($arr as $k => $s_arr) {
echo "[" . $k . "] => ";
foreach ($s_arr as $k2 => $v2) {
echo $v2["number"] . " ";
}
echo "\n";
}
I have two array. first one is multi dimensional array. second array contains key and value pair. now my goal is i want to check second index of every array from first array value to check that value in second array as key exist? if yes then that first array value need to replace with second array value.
$first_array = Array
(
[0] => Array
(
[0] => 2012/12
[1] =>
[2] => "SI"
[3] =>
[4] =>
[5] =>
)
[1] => Array
(
[0] => 2012/12
[1] =>
[2] => "MB"
[3] =>
[4] =>
[5] =>
)
)
$second_array = array(
["MB"] => "WE",
["SI"] => "SA",
["SO"] => "SA",
)
my output should look like this
$first_array = Array
(
[0] => Array
(
[0] => 2012/12
[1] =>
[2] => "SA"
[3] =>
[4] =>
[5] =>
)
[1] => Array
(
[0] => 2012/12
[1] =>
[2] => "WE"
[3] =>
[4] =>
[5] =>
)
)
Just go with simple foreach loop and if
Try this code snippet here
<?php
ini_set('display_errors', 1);
$first_array = Array
(
0 => Array
(
0 => "2012/12",
1 => "",
2 => "SI",
3 => "",
4 => "",
5 => "",
),
1 => Array
(
0 => "2012/12",
1 => "",
2 => "MB",
3 => "",
4 => "",
5 => "",
)
);
$second_array = array(
"MB" => "WE",
"SI" => "SA",
"SO" => "SA",
);
foreach($first_array as $key1 => $data)
{
if(isset($second_array[$data[2]]))
{
$first_array[$key1][2]=$second_array[$data[2]];
}
}
print_r($first_array);
What about foreach?
foreach($first_array as &$v)
{
//$v[2] = isset($second_array[$v[2]]) ? $second_array[$v[2]] : $v[2];
if(isset($second_array[$v[2]))
$v[2] = $second_array[$v[2]];
}
print_r($first_array);
I can generate array from mysql, if there is a way for making it easy. I get rows and currently I am rendering like that, if there is a way to generate on fly
3, 0.4311 |
3, 0.1803 |
4, 0.1149 |
4, 0.0775 |
5, 0.4291 |
5, 0.5100|
Considering this array, how to merge it :
Array
(
[0] => Array
(
[channel_id] => 3
[value] => 0.4311
)
[1] => Array
(
[channel_id] => 3
[value] => 0.1803
)
[2] => Array
(
[channel_id] => 4
[value] => 0.1149
)
[3] => Array
(
[channel_id] => 4
[value] => 0.0775
)
...
)
so it will look like this this:
Array
(
[0] => Array
(
[channel_id] => 3
[value] => 0.4311
[value] => 0.1803
)
[1] => Array
(
[channel_id] => 4
[value] => 0.1149
[value] => 0.0775
)
)
Here is the code that I generate this array:
while($row = $result->fetch_assoc()) {
$array[] = array('channel_id'=>$row['channel_id'],'value'=>$row['value'] );
}
OK this seem impossible, but this is closed I could get:
$channels_byid = array();
foreach($array as $v){
#$channel = $channels_byid[$v['channel_id']];
if ($array){
if(!is_array($channel[0])){
unset($channels_byid[$v['channel_id']]);
$channels_byid[$v['channel_id']] = $channel;
}
$channels_byid[$v['channel_id']][] = $v;
} else {
$channels_byid[$v['channel_id']] = $v;
}
}
print_r($channels_byid);
Which outputs:
[3] => Array
(
[0] => Array
(
[channel_id] => 3
[value] => 0.7513
)
[1] => Array
(
[channel_id] => 3
[value] => 0.7234
)
)
[4] => Array
(
[0] => Array
(
[channel_id] => 4
[value] => 0.9798
)
[1] => Array
(
[channel_id] => 4
[value] => 0.7625
)
)
Not exactly what you were after, but it might be useful. The channel_id becomes the top-level key and value becomes the values of a new array.
$data[] = array('channel_id' => 3, 'value' => 0.4311);
$data[] = array('channel_id' => 3, 'value' => 0.1803);
$data[] = array('channel_id' => 4, 'value' => 0.1149);
$data[] = array('channel_id' => 4, 'value' => 0.0775);
foreach($data as $k => $v) {
$merge[$v['channel_id']][] = $v['value'];
}
And the result of $merge is:
array(2) {
[3]=> array(2) {
[0]=> float(0.4311)
[1]=> float(0.1803)
}
[4]=> array(2) {
[0]=> float(0.1149)
[1]=> float(0.0775)
}
}
so i retrieve a json, convert it into an array and i got this output:
Array
(
[Sid] => 23888555
[pages] => Array
(
[0] => Array
(
[id] => 13111071
[name] => Page 1
[slots] => Array
(
[0] => Array
(
[SlotId] => 6
[info] => Array
(
[id] => 5247
[color] => red
)
)
[1] => Array
(
[SlotId] => 4
[info] => Array
(
[id] => 5267
[color] => blue
)
)
[2] => Array
(
[SlotId] => 7
[info] => Array
(
[id] => 5267
[color] => green
)
)
)
)
[1] => Array
(
[id] => 13111072
[name] => Page 2
[slots] => Array
(
[0] => Array
(
[SlotId] => 6
[info] => Array
(
[id] => 5247
[color] => red
)
)
[1] => Array
(
[SlotId] => 4
[info] => Array
(
[id] => 5267
[color] => blue
)
)
)
)
)
)
I have no problem reading it whatsoever, what i wanna do is count for every page how many similar "last" id i got.
Exemple :
[pages][0][slots][0][info][id]
[pages][0][slots][1][info][id]
[pages][0][slots][3][info][id]
For the page 1, I wanna compare these 3 ids between them and count the occurrences.
[pages][1][slots][0][info][id]
[pages][1][slots][1][info][id]
For the page 2, I wanna compare these 2 ids between them and count the occurrences.
The output i want looks like this :
page 1 -> 1x5247
-> 2x5267
page 2 -> 1x5247
-> 1x5267
EDIT :
I tried using
foreach ($data['pages'] as $item) {
foreach ($item['slots'] as $slotnumber => $value) {
print_r(array_count_values($item['slots'][$slotnumber]['info']));
}
}
which returns me this :
Array ( [5247] => 1 [red] => 1 )
Array ( [5267] => 1 [blue] => 1 )
Array ( [5267] => 1 [green] => 1 )
So i think i might be able to use this but i don't know how.
I manually declared the array and then created the countstuff() function. This should work for you. I tested it and it work on my end. After going through all this trouble, I really do appreciated it if you choose my answer and up vote it.
<?php
$data = Array("Sid" => "23888555", "pages" => Array("0" => Array("id" => "13111071", "name" => "Page 1", "slots" => Array("0" => Array("SlotId" => "6", "info" => Array("id" => "5247", "color" => "red")), "1" => Array("SlotId" => "4", "info" => Array("id" => "5267", "color" => "blue")), "2" => Array("SlotId" => "7","info" => Array("id" => "5267", "color" => "green")))),
"1" => Array
(
"id" => "13111072",
"name" => "Page 2",
"slots" => Array
(
"0" => Array
(
"SlotId" => "6",
"info" => Array
(
"id" => "5247",
"color" => "red"
)
),
"1" => Array
(
"SlotId" => "4",
"info" => Array
(
"id" => "5267",
"color" => "blue"
)
)
)
)
)
);
//End of array declaration
//Now the really convoluted coding starts
//Create a function
function countstuff($yourarray){
foreach($yourarray as $mainarraykey => $mainarray){
if($mainarraykey == "pages"){
foreach($mainarray as $pageskey => $pagesarray){
//echo "Page \"$pageskey\"<br/>";
foreach($pagesarray as $pagessubarraykey => $pagessubarray_array){
if($pagessubarraykey == "slots"){
foreach($pagessubarray_array as $slotskey => $slots){
foreach($slots as $slotssubkey => $slotssub){
if($slotssubkey == "info"){
foreach($slotssub as $key => $value){
if($key == "id"){
//echo $value."<br/>";
$pages[$pageskey][] = $value;
}
}
}
}
}
}
}
}
}
}
return $pages;
}
//Execute the countstuff() function
$output = countstuff($data);
function showresults($input){
foreach($input as $pagekey => $page){
echo "Page $pagekey:<br/>";
$results = array_count_values($page);
foreach($results as $resultkey => $result){
echo $result."x".$resultkey."<br/>";
}
echo "<br/>";
}
}
showresults($output);
?>
I tried some things let me know what you guys think of this.
I get every id then enter them into an array then use array_count_values
$array_ids = array();
foreach ($data['pages'] as $item) {
$numberOfElements = count($item['slots']);
$z= 0;
foreach ($item['slots'] as $slotnumber => $value) {
$array_ids[$z] = $item['slots'][$slotnumber]['info']['id'];
// search for occurrences when the array is full
if (count($array_ids) == $numberOfElements) {
var_dump(array_count_values($array_ids));
// reset the array to 0 every time we loop through the whole infos
$array_ids = array();
}
$z++;
}
}
This seems to work for every page.
array (size=2)
5267 => int 2
5247 => int 1
array (size=2)
5267 => int 1
5247 => int 1