php echo from array_chunk as string - php

I want to echo an array_chunk as string, how do I do that ?
here is the code
$rt = $this->db->query("SELECT id_reg_pd FROM 043104_kuliahmhs_20152_2a0dc380_temp");
$_datao = array_chunk($rt->result(), 3);
foreach($_datao as $batman => $robin) {
print_r($robin);
}
I want echo id_reg_pd as string.
I have tried tried :
echo $robin->id_reg_pd;
but get php error like this
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Here the array from print_r($robin);
Array
(
[0] => stdClass Object
(
[id_reg_pd] => 001be76b-4e58-4cea-96cf-fee2d8e0abdc
)
[1] => stdClass Object
(
[id_reg_pd] => 001d4fe5-73f5-4bae-b126-1f787ea0104e
)
[2] => stdClass Object
(
[id_reg_pd] => 002ab28b-e0b9-464a-89fb-12552512a5d0
)
)

Loop over $robin and then check
foreach($robin as $value)
{
echo $value->id_reg_pd;
}

try like this
for($i=0;$i<count($_datao);$i++){
$newarr = (array) $robin[$i];
echo $newarr['id_reg_pd'];
}

Sahil is incorrect. It is not true that you must use a for / foreach loop to achieve your desired result. array_column() works on an array of objects. If you can use a simple implode() call to convert your array to a string, then here is a simple one-liner:
Code (Demo):
$robin=[
(object)['id_reg_pd'=>'001be76b-4e58-4cea-96cf-fee2d8e0abdc'],
(object)['id_reg_pd'=>'001d4fe5-73f5-4bae-b126-1f787ea0104e'],
(object)['id_reg_pd'=>'002ab28b-e0b9-464a-89fb-12552512a5d0']
];
//print_r($robin); // uncomment to see for yourself
//var_export(array_column($robin,'id_reg_pd')); // uncomment to see for yourself
echo implode(', ',array_column($robin,'id_reg_pd')); // implode with whatever glue you wish
Output:
001be76b-4e58-4cea-96cf-fee2d8e0abdc, 001d4fe5-73f5-4bae-b126-1f787ea0104e, 002ab28b-e0b9-464a-89fb-12552512a5d0

Related

Codeigniter : Trying to get property of non-object

i am trying to iterate a result set using foreach loop. Below is my array when i do print_r on result set
Array
(
[0] => stdClass Object
(
[xml] =>
[qid] => 406
[title] => Q by Sikander
[description] => test question created by Sikander
)
)
Using following foreach loop
foreach ($xmls_nodes as $value){
echo $value->qid ;
echo $value->xml ;
echo $value->title ;
echo $value->description ;
}
now as a result it prints following
406 Q by Sikander test question created by Sikander
but right after this it also prints following for each of the record and row
A PHP Error was encountered Severity: Notice
Message: Trying to get property 'qid' of non-object
it give same line no in for error where i am printing these values and using die statement after this, please help me to get rid of this issue
You have to convert this object to array and then just use it as array:
$arr = json_decode(json_encode($xmls_nodes), true);
You have to do JSON-encode your object and then decode it back to an array
[0] => stdClass Object
$arr = json_decode(json_encode($xmls_nodes), true);
then loop through it

Accessing object from array returns undefined

Below is the print output from $_POST['image']
stdClass Object
(
[0] => Array
(
['filename'] => cn-100x100.png
['contents'] =>
)
[1] => Array
(
['filename'] =>
['contents'] =>
)
)
when I do,
echo '<pre>';
print_r((object)$_POST['image'][0]['filename']);
exit;
it gives me an error
Notice: Undefined index: filename
Update
I tried to do var_dump( (array)$_POST['image']),
array(2) { ["'filename'"]=> string(14) "cn-100x100.png"
["'contents'"]=> string(10218) "base64..."}
If I removed the base64 data from the array column ['contents'], now I can access the first array.
you can update it
echo '<pre>';
print_r((array)$_POST['image'][0]['filename']);
exit;
Object properties cannot be accessed by using $object['property_name']
You have to use the arrow syntax $object->property_name or $object->{'property_name'}
In this case it looks like somehow $_POST['image'] has been defined as an object, so you would have to use:
$_POST['image']->{'0'}['filename']
You could also convert it to an array by using: $_POST['image'] = (array)$_POST['image'];
Convert all the object into the array by following way
$all_images= (array) $_POST['image'];
echo '<pre>';
print_r($all_images);
echo '</pre>';
Now, you can access like this $all_images[0]['filename']

