HI,
this is my array coming in a variable
Array
(
[msg] => Array
(
[0] => Array
(
[alertId] => 2416
[alerttitle] => Raven Lexy
[alertImageUrl] => photos/81951b37ad01c4aa65662956f178214eth.jpeg
[alertDescription] => (1) New Message(s)
[alertType] => New Message
[Date] => 1304679217
[count] => 1
)
)
[rehp] => Array
(
[0] => Array
(
[alertId] => 48
[alerttitle] => Artin
[alertImageUrl] => photos/95eaf8416ee68981ab944465bcdd7bffth.jpeg
[alertDescription] => Reply From Artin
[alertType] => Reply To Hotpress
[count] => 1
[id] => 48
)
[1] => Array
(
[alertId] => 48
[alerttitle] => Artin
[alertImageUrl] => photos/95eaf8416ee68981ab944465bcdd7bffth.jpeg
[alertDescription] => Reply From Artin
[alertType] => Reply To Hotpress
[count] => 1
[id] => 48
)
i want to convert into
Array
(
[0] => Array
(
[alertId] => 2416
[alerttitle] => Raven Lexy
[alertImageUrl] => photos/81951b37ad01c4aa65662956f178214eth.jpeg
[alertDescription] => (1) New Message(s)
[alertType] => New Message
[Date] => 1304679217
[count] => 1
)
[1] => Array
(
[alertId] => 48
[alerttitle] => Artin
[alertImageUrl] => photos/95eaf8416ee68981ab944465bcdd7bffth.jpeg
[alertDescription] => Reply From Artin
[alertType] => Reply To Hotpress
[count] => 1
[id] => 48
)
[2] => Array
(
[alertId] => 48
[alerttitle] => Artin
[alertImageUrl] => photos/95eaf8416ee68981ab944465bcdd7bffth.jpeg
[alertDescription] => Reply From Artin
[alertType] => Reply To Hotpress
[count] => 1
[id] => 48
)
)
how can i use foreach loop/for loop to get the the result.
thanks
What about
$new_array = array_merge($orig["msg"],$orig["rehp"])
Simple foreach loop and concatenating the arrays:
$result = array();
foreach($array as $a) {
$result = array_merge($result, $a);
}
This works and has been tested:
$a = Array(
"msg" => Array
(
0 => Array
(
"alertId" => 2416,
"alerttitle" => "Raven Lexy",
"alertImageUrl" => "photos/81951b37ad01c4aa65662956f178214eth.jpeg",
"alertDescription" => "(1) New Message(s)",
"alertType" => "New Message",
"Date" => 1304679217,
"count" => 1
)
),
"rehp" => Array
(
0 => Array
(
"alertId" => 48,
"alerttitle" => "Artin",
"alertImageUrl" => "photos/95eaf8416ee68981ab944465bcdd7bffth.jpeg",
"alertDescription" => "Reply From Artin",
"alertType" => "Reply To Hotpress",
"count" => 1,
"id" => 48
),
1 => Array
(
"alertId" => 48,
"alerttitle" => "Artin",
"alertImageUrl" => "photos/95eaf8416ee68981ab944465bcdd7bffth.jpeg",
"alertDescription" => "Reply From Artin",
"alertType" => "Reply To Hotpress",
"count" => 1,
"id" => 48,
)
)
);
$b = array();
foreach ($a as $v)
{
foreach ($v as $i)
$b[] = $i;
}
print_r($b);
Related
i have this array
Array
(
[0] => Array
(
[user_id] => 78
[post_id] => 3
[post_user_added_id] => 2
)
[1] => Array
(
[user_id] => 76
[post_id] => 3
[post_user_added_id] => 16
)
[2] => Array
(
[user_id] => 78
[post_id] => 3
[post_user_added_id] => 12
)
[3] => Array
(
[user_id] => 76
[post_id] => 9
[post_user_added_id] => 15
)
[4] => Array
(
[user_id] => 77
[post_id] => 9
[post_user_added_id] => 15
)
what i want to do is if the key post_id is repeated i just want to empty it and keep one so my final array will look like this
Array
(
[0] => Array
(
[user_id] => 78
[post_id] => 3
[post_user_added_id] => 2
)
[1] => Array
(
[user_id] => 76
[post_id] =>
[post_user_added_id] => 16
)
[2] => Array
(
[user_id] => 78
[post_id] =>
[post_user_added_id] => 12
)
[3] => Array
(
[user_id] => 76
[post_id] => 9
[post_user_added_id] => 15
)
[4] => Array
(
[user_id] => 77
[post_id] =>
[post_user_added_id] => 15
)
i have tried this code but it doesn't seem to work it deletes the whole array
foreach($arry as $k => $v)
{
foreach($arry as $key => $value)
{
if($k != $key && $v['post_id'] == $value['post_id'])
{
unset($arry [$k]);
}
}
}
print_r($arry);
You can perform foreach with ternary operator
$last = null;//this will keep the previous post_id
foreach($arr as &$v){
($last && $last == $v['post_id']) ? ($v['post_id'] = '') : ($last = $v['post_id']);
}
print_r($arr);
Working example :- https://3v4l.org/RiU9J
here try this.
$tempArr = [];
$resArr = [];
foreach($orignalArr as $key=>$obj){
if(in_array($obj['post_id'], $tempArr)){
$obj['post_id'] = '';
$resArr[] = $obj;
}else{
$tempArr[] = $obj['post_id'];
$resArr[] = $obj;
}
}
print_r($resArr);
try this :
// example code
$arrayResult = array();
$arry = [
1 =>
[
'user_id' => 78,
'post_id' => 3,
'post_user_added_id' => 2
],
2=>
[
'user_id' => 76,
'post_id' => 3,
'post_user_added_id' => 16
],
3 =>
[
'user_id' => 78,
'post_id' => 3,
'post_user_added_id' => 12
],
4 =>
[
'user_id' => 76,
'post_id' => 9,
'post_user_added_id' => 15
],
5 =>
[
'user_id' => 77,
'post_id' => 9,
'post_user_added_id' => 15
]];
$final = array();
foreach($arry as $a)
{
if (in_array($a['post_id'], $arrayResult)) {
$a['post_id'] = 0;
}else{
$arrayResult[] = $a['post_id'];
}
$final[] = $a;
}
var_dump($final);
Maybe if you use a stack for comparison?
edit: rewritten the code...
$stack=[];
foreach ($array as $index => $subarray){
if(in_array($subarray['post_id'], $stack)) {
$array[$index]['post_id'] = null;
}
else $stack[]=$subarray['post_id'];
}
print_r($array);
example: https://3v4l.org/FsPUG
Hope this helps!
use of your array value in instead of $arr and try to filter array like the following code :
$arr = array(
array(
'user_id'=>15,
'post_id'=>3,
'post_user_added_id'=>2
),
array(
'user_id'=>16,
'post_id'=>3,
'post_user_added_id'=>2
),
array(
'user_id'=>18,
'post_id'=>3,
'post_user_added_id'=>2
),
array(
'user_id'=>18,
'post_id'=>3,
'post_user_added_id'=>2
),
array(
'user_id'=>16,
'post_id'=>3,
'post_user_added_id'=>2
),
);
$postids = array();
foreach($arr as $key=>$val){
if(in_array($val['post_id'], $postids)){
$arr[$key]['post_id'] = '';
}else{
$postids[] = $val['post_id'];
}
}
echo "<pre>";print_r($arr);exit;
Output :
Array
(
[0] => Array
(
[user_id] => 15
[post_id] => 3
[post_user_added_id] => 2
)
[1] => Array
(
[user_id] => 16
[post_id] =>
[post_user_added_id] => 2
)
[2] => Array
(
[user_id] => 18
[post_id] =>
[post_user_added_id] => 2
)
[3] => Array
(
[user_id] => 18
[post_id] =>
[post_user_added_id] => 2
)
[4] => Array
(
[user_id] => 16
[post_id] =>
[post_user_added_id] => 2
)
)
This is my array,
array
(
[0] => Array
(
[Invoice_id] => 1
[Customer_Name] => Abcd Ltd
[Order_Created] => 2018-02-07
[Order_Delivery_Date] => 2018-02-17
[State_Code] => 35
[CGST] => 212.5
[SGST] => 212.5
[IGST] => 0
[Total_Amount] => 8925
)
[1] => Array
(
[Invoice_id] => 2
[Customer_Name] => Johnson and Sons
[Order_Created] => 2018-02-07
[Order_Delivery_Date] => 2018-02-17
[State_Code] => 35
[CGST] => 2975
[SGST] => 2975
[IGST] => 0
[Total_Amount] => 124950
)
)
How to convert this array like below,
array
(
array("invoice_id" => "1", "customer_name" => "Abcd Ltd", "order_created" => 2018-02-07, "delivery_date" => 2018-02-17, "state_code" => 35, "cgst" =>212.5, "sgst" =>212.5, "igst" =>0, "total_amount" =>8925),
array("invoice_id" => "2", "customer_name" => "Johnson and Sons", "order_created" => 2018-02-07, "delivery_date" => 2018-02-17, "state_code" => 35, "cgst" =>2975, "sgst" =>2975, "igst" =>512.5, "total_amount" =>124950)
);
You already have it in the format you want, they are just in different ways of displaying arrays, except that your arrays are indexed by keys, if you really want to unindex it use this $newArray = array_values($array)
You could try this to change keys of your array :
foreach ($array as $k => $item) {
foreach ($item as $key => $value) {
unset($array[$k][$key]) ; // remove old key
$array[$k][strtolower($key)] = $value ; // add new one
}
}
Then "Invoice_id" keys will be "invoice_id", and so on.
I need to know how many arrays have valid keys, how many arrays with valid keys in multidimensional array. Let me explain:
Input:
Array
(
[65] => Array
(
[1] => Array
(
[0] => Array
(
[mediumid] => 65
[mediumname] => VINYL
[trackid] => 525
[trackposition] => 1
[tracklocation] => SIDE A
[tracknumber] => 1
[trackname] => I love u
)
[1] => Array
(
[mediumid] => 65
[mediumname] => VINYL
[trackid] => 526
[trackposition] => 1
[tracklocation] => SIDE A
[tracknumber] => 2
[trackname] => Sun is yellow
)
)
[2] => Array
(
[0] => Array
(
[mediumid] => 65
[mediumname] => VINYL
[trackid] => 527
[trackposition] => 2
[tracklocation] => SIDE B
[tracknumber] => 1
[trackname] => Car red
)
[1] => Array
(
[mediumid] => 65
[mediumname] => VINYL
[trackid] => 528
[trackposition] => 2
[tracklocation] => SIDE B
[tracknumber] => 2
[trackname] => Lady in red
)
)
)
[769] => Array
(
[] => Array
(
[0] => Array
(
[mediumid] => 769
[mediumname] => DVD
[trackid] =>
[trackposition] =>
[tracklocation] =>
[tracknumber] =>
[trackname] =>
)
)
)
)
The mediums[65] next array contains 2 valid keys (1 and 2). The mediums[769] next array contains no valid keys
Therefore only mediums[65] contains valid keys, so total of arrays with valid keys = 1.
I need to find that total. How ?
I've try using array_keys and array_filter, with no success (or either i'm doing it wrong)
PHP code demo
<?php
$array=Array
(
65 => Array
(
1 => Array
(
0 => Array
(
"mediumid" => 65,
"mediumname" => "VINYL",
"trackid" => 525,
"trackposition" => 1,
"tracklocation" => "SIDE A",
"tracknumber" => 1,
"trackname" => "I love u"
),
1 => Array
(
"mediumid" => 65,
"mediumname" => "VINYL",
"trackid" => 526,
"trackposition" => 1,
"tracklocation" => "SIDE A",
"tracknumber" => 2,
"trackname" =>"Sun is yellow"
)
),
2 => Array
(
0 => Array
(
"mediumid" => 65,
"mediumname" => "VINYL",
"trackid" => 527,
"trackposition" => 2,
"tracklocation" => "SIDE B",
"tracknumber" => 1,
"trackname" => "Car red"
),
1 => Array
(
"mediumid" => 65,
"mediumname" => "VINYL",
"trackid" => 528,
"trackposition" => 2,
"tracklocation" => "SIDE B",
"tracknumber" => 2,
"trackname" => "Lady in red"
)
)
),
769 => Array
(
"" => Array
(
0 => Array
(
"mediumid" => 769,
"mediumname" => "DVD",
"trackid" => "",
"trackposition" => "",
"tracklocation" => "",
"tracknumber" =>"",
"trackname" => ""
)
)
)
);
$counter=0;
$trackedNull=false;
foreach($array as $key => $value)
{
$keys=array_keys($array[$key]);
foreach($keys as $arraykey)
{
if($arraykey=="")
{
$trackedNull=true;
break;
}
}
if($trackedNull==true)
{
$trackedNull=false;
}
else
{
$counter++;
}
}
echo $counter;
I am currently playing with Riot Games API and using one of the wrappers developed by the community (https://github.com/kevinohashi/php-riot-api). My issue is, I am trying to sort the results using arsort
My code example:
<?php
include('php-riot-api.php');
$summoner_name = 'fallingmoon';
$summoner_id = 24381045;
$test = new riotapi('euw');
$r = $test->getLeague($summoner_id);
?>
<?php
$array = json_decode($r, true);
foreach($array AS $key => $newArray) {
$tempArray[$key] = $newArray['entries'][0]['leaguePoints'];
}
arsort($tempArray);
$finalArray = array();
foreach($tempArray AS $key => $value) {
$finalArray[] = $array[$key];
}
?>
My goal is sort the array by league points (Highest to lowest), but the output of the array once I print it is as followed, as you can see it hasn't sorted. I am probably missing something very minor but any help will be greatly appreciated.
Array
(
[0] => Array
(
[name] => Rengar's Demolishers
[tier] => GOLD
[queue] => RANKED_SOLO_5x5
[entries] => Array
(
[0] => Array
(
[playerOrTeamId] => 33372844
[playerOrTeamName] => L3tsPl4yLoL
[leagueName] => Rengar's Demolishers
[queueType] => RANKED_SOLO_5x5
[tier] => GOLD
[rank] => V
[leaguePoints] => 0
[wins] => 34
[isHotStreak] => 1
[isVeteran] =>
[isFreshBlood] =>
[isInactive] =>
[lastPlayed] => -1
)
[1] => Array
(
[playerOrTeamId] => 19397582
[playerOrTeamName] => Lunchi
[leagueName] => Rengar's Demolishers
[queueType] => RANKED_SOLO_5x5
[tier] => GOLD
[rank] => IV
[leaguePoints] => 10
[wins] => 7
[isHotStreak] =>
[isVeteran] =>
[isFreshBlood] =>
[isInactive] =>
[lastPlayed] => -1
)
[2] => Array
(
[playerOrTeamId] => 24613501
[playerOrTeamName] => RadiantBurst
[leagueName] => Rengar's Demolishers
[queueType] => RANKED_SOLO_5x5
[tier] => GOLD
[rank] => II
[leaguePoints] => 42
[wins] => 48
[isHotStreak] =>
[isVeteran] =>
[isFreshBlood] =>
[isInactive] =>
[lastPlayed] => -1
)
[3] => Array
(
[playerOrTeamId] => 19939979
[playerOrTeamName] => vinter
[leagueName] => Rengar's Demolishers
[queueType] => RANKED_SOLO_5x5
[tier] => GOLD
[rank] => I
[leaguePoints] => 38
[wins] => 57
[isHotStreak] =>
[isVeteran] =>
[isFreshBlood] =>
[isInactive] =>
[lastPlayed] => -1
)
The issue is that $array is an array with one array in it.
Presumably you want to sort the entries array in which case you can tweak your code:
foreach($array[0]['entries'] AS $key => $team) {
$tempArray[$key] = $team['leaguePoints'];
}
arsort($tempArray);
$finalArray = array();
foreach($tempArray AS $key => $value) {
$finalArray[] = $array[0]['entries'][$key];
}
Note that the above doesn't support multiple leagues.
However I find using usort to be more readable:
foreach($array as $key => $league){
usort($array[$key]['entries'], function($a,$b){
return $a['leaguePoints'] - $b['leaguePoints'];
});
}
My array is like that:
Array
(
[0] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 1
[tran_name] => private
[tran_image] => 1251961905A1.jpg
[type] => car
[troute_id] => 10
)
[1] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 2
[tran_name] => express
[tran_image] => bus3.jpg
[type] => car
[troute_id] => 13
)
[2] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 3
[tran_name] => MyanmarTrain
[tran_image] => Burma-Gorteikviaduct.jpg
[type] => train
[troute_id] => 16
)
[3] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 4
[tran_name] => Ayeyarwaddy Cruise
[tran_image] => boat-ChutzpahToo1.jpg
[type] => cruise
[troute_id] => 22
)
)
I want to change that array like that depending on key['type']. If array key['type'] are same, I want to change array like that:
Array
(
[car] => Array(
[0]=>Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 1
[tran_name] => private
[tran_image] => 1251961905A1.jpg
[type] => car
[troute_id] => 10
),
[1] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 2
[tran_name] => express
[tran_image] => bus3.jpg
[type] => car
[troute_id] => 13
)
),
[train]=>Array(
[0] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 3
[tran_name] => MyanmarTrain
[tran_image] => Burma-Gorteikviaduct.jpg
[type] => train
[troute_id] => 16
)
[cruise]=>Array(
[0] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 4
[tran_name] => Ayeyarwaddy Cruise
[tran_image] => boat-ChutzpahToo1.jpg
[type] => cruise
[troute_id] => 22
)
)
)
)
what I mean is that if key['type'] is car, I want to create car array or if the type is train I want to create train array or if the type is cruise I want to create cruise array. I don't know how to loop the array. Anyone please help me. Thanks a lot!
Here's a simple way to do it: loop over the data, and just append to the subarray matching the type value:
// starting data
$starting_array = array (
0 => array (
'des_id' => 1,
'des_name' => 'bagan',
'tran_id' => 1,
'tran_name' => 'private',
'tran_image' => '1251961905A1.jpg',
'type' => 'car',
'troute_id' => 10
),
1 => array (
'des_id' => 1,
'des_name' => 'bagan',
'tran_id' => 2,
'tran_name' => 'express',
'tran_image' => 'bus3.jpg',
'type' => 'car',
'troute_id' => 13
),
2 => array (
'des_id' => 1,
'des_name' => 'bagan',
'tran_id' => 3,
'tran_name' => 'MyanmarTrain',
'tran_image' => 'Burma-Gorteikviaduct.jpg',
'type' => 'train',
'troute_id' => 16
),
3 => array (
'des_id' => 1,
'des_name' => 'bagan',
'tran_id' => 4,
'tran_name' => 'Ayeyarwaddy Cruise',
'tran_image' => 'boat-ChutzpahToo1.jpg',
'type' => 'cruise',
'troute_id' => 22
)
);
// initialize the result array
$result = array();
// loop over the starting array
foreach($starting_array as $entry) {
// make sure the result array has a key matching this item's type
if(!array_key_exists($entry['type'], $result)) {
$result[ $entry['type'] ] = array();
}
// add this item to the result array
$result[ $entry['type'] ][] = $entry;
}
// this is just for testing, so you can verify the output matches your desired result
echo "<pre>";
var_dump($result);
echo "</pre>";
Try this:
<?php
$tempArr = Array
(
Array(
"des_id" => 1,
"des_name" => "bagan",
"tran_id" => 1,
"tran_name" => "private",
"tran_image" => "1251961905A1.jpg",
"type" => "car",
"troute_id" => 10
),
Array
(
"des_id" => 1,
"des_name" => "bagan",
"tran_id" => 2,
"tran_name" => "express",
"tran_image" => "bus3.jpg",
"type" => "car",
"troute_id" => 13
),
Array
(
"des_id" => 1,
"des_name" => "bagan",
"tran_id" => 3,
"tran_name" => "MyanmarTrain",
"tran_image" => "Burma-Gorteikviaduct.jpg",
"type" => "train",
"troute_id" => 16
),
Array
(
"des_id" => 1,
"des_name" => "bagan",
"tran_id" => 4,
"tran_name" => "Ayeyarwaddy Cruise",
"tran_image" => "boat-ChutzpahToo1.jpg",
"type" => "cruise",
"troute_id" => 22
)
);
$resultArr = array();
foreach($tempArr as $tempKey=>$temp)
{
if(!array_key_exists($temp['type'], $resultArr))
{
$resultArr[$temp['type']] = array();
}
$resultArr[$temp['type']][] = $temp;
}
echo '<pre>';
print_r($resultArr);
?>
This is working fine .....