I have below array and i want to fetch [2] => Array with foreach but it's showing me an error.
for example array name is $other
Array
(
[0] => Array
(
[0] => aaaaaaaaaaaa
[1] => bbbbbbbbbbbb
[2] => cccccccccccc
)
[1] => Array
(
[0] => dddddddddddd
[1] => eeeeeeeeeeee
[2] => ffffffffffff
)
[2] => Array
(
[0] => gggggggggggg
[1] => hhhhhhhhhhhh
[2] => iiiiiiiiiiii
)
)
fetch array:
foreach ($other[2] as $value) {
echo $value.'<br/>';
}
How do I print all the values of the second array?
You need to nest further more
foreach ($other as $arr)
{
foreach($arr as $k=>$v)
{
if($k==2)
{
echo $v.'<br/>';
}
}
}
OUTPUT :
cccccccccccc
ffffffffffff
iiiiiiiiiiii
Try Something like this,
<?php
$x = array
(
0 => array
(
0 => 'aaaaaaaaaaaa',
1 => 'bbbbbbbbbbbb',
2 => 'cccccccccccc'
),
1 => array
(
0 => 'dddddddddddd',
1 => 'eeeeeeeeeeee',
2 => 'ffffffffffff'
),
2 => array
(
0 => 'gggggggggggg',
1 => 'hhhhhhhhhhhh',
2 => 'iiiiiiiiiiii'
)
);
$count = count($x);
$w = $count - 1;
var_dump($x[$w]);
?>
Related
Hey Dear I am Making Referal System With 11 Level Of Referal So Ho Can I Show All Referals And There Level I Have Tried
Array
(
[L1] => Array
(
[0] => TL422632
[1] => TL626461
)
[L2] => Array
(
[0] => TL4321
[1] => TL191123
)
[L3] => Array
(
[0] => TL555938
)
[L4] => Array
(
[0] => TL197752
)
[L5] => Array
(
[0] => TL835309
)
[L6] => Array
(
[0] => TL495903
)
[L7] => Array
(
[0] => TL207447
)
[L8] => Array
(
[0] => TL427427
)
[L9] => Array
(
[0] => TL288884
)
[L10] => Array
(
[0] => TL251399
)
[L11] => Array
(
[0] => TL284394
)
)
But I Don't Know Ho To Get L Number By Value For Example I Have TL284394 And I Want To Know Level So It Will Show L11 How Can I Do It
you can use two foreach loop inside each other and array_search() like this:
$a = array("L1" => [ 0 => "TL422632" ],"L2"=> [ 0 => "TL422635" ]);
$x = "TL422635";
foreach($a as $key=>$value){
if($x === $value){
return $key;
}else{
foreach($value as $k=>$v){
if($x === $v){
return array_search([$k=>$v],$a);
}
}
}
}
I tried to read from a JSON file using PHP. But I am stuck now.
This is my JSON file :
Array
(
[date] => 25-1-2017
[leagues] => Array
(
[0] => Array
(
[league_id] => 0
[league_name] => كأس امم افريقيا
[league_logo] => http://3.bp.blogspot.com/-4iampWUCLto/VmldL2XTz7I/AAAAAAAAD0I/eZFfzSxRbnE/s60/africa.png
[league_matches] => Array
(
[0] => Array
(
[match_id] => 1
[team1logo] => http://2.bp.blogspot.com/-mycTzHXuzJA/Ugbb-UG_3JI/AAAAAAAAEv0/_mzaSHnRedE/s60/egypt+(4).png
[team2logo] => http://1.bp.blogspot.com/-kYrkU4jahZY/UgbeVpHf8RI/AAAAAAAAFIQ/TBgM5fvVW14/s60/ghana+(3).png
[match_time] => 19:00
[channels_id] => Array
(
[0] => 18
)
[team1] => مصر
[team2] => غانا
)
[1] => Array
(
[match_id] => 2
[team1logo] => http://3.bp.blogspot.com/-_y1MazEn-vg/UgbeoIWUpII/AAAAAAAAFPI/JTRGGpG3GiQ/s60/mali+(4).png
[team2logo] => http://2.bp.blogspot.com/-GTxKONxvKy4/Ugt6AKXOmyI/AAAAAAAAF8Y/pPcM0O58cQI/s60/uganda_2.png
[match_time] => 19:00
[channels_id] => Array
(
[0] => 19
)
[team1] => مالي
[team2] => أوغندا
)
)
)
[1] => Array
(
[league_id] => 1
[league_name] => كأس ملك أسبانيا
[league_logo] => http://1.bp.blogspot.com/-zAzWT2Vpbe0/Vmle9TUWDlI/AAAAAAAAD04/-BKgPzRCT1k/s60/Copa_del_Rey_logo_since_2012.png
[league_matches] => Array
(
[0] => Array
(
[match_id] => 3
[team1logo] => http://4.bp.blogspot.com/-6sfdkbboNdk/VBxVKisRkcI/AAAAAAAADG8/oBKDHmBW5xc/s60/eibar+fc.png
[team2logo] => http://2.bp.blogspot.com/-S7p2yMaLywM/UhCz6GqNCRI/AAAAAAAAGFE/CfxzSQk8bgQ/s60/Atletco+Madrid2.Png
[match_time] => 18:15
[channels_id] => Array
(
[0] => 1
)
[team1] => إيبار
[team2] => أتلتيكو مدريد
)
[1] => Array
(
[match_id] => 4
[team1logo] => http://1.bp.blogspot.com/--6y77FXuPLI/UjeVcpJ-AJI/AAAAAAAAGok/QyZhwahPamo/s60/Celta+Vigo.png
[team2logo] => http://2.bp.blogspot.com/-tncIAL_U6mI/UgbnQw75V8I/AAAAAAAAFhA/8-9Xpw83GKY/s60/Real+Madrid+(3).png
[match_time] => 20:15
[channels_id] => Array
(
[0] => 3
)
[team1] => سيلتا فيغو
[team2] => ريال مدريد
)
)
)
[2] => Array
(
[league_id] => 2
[league_name] => كأس الرابطة الإنجليزية
[league_logo] => http://4.bp.blogspot.com/-31G65FeskFs/VmlezHMPm_I/AAAAAAAAD0w/9OMjh8AQP-M/s60/TheFA_CapitalOneCup.png
[league_matches] => Array
(
[0] => Array
(
[match_id] => 5
[team1logo] => http://3.bp.blogspot.com/-D0lb4b-qN5U/UgbeDosqjYI/AAAAAAAAFBY/Qg7kvoodvFY/s60/Liverpool+(2).png
[team2logo] => http://2.bp.blogspot.com/-prH3jgmfewQ/Ug8ZgNxPlcI/AAAAAAAAGAU/HLmbFvGuDB8/s60/Southampton.png
[match_time] => 20:00
[channels_id] => Array
(
[0] => 2
)
[team1] => ليفربول
[team2] => ساوثهامبتون
)
)
)
[3] => Array
(
[league_id] => 3
[league_name] => كاس ايطاليا
[league_logo] => http://2.bp.blogspot.com/-p9Kjb2_GZPU/VnAdQcHV9_I/AAAAAAAAD7E/6R-P54Upui4/s60/tim-cup.png
[league_matches] => Array
(
[0] => Array
(
[match_id] => 6
[team1logo] => http://1.bp.blogspot.com/-POxAfSSnlW0/UhCt7oQsdLI/AAAAAAAAGDo/rMRXx2mqvUI/s60/Juventus[1].Png
[team2logo] => http://3.bp.blogspot.com/-_Qj_GaxVaDE/UgaEPtt7EsI/AAAAAAAAEA8/redsOTj7F4Q/s60/Ac+Milan+(3).png
[match_time] => 20:00
[channels_id] => Array
(
[0] => 103
)
[team1] => يوفنتوس
[team2] => ميلان
)
)
)
)
)
And this is my PHP so far:
$str = file_get_contents($url);
$json = json_decode($str, true);
$date = $json['date'];
$leagues = $json['leagues'][0];
foreach ($leagues as $key => $value) {
for ($i=0; $i<count($value); $i++) {
foreach ($value[$i] as $key1 => $value1) {
$sql = "INSERT INTO table_name (column1,column2,column3,column4) VALUES (...,...,...,...)";
for ($c=0; $c<count($value1); $c++) {
foreach ($value1[$c] as $key2 => $value2) {
echo $value2;
}
}
}
}
}
the problem how can read all each array by foreach and store every value to table in database.
I would appreciate an example for this.
Basic structure to loop all nodes in your json
//leagues level
foreach ($json['leagues'] as $key => $league) {
$league_id = $league['league_id'];
//leagues -> league_matches level
foreach ($league['league_matches'] as $key => $match) {
$match_id = $match['match_id'];
//channel ids
foreach ($match['channels_id'] as $channels_id) {
//$channels_id;
//create an sql
$sql = "INSERT INTO xy (col1,col2,col3) values ($league_id,$match_id,$channels_id)";
$db->query($sql);
}
}
}
About the SQL You dont show any structure of your databse, so i cant help creating the sql.
I hope your JSON file sends the value using json_encode() method. You need to separate the array elements with a comma (,). Also you need to write the array indexes within quotes ('). Here is a corrected sample of your JSON page.
$retArr = Array
(
['date'] => 25-1-2017,
['leagues'] => Array
(
[0] => Array
(
['league_id'] => 0,
['league_name'] => كأس امم افريقيا,
['league_logo'] => http://3.bp.blogspot.com/-4iampWUCLto/VmldL2XTz7I/AAAAAAAAD0I/eZFfzSxRbnE/s60/africa.png,
['league_matches'] => Array
(
[0] => Array
(
['match_id'] => 1,
['team1logo'] => http://2.bp.blogspot.com/-mycTzHXuzJA/Ugbb-UG_3JI/AAAAAAAAEv0/_mzaSHnRedE/s60/egypt+(4).png,
['team2logo'] => http://1.bp.blogspot.com/-kYrkU4jahZY/UgbeVpHf8RI/AAAAAAAAFIQ/TBgM5fvVW14/s60/ghana+(3).png,
['match_time'] => 19:00
['channels_id'] => Array
(
[0] => 18
)
['team1'] => مصر ,
['team2'] => غانا
)
)
)
)
);
echo json_encode($retArr);
On the receiving page, you need to set a second parameter to TRUE in the json_decode() method so that you will get the result as an array rather than as a JSON Object.
$str = file_get_contents($url);
$json = json_decode($str, true);
$date = $json['date'];
$leagues = $json['leagues'][0];
foreach ($leagues as $key => $value) {
for ($i=0; $i<count($value); $i++) {
foreach ($value[$i] as $key1 => $value1) {
$sql = "INSERT INTO table_name (column1,column2,column3,column4) VALUES (...,...,...,...)";
for ($c=0; $c<count($value1); $c++) {
foreach ($value1[$c] as $key2 => $value2) {
echo $value2;
}
}
}
}
}
Try this way.
I have build this array structure from dig query data.
[10] => Array
(
[id] => 150
[0] => 200.201.202.23
[1] => dns.name1.com
[2] => 200.201.202.24
[3] => dns.name2.com
[4] => 200.201.202.25
[5] => dns.name3.com
) `
I need something like:
[10] => Array
(
[0] => array ( [0] => 200.201.202.23
[1] => dns.name1.com
[id] => 150
)
[1] => array ( [0] => 200.201.202.24
[1] => dns.name2.com
[id] => 150
)
[2] => array ( [0] => 200.201.202.25
[1] => dns.name3.com
[id] => 150
)
) `
I'm not sure if this is possible?
Heres the code where i create the array:
At the first time from the dig i use array_push() to add content to it.
$temp = array();
$i = 0;
foreach ($digResult as $single){
if (preg_match('/(?:^|\s+)(\d+)(?:\s+|\n+|$)/', $single )){
$temp []["id"]= $single;
$i++;
}else {
$temp[$i][] = $single;
}
}
This will work for you :
<?php
$dataArray = array(10 => array
(
'id' => 150 ,
0 => '200.201.202.23' ,
1 => 'dns.name1.com',
2 => '200.201.202.24',
3 => 'dns.name2.com',
4 => '200.201.202.25',
5 => 'dns.name3.com',
)
);
$newArray = array();
$id = $dataArray[10]['id'];
for($i=0; $i< 6; $i++)
{
$newArray[10][] = array(0=>$dataArray[10][$i],1=>$dataArray[10][$i+1],'id'=> $id);
$i+=1;
}
print_r($newArray);
?>
This will output
Array
(
[10] => Array
(
[0] => Array
(
[0] => 200.201.202.23
[1] => dns.name1.com
[id] => 150
)
[1] => Array
(
[0] => 200.201.202.24
[1] => dns.name2.com
[id] => 150
)
[2] => Array
(
[0] => 200.201.202.25
[1] => dns.name3.com
[id] => 150
)
)
)
LIVE EXAMPLE : CLICK HERE
Try this:-
<?php
$array = array(
'id' => '150',
'0' => '200.201.202.23',
'1' => 'dns.name1.com',
'2' => '200.201.202.24',
'3' => 'dns.name2.com',
'4' => '200.201.202.25',
'5' => 'dns.name3.com'
);
$i = 0;
$arrayLenght = (count($array)-2);
$newArray = array();
while ($i <= $arrayLenght) {
$newArray[] = array(
"0" => $array[$i++],
"1" => $array[$i++],
"id" => $array['id']
);
}
echo '<pre>';
print_r($newArray);
echo '</pre>';
?>
Output:-
Array
(
[0] => Array
(
[0] => 200.201.202.23
[1] => dns.name1.com
[id] => 150
)
[1] => Array
(
[0] => 200.201.202.24
[1] => dns.name2.com
[id] => 150
)
[2] => Array
(
[0] => 200.201.202.25
[1] => dns.name3.com
[id] => 150
)
)
I have one array which I am getting from database query response.
Now I want to count same categories and print in option_name array.
I have tried it with different array functions but want get desire output.
I have one idea to take new array and push it with foreach loop but not much idea of how can i achieve using code. Please help me to solve it.
Array
(
[0] => Array
(
[CNC] => Array
(
[id] => 5
[category_id] => 68
)
[GVO] => Array
(
[option_name] => Actors
)
)
[1] => Array
(
[CNC] => Array
(
[id] => 11
[category_id] => 72
)
[GVO] => Array
(
[option_name] => Cricketers
)
)
[2] => Array
(
[CNC] => Array
(
[id] => 3
[category_id] => 72
)
[GVO] => Array
(
[option_name] => Cricketers
)
)
[3] => Array
(
[CNC] => Array
(
[id] => 4
[category_id] => 74
)
[GVO] => Array
(
[option_name] => Musician
)
)
[4] => Array
(
[CNC] => Array
(
[id] => 7
[category_id] => 76
)
[GVO] => Array
(
[option_name] => Directors
)
)
[5] => Array
(
[CNC] => Array
(
[id] => 6
[category_id] => 76
)
[GVO] => Array
(
[option_name] => Directors
)
)
)
Desire Output:
Array
(
[Actors] => 1
[Cricketers] => 2
[Musician] => 1
[Directors] => 2
)
Thanks in advance!
You simply need to loop through the array using foreach like as
$result = [];
foreach($arr as $k => $v){
if(isset($result[$v['GVO']['option_name']])){
$result[$v['GVO']['option_name']] += 1;
}else{
$result[$v['GVO']['option_name']] = 1;
}
}
print_R($result);
You can count the option_name values by incrementing a counter in an associative array where the key is the option_name:
$counts = [];
foreach($array as $v) {
if(!isset($counts[$v['GVO']['option_name']])) {
$counts[$v['GVO']['option_name']] = 0;
}
$counts[$v['GVO']['option_name']]++;
}
print_r($counts);
Try this:
$new_arr = array();
foreach(array_column($your_arr, 'option_name') as $value){
if(in_array($value, $new_array)){
$new_array[$value] = $new_array[$value]+1;
}else{
$new_array[$value] = 1;
}
}
Output
Array
(
[Actors] => 1
[Cricketers] => 2
[Musician] => 1
[Directors] => 2
)
I am generating dynamic textbox for save and add more functionality and i have to store that data in database i got the array but i dont know how to put that array in to loop so i can get my data.
Array looks like this based on this prepare loop so i can access every element of array:
Array
(
[0] => Array
(
[0] => Array
(
[0] => Array
(
[prem_type] => 1
)
[1] => Array
(
[phase_name] => a1
)
[2] => Array
(
[counter] => 2
)
[3] => Array
(
[block] => A
)
[4] => Array
(
[block] => B
)
)
)
[1] => Array
(
[0] => Array
(
[0] => Array
(
[prem_type] => 1
)
[1] => Array
(
[phase_name] => a2
)
[2] => Array
(
[counter] => 2
)
[3] => Array
(
[block] => A
)
[4] => Array
(
[block] => B
)
)
)
)
Thanks
try this
$array = //your array
foreach($array as $value){
foreach($value as $value2){
foreach($value2 as $value3){
foreach($value3 as $key3 => $value3){
//$key3 is rem_type, phase_name…
//$value3 is required values
}
}
}
}
To get to the data you can use something like:
foreach ($array AS $row) {
$prem_type = $row[0][0]['prem_type'];
$phase_name = $row[0][1]['phase_name'];
$counter = $row[0][2]['counter'];
$block1 = $row[0][3]['block'];
$block2 = $row[0][4]['block'];
}
An alternative is to restructure the array into something more tidy:
$result = array();
foreach ($array AS $rowId => $row) {
$result[$rowId] = array(
'prem_type' => $row[0][0]['prem_type'],
'phase_name' => $row[0][1]['phase_name'],
'counter' => $row[0][2]['counter'],
'block1' => $row[0][3]['block'],
'block2' => $row[0][4]['block']
);
}
This results in:
Array
(
[0] => Array
(
[prem_type] => 1,
[phase_name] => a1,
[counter] => 2,
[block1] => A,
[block2] => B
)
...
)
Here is the answer:
for($i=0;$i<count($data1);$i++){
for($j=0;$j<count($data1[$i]);$j++){
$prem_type =$data1[$i][$j][0]['prem_type'];
$prem_name= $data1[$i][$j][1]['phase_name'];
$no_of_phase= $data1[$i][$j][2]['counter'];
echo $prem_type." ".$prem_name." ".$no_of_phase."<br>";
for($k=3;$k<count($data1[$i][$j]);$k++){
echo $data1[$i][$j][$k]['unitname']."<br>";
}
}
}