get array value from std object in php

I have an array like below. I want to extract the values . Help me out please. But this doesn't print anything. Please help me.Any help would be appreciated.May you all find this question similar.But I am unable to find any answer,because that's the way we do to find the array values.
Array
(
[0] => stdClass Object
(
[bHeader] => stdClass Object
(
[ei] => NSE
[seg] => I
)
[cNetChangeIndicator] =>
[fClosingIndex] => 10558.5
[fHighIndexValue] => 10532
[fIndexValue] => 10469
[fLowIndexValue] => 10438.5
[fOpeningIndex] => 10499.5
[fPercentChange] => -0.85
[sIndexName] => 962450
[fChange] => -89.5
[iIdxId] => 311
)
)
Thanks in advance
convert your object in to array using
$array = (array) $yourObject;
if you use json_decode than give second parameter true e.g
$array = json_decode($jsonStr,TRUE);
It will return array so no need to typecast(conveting) obj to array
also used operator '->' which help to fetch data from object
You are accessing the object in the array as if it is also an array.
You need to access the object's properties using ->
echo $arr[0]->fIndexValue;
echo $arr[0]->fChange;
echo $arr[0]->fPercentChange';
For example:
$obj = new stdClass;
$obj->fIndexValue = 10469;
$arr = array();
$arr[0] = $obj;
echo $arr[0]->fIndexValue;
Prints "10469".
Try this to print the whole thing, assuming your var is $arr:
print_r($arr);
Or for variables
print($arr[0]-->fIndexValue);

Array to string conversion error when parsing xml data to php

I m new in PHP.When I try to store variable in array i got this error ** Array to string conversion **
PHP Code is :
$record = simplexml_load_file('demo.xml');
foreach ($record as $item):
$a=$item->item2->record->p21;
$b=$item->item2->record->bq_21;
echo $arr1 = array($a,$b);
endforeach;
I wants value only ....If print_r is used then its gives this o/p
Array ( [0] => SimpleXMLElement Object ( [0] => 26 ) [1] => SimpleXMLElement Object ( [0] => 1 ) )
I want this only
26 1
Use print_r() instead of echo
print_r(array($a,$b));
You have to cast simpleXML Object to a string. (string)$a and (string)$b
$record = simplexml_load_file('demo.xml');
foreach ($record as $item):
$a=$item->item2->record->p21;
$b=$item->item2->record->bq_21;
$arr1 = array($a,$b);
print_r($arr1);
endforeach;
var_dump (http://sg3.php.net/var_dump) should be used for debugging data in an object.
Also print_r (http://us2.php.net/print_r) can be used that helps printing arrays.

Adding a new array to a multidimensional array item

I need to add some details to an array without overwriting the old data.
At the moment I have something like this if I print_r($data)
Array
(
[one] => Hello
[two] => Hello World
)
I then have a function that adds some data to the array
foreach ($rubric as $rs){
if($rs['position']==1){$data['one']=$rs;}
if($rs['position']==2){$data['two']=$rs;}
}
This gives me the following if I print_r($data)
Array
(
[one] => Array
(
[id] => 1
)
[two] => Array
(
[id] => 2
)
)
Does anyone have any ideas
What I would like to do is....
foreach ($rubric as $rs){
if($rs['position']==1){$data['one']['details']=$rs;}
if($rs['position']==2){$data['two']['details']=$rs;}
}
With the aim of adding a new subarray called "details" within each array item...
Does that make sense? If I try to run that code however I get the following error
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Could someone tell me what I'm doing wrong?
See, array_push array_unshift.
Array push puts an element at the end. Array unshift adds a number to the beginning of the array.
You can also use the structure
$myArray['nameOfNewElement']=$newElement;
This adds $newElement to the array $myArray;
You can use array_merge.
According to your question, here is the solution :
// sample array
$rubric = array(0=> array("position"=>1),1 => array("position"=>2));
$data = array("one" => "Hello","two" => "hello world");
foreach ($rubric as $rs){
if($rs['position']==1){
$d= array_merge($data,$rs);
}
if($rs['position']==2){
$d= array_merge($data,$rs);
}
}
print_r($d);
Here is the working DEMO : http://codepad.org/rgKiv542
Hope, it'll help you.
When you write $data['one'] = $rs;, you are assigning $rs to overwrite the value "Hello".
Perhaps what you were trying to do is
$data['three'] = $rs['id'];

Categories