how i can get data query cake php in array var_dump? - php

i have a query in cake php
$sql ="select menu from ms_menu";
$result=advancedQuery($sql);
foreach ($result as $data ){
echo $data[0];
}
the case is :
the $data[0] show nothing ...
i try to
var_dump $result;
and the result is
output
array(1) { [0]=> array(1) { [0]=> array(1) { ["NAMA_MENU"]=> string(6) "Report" } } }
i need to get "Report" to my variabel..
anyone knows the problem ?? please help

First of all, var_dump(); is a function and you should use it like this : var_dump($result);
Here is your information hierarchy :
- $data
-- $data[0]
--- $data[0][0]
---- $data[0][0]['NAMA_MENU']
Here, you are trying to echo an array ($data[0]). It's not possible.
You can :
— Create a double recursive foreach :
foreach ($result as $data ){
foreach ($data[0] as $innerData ){
echo $innerData['NAMA_MENU'];
}
}
— Get directly your needed value inside the first foreach :
foreach ($result as $data ){
echo $data[0][0]['NAMA_MENU'];
}

Related

PHP how to loop over nested JSON Object?

1. Extracted from my laravel controller:
..
..
$data = json_decode($response, true);
return $data;
..
..
return view('homepage')->with('homeExclusives', $homeExclusives);
Here is a sample of the returned data, just a short version, since the returned feed is very large, but this will give you an idea of the way it's structured.
array(4) {
["success"]=> bool(true)
["status"]=> int(200)
["bundle"]=> array(2) {
[0]=> array(631) {
["StreetDirPrefix"]=> string(2) "SW"
["DistanceToStreetComments"]=> NULL
}
[1]=> array(631) {
["StreetDirPrefix"]=> string(2) "NE"
["DistanceToStreetComments"]=> NULL
}
}
I need to extract "StreetDirPrefix" value from [0] and [1], but I always get an error. Can someone help?
For the data in your example you might use array_column and specify StreetDirPrefix as the column key.
$res = array_column($array["bundle"], "StreetDirPrefix");
print_r($res);
Php demo
Without knowing what error you are getting my solution would be something like this:
<?php
if (is_array($data) && is_array($data["bundle"]) ) {
foreach ($data["bundle"] as $tmpKey => $tmpVal) {
if (isset($tmpVal["StreetDirPrefix"])) {
echo $tmpKey." => ".$tmpVal["StreetDirPrefix"]."\n";
}
}
}
?>
I always like to validate arrays, so if your $data variable or the $data["bundle"] subsection are not arrays then you will not get anything. Not even an error.
I have a working example here:
https://www.seeque-secure.dk/demo.php?id=PHP+how+to+loop+over+nested+JSON+Object
EDIT:
(if i understand you correct)
When you have validated your array all you have to do is repeat the inner validation like this:
<?php
if (is_array($data) && is_array($data["bundle"]) ) {
foreach ($data["bundle"] as $tmpKey => $tmpVal) {
if (isset($tmpVal["StreetDirPrefix"])) {
echo $tmpKey." => ".$tmpVal["StreetDirPrefix"]."\n";
}
if (isset($tmpVal["UnparsedAddress"])) {
echo $tmpVal["UnparsedAddress"]."\n";
}
if (isset($tmpVal["SalePrice"])) {
echo $tmpVal["SalePrice"]."\n";
}
//...... ect.....
}
}
?>

How can I define the last element in a foreach loop?

This is my for each loop
foreach ($row as $key => $value) {
echo $key;
}
The result is:
one
four
end
three
two
I want now that the $key end is always at the end. Is this possible?
Something like
foreach ($row as $key => $value) {
if($key == "end"){
echo $key as last;
} else {
echo $key;
}
}
So that the result is
one
four
three
two
end
As mentined in my comment, simply add the static value to the end of your array
array_push($row, "end");
If you want you can create a custom function , pass your array to a function. Inside that function you could filter the data the way you want .I have created formatRow() function , it just remove the key that you want at last from the array , and insert it again , as the new value are always inserted at end of array , you get the desired output
$row = array(
'one'=>"data",
'four'=>"data",
'end'=>"enddata",
'three'=>"data",
'two'=>"data",
);
$formatted_row = formatRow($row,'end');
echo "<pre>";
var_dump($formatted_row);
//output
// array(5) {
// ["one"]=>
// string(4) "data"
// ["four"]=>
// string(4) "data"
// ["three"]=>
// string(4) "data"
// ["two"]=>
// string(4) "data"
// ["end"]=>
// string(7) "enddata"
// }
function formatRow ($row,$key_that_you_need_last) {
if (array_key_exists($key_that_you_need_last,$row)) {
$value = $row["$key_that_you_need_last"];
unset($row["$key_that_you_need_last"]);
$row["$key_that_you_need_last"] = $value;
}
return $row;
}
If you want to achieve more ,these are the links that you might wanna go through.
array_map
array_walk

PHP Trying to get property of non-object for valid object

I have the following problem:
I'm iterating through an array of valid objects using foreach. When trying to access the resulting objects or their properties I am getting the notice I would be trying to access a non-object.
Here is the code:
$schema = json_decode($_POST['d']);
foreach ($schema->node as $node) {
var_dump($node);
if ($node->status == 1) {
$data = $node->id;
}
}
var_dump outputs the following:
object(stdClass)#5 (6) {
["status"]=>
int(0)
["id"]=>
int(1)
["title"]=>
string(6) "Sensor"
["script"]=>
string(24) "from eZness import swag;"
["x"]=>
int(60)
["y"]=>
int(80)
}
Thanks in advance.
UPDATE:
$schema = json_decode($_POST['d']);
foreach ($schema->node as $node) {
var_dump($node);
echo $node->status; //Funnily this works
$status = $node->status; //while this doesn't
if ($node->status == 1) { //and this doesn't as well
$data = $node->id;
}
}
But when removing the var_dump even the echo doesn't work anymore.
UPDATE:
Resolved. Had a look at the client part of the application, there was a problem with pushing NULL values in the $schema->node array which of course are non-objects.
You are trying to access $node->data, which does not exist.
Perhaps more of a workaround than an answer but: use
$schema = json_decode($_POST['d'],true);
When you pass true as the second parameter, you get back an associative array instead of an object.
You should be able to loop through it with this:
$schema = json_decode($_POST['d'],true);
foreach ($schema['node'] as $node) {
if ($node['status'] == 1) {
$data = $node['id'];
}
}

