PHP array values into key values - php

I have below array
<?php
echo "<pre>";
print_r($row);
?>
Array
(
[0] => Array
(
[contact] => ugin
)
[1] => Array
(
[contact] => gsGR
)
[2] => Array
(
[addtnlcontact] => ghjey
)
)
Array
(
[0] => Array
(
[relation] => uncle
)
[1] => Array
(
[relation] => mrsyjsrky
)
[2] => Array
(
[relation] => cghjrtdj
)
)
If i print echo "<pre>"; print_r($row[0]);
i have below array
Array
(
[contact] => ugin
)
Array
(
[relation] => uncle
)
i need above should be in $row[0] => contact => ugin
relation => uncle
tried below code:
$addcont[] = $this->input->post(sheepItForm1_addtnlcontact);
$this->conditionloop($addcont[0],'contact');
$relation[] = $this->input->post(sheepItForm1_relationship);
$this->conditionloop($relation[0],'relation');
public function conditionloop($val,$name)
{
foreach($val as $key => $res)
{
if(!empty($res)) {
$row[$key][$name] = $res;
//echo "<pre>"; print_r($row[$key]);
}
}
echo "<pre>"; print_r($row[0]);
}
please help to solve this

Related

Foreach loop not display result in Codeigniter View

I'm trying get multiple records of attendance comment by studentIDs, and ClassID but when i print Array
Array ( [0] => Array ( [0] => stdClass Object ( [attendanceID] => 89 [comment] => This is Yusra ) ) [1] => Array ( [0] => stdClass Object ( [attendanceID] => 90 [comment] => this is zara butt ) ) [2] => Array ( [0] => stdClass Object ( [attendanceID] => 91 [comment] => this is ibrahim ) ) [3] => Array ( [0] => stdClass Object ( [attendanceID] => 92 [comment] => comment ) ) [4] => Array ( [0] => stdClass Object ( [attendanceID] => 93 [comment] => ) ) [5] => Array ( [0] => stdClass Object ( [attendanceID] => 94 [comment] => ) ) [6] => Array ( [0] => stdClass Object ( [attendanceID] => 95 [comment] => ) ) [7] => Array ( [0] => stdClass Object ( [attendanceID] => 96 [comment] => ) ) )
but i print in view using foreach loop nothing show.. don't know where i'm doing wrong
Controllor
$studentID = []; // Multiple Student ids
$comments = [];
$this->data['set'] = $id; //class id
$this->data['students'] = $this->student_m->get_order_by_student(array('classesID' => $id, 'schoolyearID' => $schoolyearID));
foreach($this->data['students'] as $key => $val){
$studentID[] = $val->studentID; //
}
foreach ($studentID as $student) {
$comments[] = $this->sattendance_m->get_comment($id,$student);
}
$this->data['comments']= $comments;
print_r($comments);
Model
function get_comment($id,$student) {
$this->db->select('attendanceID,comment');
$this->db->where('classesID', $id);
$this->db->where_in('studentID', $student);
$this->db->order_by($this->_primary_key,"desc");
$this->db->limit(1);
$this->db->from($this->_table_name);
$query=$this->db->get();
return $query->result();
}
View
<?php
if(count($comments)) {
foreach ($comments as $key => $row) {
echo $row->comment;
}
}?>
As you can see output of print_r you have multiple comments per student. So you need to do inner loop again
if(count($comments))
{
foreach ($comments as $key => $row) {
foreach ($row as $value) {
echo $value->comment;
}
}
}
this should be
<?php
$i=0;
if($comments)
{
foreach ($comments as $key => $row) {
echo $key[$i++]->row
}
}else{
echo "null";
}
?>

How can i rearrange a php array without using key names

