I have an array
Array (
[0] => Array ( [0] => Array ( [videoId] => FysV6XnDlQk [title] => Kannaana Kanney Song with Lyrics | Viswasam Songs | Ajith Kumar,Nayanthara | D.Imman|Siva|Sid Sriram [likeInfo] => Array ( [likes] => 1 [dislikes] => 0 [liked] => 1 [disliked] => 0 ) ) )
[1] => Array ( [0] => Array ( [videoId] => hXNSAb3s1XY [title] => Best of IRON MAN | Best of TONY STARK [2008-2018] [likeInfo] => Array ( [likes] => 0 [dislikes] => 0 [liked] => 0 [disliked] => 0 ) ) ) )
how can I get each videoId,title one by one
I have tried
$s=array();
for ($i=0; $i < sizeof($myArray) ; $i++) {
$s[] = array($myArray[$i]);
echo "<br/>";
}
print_r($s);
$ids=array();
foreach($s as $user) {
$ids[] = $user['videoId'];
}
print_r($ids);
I'm expecting each videoId and title should be print one by one..I totally confused..If you give me small hint I'll work on that
Use array_column to get particular key's value in multidimensional array
<?php
$data = array_column($yourArr, 0);
$videoIdArr = array_column($data, 'title', 'videoId');
print_r($videoIdArr );
?>
Your output will be
Array
(
[FysV6XnDlQk] => Kannaana Kanney Song with Lyrics | Viswasam Songs | Ajith Kumar,Nayanthara | D.Imman|Siva|Sid Sriram
[hXNSAb3s1XY] => Best of IRON MAN | Best of TONY STARK
)
Try this .You have two sub array inside the array .So you need to iterate via 2 forEach loop.
forEach($myArray as $value){
forEach($value as $subvalue){
echo $subvalue['videoId'].':'.$subvalue['title'];
}
}
Related
I have an array which is
Array ( [0] => Array ( [picture] => 5a55ed8d8a5c8910913.jpeg
[id] => 1284
[price_range] => Rs 12000 - 9000
[name] => Brown Beauty Office Chair )
[1] => Array ( [picture] => 5a55eefeb9a8e255836.jpeg
[id] => 1285
[price_range] => Rs 8989 - 7000
[name] => Chang Series Office Chair (Grey)
)
)
Now I am fetching the value of id on clicking a remove button, the value I fetch is 1284.
I want to take out just [id]=> 1284 from the above array and then display it using a foreach loop. How I can delete just the [id]=> 1284 without disturbing the other id values and other element.
In the above array I would like to delete one particular id value say just the [id]=> 1284 and keep all other elements intact and as it is.
Any help is welcome.
Use array_search and array_column, to find by id and remove by unset method,
<?php
$array = [
["id"=>123,"desc"=>"test1"],
["id"=>456,"desc"=>"test2"],
["id"=>789,"desc"=>"test3"],
];
$id = 456;
$index = array_search($id, array_column($array, 'id'));
unset($array[$index]);
print_r($array);
?>
Live Demo
Array
(
[0] => Array
(
[id] => 123
[desc] => test1
)
[2] => Array
(
[id] => 789
[desc] => test3
)
)
Since you asked how to achieve it using foreach, I came up with this.
$array = Array (Array ( 'picture' => '5a55ed8d8a5c8910913.jpeg','id' => 1284,'price_range' => 'Rs 12000 - 9000', 'name' => 'Brown Beauty Office Chair'),
Array ( 'picture' => '5a55eefeb9a8e255836.jpeg','id' => 1285,'price_range' => 'Rs 8989 - 7000','name' => 'Chang Series Office Chair (Grey)')
);
foreach($array as $key => $val) {
$id = $array[$key]['id'];
if($id === 1284){
unset($array[$key]['id']);
}
}
print_r($array)
?>
You can also use this too:
<?php
$element_to_remove = 1284;
$i = 0;
foreach($array as $this_arr){
$index = array_search($element_to_remove, $this_arr);
//unset($this_arr[$index]); this formate does not remove element from array
//but below works fine
if(isset($array[$i][$index])){
unset($array[$i][$index]);
}
}
print_r($array);
?>
I have 2 elements which has the value of member which is 5
I want to get the sum of all value of member
[member] => 5 + [member] => 5 = 10
The output should be 10
This is my array
Array
(
[0] => Array
(
[membUid] => 0090000816
[service_id] => 0
[member] => 5
)
[1] => Array
(
[membUid] => 0090000867
[service_id] => 0
[member] => 5
)
)
This is my code:
foreach($result['content'] as $res){
sum($res['member']);
}
Does this work?
$sum = 0;
foreach ($result as $res) {
$sum += $res['member'];
}
echo $sum;
alternative solution, with array_sum & array_map functions
echo array_sum(array_map(function($i){return $i["member"];},$result['content']));
I have this array which has more then 100 results but some of these has same sub array key. I would like to sum array element which has same key which is [/xyx/888350] in this example. However, I want to keep the format as it is which is two dimensional.
Array
(
[0] => Array
(
[/xyx/888350] => /xyx/888350
[visitors] => 1
[pageviews] => 2
[uniquepageviews] => 1
)
[1] => Array
(
[/xyx/888350] => /xyx/888350
[visitors] => 1
[pageviews] => 3
[uniquepageviews] => 1
)
[2] => Array
(
[/xyx/888350] => /xyx/888350
[visitors] => 1
[pageviews] => 2
[uniquepageviews] => 1
)
[3] => Array
(
[/xyx/102254] => /xyx/102254
[visitors] => 1
[pageviews] => 2
[uniquepageviews] => 1
)
)
I am expecting out put something like below:
Array
(
[0] => Array
(
[/xyx/888350] => /xyx/888350
[visitors] => 2
[pageviews] => 7
[uniquepageviews] => 2
)
[1] => Array
(
[/xyx/102254] => /xyx/102254
[visitors] => 1
[pageviews] => 3
[uniquepageviews] => 1
)
)
Thanks in advance.
Loop the array, and store the results in a temporary array, by using the first value as a key:
$input = /*your example data here*/;
$result = array();
foreach($input as $data){
$keys = array_keys($data);
$key = $keys[0]; //get the first key of the array a.k.a '/xyx/888350'
if(isset($result[$key])){
//sum the values if we have this key
$result[$key]['visitors'] += $data['visitors'];
$result[$key]['pageviews'] += $data['pageviews'];
$result[$key]['uniquepageviews'] += $data['uniquepageviews'];
}else{
$result[$key] = $data;
}
}
//drop the extra keys and return a indexed array with the summed values
return array_values($result);
$results=$service->data_ga->get("");
$stats = array();
for ($i=0; $i < count($stats_results=$results->getRows()) ; $i++)
{
$stats[]=array(
'path'=>$stats_results[$i][1],
'visitors'=>$stats[$i]['visitors'] + $stats_results[$i][2],
'pageviews'=>$stats[$i]['visitors'] + $stats_results[$i][3],
'uniquepageviews'=>$stats[$i]['visitors'] + $stats_results[$i][4]
);
}
$result = array();
foreach($stats as $data){
$keys = array_keys($data);
$key = $keys[0]; //get the first key of the array a.k.a '/xyx/888350'
if(isset($result[$key]))
{
//sum the values if we have this key
$result[$key]['visitors'] += $data['visitors'];
$result[$key]['pageviews'] += $data['pageviews'];
$result[$key]['uniquepageviews'] += $data['uniquepageviews'];
}else{
$result[$key] = $data;
}
}
echo "<pre>";
print_r($result);
Output:
Array
(
[path] => Array
(
[path] => /path/888350
[visitors] => 3
[pageviews] => 7
[uniquepageviews] => 3
)
)
Thanks both of you. As time goes on I will also answer questions other people have. I have just started my career. Thank you guys :)
So I'm trying to create a recursive flattenArray function that will take an array (with an unknown number of elements and sub-arrays (with possibly more sub-arrays).
Here's the PHP code I'm having trouble with:
<?php
// Javascript Array [ [1,2,3],[[[4]]],[5],[6],[[7,8,[9]]] ]
// Equivalent PHP Array:
$sampleArray = Array(
Array(1,2,3),
Array(
Array(
Array(4)
)
),
Array(5),
Array(6),
Array(
Array(
7,
8,
Array(9)
)
)
);
$finishedArray = Array();
function flattenArray($array){
foreach ($array as $key => $value) {
if(is_array($value)){
flattenArray($value);
} else {
$finishedArray[] = $value;
echo "<br> ".$value." | ";
print_r($finishedArray);
}
}
}
flattenArray($sampleArray);
echo "<br><br>FinishedArray: <br>";
print_r($finishedArray);
?>
I get this output:
1 | Array ( [0] => 1 )
2 | Array ( [0] => 1 [1] => 2 )
3 | Array ( [0] => 1 [1] => 2 [2] => 3 )
4 | Array ( [0] => 4 )
5 | Array ( [0] => 5 )
6 | Array ( [0] => 6 )
7 | Array ( [0] => 7 )
8 | Array ( [0] => 7 [1] => 8 )
9 | Array ( [0] => 9 )
FinishedArray:
Array ( )
For some reason it resets the $finishedArray.
What am I doing wrong here?
Seems you need to read up on variable scope. There are 2 variables called $finishedArray - one in the main script, and another within the function. Probably the simplest solution would be to pass it via reference:
function flattenArray($array, &$finishedArray){
foreach ($array as $key => $value) {
if(is_array($value)){
flattenArray($value, $finishedArray);
} else {
$finishedArray[] = $value;
}
}
}
$finishedArray = Array();
flattenArray($sampleArray, $finishedArray);
I have array like this. This array is created dynamically
Array
(
[Data NotUploading] => Array
(
[items] => Array
(
[0] => Array
(
[date] => 2013-04-02
[issue_id] => 1
[phone_service_device] => A
[phone_service_model] =>
[phone_service_os] =>
[phone_service_version] =>
[issue_name] => Data NotUploading
)
[1] => Array
(
[date] => 2013-04-02
[issue_id] => 1
[phone_service_device] => I
[phone_service_model] =>
[phone_service_os] =>
[phone_service_version] =>
[issue_name] => Data NotUploading
)
)
)
[Battery Problem] => Array
(
[items] => Array
(
[0] => Array
(
[date] => 2013-04-03
[issue_id] => 3
[phone_service_device] => I
[phone_service_model] =>
[phone_service_os] =>
[phone_service_version] =>
[issue_name] => Battery Problem
)
)
)
)
What I need to do is to use 2 foreach or 1 foreach & 1 for loop so that I can get each value of date I did like this
foreach($gResultbyName as $key1 => $rec){
for($j = 0;$j<count($rec);$j++ ){
echo $rec['items'][$j]['date'];
}
}
but its only retrieving 2013-04-02 & 2013-04-03 that is 0 index date of data NotUploading & 0 index date of Battery Problem. Basically I need to compare each value and others stuff but I just cant get each date value.
Forgive me for ignorance but I tried a lot :(
try this:
foreach ($data as $d)
{
foreach($d['items'] as $item)
{
echo $item['date'];
}
}
Your for-loop loops count($rec) times, but should count($rec['items']).
This should load the dates into an array sorted by the issue_name. The you can step through them for further processing.
$dates= array();
foreach ($gResultbyName AS $events){
foreach ($events['items'] AS $item){
$dates[$item['issue_name']][] = $item['date'];
}
}
var_dump($dates);