Unable to iterate on the content of an array

I have an array which contents data that I can see on doing a var_dump() but I am unable to iterate through its content using foreach()
The var_dump() generates the following output
array(4) { [0]=> array(1) { [0]=> string(5) "Admin" } [1]=> array(1) { [0]=> string(4) "rick" } [2]=> array(1) { [0]=> string(6) "techbr" } [3]=> array(1) { [0]=> string(7) "testdom" } }
I want to be able to get the content of this array and store it in another.
Currently I am using the following code
$empList = array();
$empList = emp_list($mysqli);
var_dump($empList);//This generated the above output
foreach ($empList as $value)
{
echo $value."<br>";
}
Output of the echo is this
Array
Array
Array
Array
How do I sort this out?
Thank you for your suggestions I have modified the code this way
$i=0;
$empList = array();
$tempList = array();
$tempList = emp_list($mysqli);
foreach ($tempList as $value)
{
$empList[$i] = $value[0];
$i++;
}
Now the $empList array stores stuff in the correct format
It has an array inside another array so, use two foreach loops
$empList = array();
$empList = emp_list($mysqli);
foreach ($empList as $value)
{
foreach ($value as $temp)
{
echo $temp."<br>";
}
}
As u_mulder says in the comments on your question, your array isn't an array of strings - it's an array of more arrays. var_dump() is designed to deal with complicated nested contents, but echo can't print arrays - that's why it's just telling you that each item in $empList is an array, and not what its contents are.
If you wanted to get the content out of a specific array in $empList you'd need to access it by its index key, with something like:
$first = $empList[0];
foreach ($first as $value) {
echo $value."<br>";
}
Or if you wanted to iterate through them all you could just put two foreach loops one inside the other.

using var_dump,value not returning in proper format

Here is my code:
foreach ($results as $result)
{
$getdata[] = $result->salt;
$getdata[] = $result->password;
}
var_dump($getdata);
echo $sal->$getdata[0];
echo "<br>";
echo $pwd->$getdata[1];
Output:
array(2) { [0]=> string(3) "f9e" [1]=> string(64) "61eed489ddfa309ab764hj876bfhfa5d18e3c3e695edc15" }
But i want the ouput like dis:
[0]=> "f9e" [1]=> "61eed489ddfa309ab764hj876bfhfa5d18e3c3e695edc15"
You can use print_r($getdata) insted of var_dump($getdata)
Please try following code:
foreach ($results as $result) {
$getdata[] = $result->salt;
$getdata[] = $result->password;
}
foreach ($getdata as $key => $value) {
echo '[' . $key . '] => "' . $value . '" ';
}
Before printing the result, use
echo "<pre>";
print_r($result);
This will give you result in a neat understandable way
FYI:
The var_dump function displays structured information about variables/expressions including its type and value
The print_r() displays information about a variable in a way that's readable by humans. array values will be presented in a format that shows keys and elements

Categories