My array is below i need to arrange like array2 (without use $aa['caption1'] like names directly)
arrray1 is
Array
(
[0] => Array
(
[caption1] => Array
(
[0] => gfdhgfjhg
[1] => dfhfgjghk
)
[caption2] => Array
(
[0] => shgjgh
[1] => dhfgkgjl
)
[banner_image] => Array
(
[0] => assets/images/page_content/img_namT7.jpg
[1] => assets/images/page_content/img_R8mzP.jpg
)
)
[1] => Array
(
[heading] => Array
(
[0] =>
)
[pragraph] => Array
(
[0] =>
)
)
)
arrray2 is(Required format )
Array
(
[0] => Array
(
array('caption1'=>'caption1','caption2'=>'shgjgh','banner_image'=>'assets/images/page_content/img_namT7.jpg'),
array('caption1'=>'dfhfgjghk','caption2'=>'dhfgkgjl','banner_image'=>'page_content/img_R8mzP.jpg')
)
[1] => Array
(
array('heading'=>'','pragraph'=>''),
array('heading'=>'fgh','pragraph'=>'ghgh'),
)
)
please any one help me.
The solution using array_keys, array_map and array_combine functions:
// $arr is your initial array
$result = [];
foreach($arr as $v){
$keys = array_keys($v);
$data = call_user_func_array('array_map', [null] + $v);
$result[] = array_map(function($item) use($keys){
return array_combine($keys, $item);
}, $data);
}
print_r($result);
The output:
Array
(
[0] => Array
(
[0] => Array
(
[caption1] => gfdhgfjhg
[caption2] => shgjgh
[banner_image] => assets/images/page_content/img_namT7.jpg
)
[1] => Array
(
[caption1] => dfhfgjghk
[caption2] => dhfgkgjl
[banner_image] => assets/images/page_content/img_R8mzP.jpg
)
)
[1] => Array
(
[0] => Array
(
[heading] =>
[pragraph] =>
)
)
)

Bulid for loop based on given array

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>";
}
}
}

Get a part from the array

The following code is converting XML to array. I want to get a sting by the value of [name]. So for example: $..[offerid].. should give 8b5f7fd3806a42ccb0ade9f8309c5587
Who can help me?? :)
$xmldata = file_get_contents($XMLURL);
$arr= xml2ary($xmldata);
foreach($arr['m4n']['_c']['data']['_c']['record'] as $result){
echo "<pre>"; print_r($result);
}
The echo result is:
Array
(
[recordHash] => Array
(
[_v] => -652572603
)
[column] => Array
(
[0] => Array
(
[_a] => Array
(
[name] => url
)
[_v] => http://ad.dd.com/ppc/?20910868C187884459&zpar6=DF_1&ULP=[[http%3A%2F%2F]]
)
[1] => Array
(
[_a] => Array
(
[name] => title
)
[_v] => This is the title
)
[2] => Array
(
[_a] => Array
(
[name] => description
)
[_v] => Aanbod
)
[3] => Array
(
[_a] => Array
(
[name] => offerid
)
[_v] => 8b5f7fd3806a42ccb0ade9f8309c5587
)
)
)
Try looping through all of the items using a php foreach loop.
$name_to_find = 'offerid';
$value = FALSE;
foreach ($result['column'] as $item) {
if ($item['_a']['name'] == $name_to_find) {
$value = $item['_v'];
break;
}
}
// Now $value has the value of the entry with the name $name_to_find
If you end up finding an entry with the given $name_to_find, then $value will not be FALSE (granted that, you don't have any FALSE values :D).

Nested foreach to echo a key from sub-array?

I'm trying to echo the value stored in key = 'Tipo' from each sub-array.
foreach ($palabras as $key => $palabra) {
foreach ($palabra as $key2 => $meaning) {
echo $palabra[$key2]['Tipo'];
}
}
I'm sure my error is pretty simple, but I'm not sure what it is. The problem is that I get random characters instead of just the values.
Just so you know this is the original array:
Array
(
[0] => Array
(
[Palabra] => juan
)
[1] => Array
(
[Palabra] => es
[0] => Array
(
[Raiz] => ser
[Tipo] => verbo1
)
[1] => Array
(
[Raiz] => ser
[Tipo] => verbo2
)
)
[2] => Array
(
[Palabra] => un
[0] => Array
(
[Tipo] => articulo-indeterminado
)
)
[3] => Array
(
[Palabra] => adjetivo
[0] => Array
(
[Tipo] => Clase
[Clase] => Adjetivo
)
)
)
And this is what I get with my code:
jeverbo1verbo2uarticulo-indeterminadoaClase
<?php
$palabras[0]['palabra'] ='juan';
$palabras[1]['palabra'] ='es';
$palabras[1][0]['tipo']= 'verbo1';
$palabras[1][1]['tipo']= 'verbo2';
$palabras[2]['palabra'] ='blah';
$palabras[2][0]['tipo']= 'verb03';
$palabras[3]['palabra'] ='wha';
$palabras[3][0]['tipo']= 'adjectivo';
$palabras[3][1]['tipo']= 'verbo4';
print_r($palabras);
foreach ($palabras as $palabra) {
foreach ($palabra as $key=>$parto) {
if (is_numeric($key)) {
echo $parto['tipo'] . "<br>";
}
}
}
http://www.ideone.com/cXVAe

Categories