I have read other questions regarding reading arrays in PHP and implemented the suggested solutions but these do not seem to work for me...
My array looks like this:-
{
[0]=> array(6)
{ ["name"]=> string(9) "Test04Feb" [0]=> string(9) "Test04Feb"
["paymentvalue"]=> string(6) "500.00" [1]=> string(6) "500.00"
["transactiondate"]=> string(19) "2020-02-05 13:29:37" [2]=> string(19) "2020-02-05 13:29:37"
}
[1]=> array(6)
{ ["name"]=> string(9) "Test04Feb" [0]=> string(9) "Test04Feb"
["paymentvalue"]=> string(7) "1500.00" [1]=> string(7) "1500.00"
["transactiondate"]=> string(19) "1970-01-01 05:30:00" [2]=> string(19) "1970-01-01 05:30:00"
}
[2]=> array(6)
{ ["name"]=> string(9) "Test04Feb" [0]=> string(9) "Test04Feb"
["paymentvalue"]=> string(5) "90.00" [1]=> string(5) "90.00"
["transactiondate"]=> string(19) "2020-02-05 18:12:18" [2]=> string(19) "2020-02-05 18:12:18"
}
}
And none of these work for me:-
$stmt1->execute([$myname]);
$value = $stmt1->fetchAll();
...
foreach ($stmt1 as $row1) {
echo $row1[0]->name;
echo $row1['0']['transactiondate'];
echo $row1[0]['paymentvalue'];
}
Any help would be much appreciated, thank you.
Using
foreach ($stmt1 as $row1) {
$row1 is each individual record from the database. Therefore, rather than having to use [0] as in your code, you should simply be using...
echo $row1->name;
etc.
To check this for yourself, use something like print_r($row1); in the loop to see what each loop has to work with.
In your case $row1 is an array as indicated in your dump. In foreach should use $value and not $stmt. Then:
foreach ($value as $row1) {
echo $row1['name'];
}
P.S. If you want to get only associative keys, use like this:
$value = $stmt1->fetchAll(PDO::FETCH_ASSOC);
Try this if you are using foreach
foreach ($stmt1 as $row1) {
echo $row1['name'];
}
or you can use
echo $stmt1[0]['name'];
Hope this helps you
<?php
session_start();
$pid = $_GET['pid'];
$ptype = $_GET['ptype'];
$_SESSION = array();
$_SESSION['cart_items'] = array();
if (isset($_GET['add_cart']) && !empty($_GET['add_cart'])) {
// Add new data to Session var
$newdata = array($pid , $ptype, 1 );
array_push($_SESSION['cart_items'], $newdata);
}
echo '<pre>';
var_dump($_SESSION);
echo '</pre>';
?>
array_push replaces the data already in the $_SESSION with $newdata variable in the $_SESSION instead of adding it.
For example:
I enter the url: ?pid=1&ptype=CH-&add_cart=Add+to+Cart
And the array looks like this:
array(1) {
["cart_items"]=>
array(1) {
[0]=>
array(3) {
[0]=>
string(1) "1"
[1]=>
string(3) "CH-"
[2]=>
int(1)
}
}
}
That's great. But when I enter another url like: ?pid=1&ptype=CPU-&add_cart=Add+to+Cart
The array looks like this:
array(1) {
["cart_items"]=>
array(1) {
[0]=>
array(3) {
[0]=>
string(1) "1"
[1]=>
string(4) "CPU-"
[2]=>
int(1)
}
}
}
instead of this:
array(1) {
["cart_items"]=>
array(1) {
[0]=>
array(3) {
[0]=>
string(1) "1"
[1]=>
string(3) "CH-"
[2]=>
int(1)
}
[1]=>
array(3) {
[0]=>
string(1) "1"
[1]=>
string(4) "CPU-"
[2]=>
int(1)
}
}
}
It replaces the data that was already in the Session. I want it to add to it. How do I do so?
Thanks in advance!
change lines 5&6 from
$_SESSION = array();
$_SESSION['cart_items'] = array();
to
// $_SESSION = array();
// $_SESSION['cart_items'] = array();
array_push wasn't clearing your data. those two lines were clearing your session data on every page load.
Im new to json & php and I'm having some issues with json into php string
My json string looks like this
{"status":"OK","cards":
[{"id":100001,"name":"batman","image":11111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T11:37:07Z"},
{"id":100002,"name":"superman","image":111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:30:09Z"},
{"id":100003,"name":"catwoman","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:39:42Z"},
{"id":100004,"name":"bane","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-09-08T12:56:04Z"}
]}
So Far i have created my string
$json_raw = '{"status":"OK","cards": [{"id":100001,"name": .....
Decoded the json
$arr = json_decode($json_raw, TRUE);
I var_dump($arr);
then it returns
array(2) { ["status"]=> string(2) "OK" ["cards"]=> array(4) { [0]=> array(8) { ["id"]=> int(100001) ["name"]=> string(6) "batman" ["image"]=> int(11111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T11:37:07Z" } [1]=> array(8) { ["id"]=> int(100002) ["name"]=> string(8) "superman" ["image"]=> int(111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T12:30:09Z" } [2]=> array(8) { ["id"]=> int(100003) ["name"]=> string(8) "catwoman" ["image"]=> int(1111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T12:39:42Z" } [3]=> array(8) { ["id"]=> int(100004) ["name"]=> string(4) "bane" ["image"]=> int(1111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-09-08T12:56:04Z" } } }
Now all I want to do is be able to use this data
e.g if name = batman then
I know this is a stupid question but I am struggling :(
Thank in Advance
json_decode() with TRUE as second parameter gives you an associative array. You need to access the correct index to do what you want.
To list the complete associative array with nice formatting, you can do:
echo '<pre>', print_r($arr), '</pre>';
Now, to access the name in your array:
$man = $arr['cards'][0]['name'];
To check if it's Batman (yay!):
if( isset($man) && $man == 'batman' ) {
# code ...
}
For getting the name of all similar names:
$man = $json['cards']['0']['name'];
for ($i=0; $i < count($json['cards']); $i++) {
echo $json['cards'][$i]['name']."\n";
}
See it live!
when you got the array
$arr = json_decode($json_raw, TRUE);
then check if cards key exist
if(array_key_exists('cards', $arr)){
foreach($arr['cards'] as $key=>$val){
echo $key; ///name, id..
echo $val; /// batman,...
if($key == 'name' && $val =='batman'){
//-------do your stuff
}
}
}
Try with:
$cards = $arr['cards'];
foreach($cards as $card) {
if($card['name'] == 'batman') echo 'Hello batman!';
}
EDIT:
Ok, so this worked for me using code above, try it yourself if you want:
<?php
$json_raw = '{"status":"OK","cards":
[{"id":100001,"name":"batman","image":11111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T11:37:07Z"},
{"id":100002,"name":"superman","image":111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:30:09Z"},
{"id":100003,"name":"catwoman","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:39:42Z"},
{"id":100004,"name":"bane","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-09-08T12:56:04Z"}
]}';
$arr = json_decode($json_raw, TRUE);
$cards = $arr['cards'];
foreach($cards as $card) {
if($card['name'] == 'batman') echo 'Hello batman!';
}
?>
I need to get the objects information for "label", "name" where value=true in a PHP variable and not were value=false.
How is this done with this JSON array?
If I make a var_dump of the JSON I get this:
array(8) {
[0]=>
object(stdClass)#8 (3) {
["label"]=>
string(4) "Name"
["name"]=>
string(7) "txtName"
["value"]=>
bool(true)
}
[1]=>
object(stdClass)#9 (3) {
["label"]=>
string(6) "E-mail"
["name"]=>
string(8) "txtEmail"
["value"]=>
bool(true)
}
[2]=>
object(stdClass)#10 (3) {
["label"]=>
string(12) "Phone Number"
["name"]=>
string(8) "txtPhone"
["value"]=>
bool(false)
}
[3]=>
object(stdClass)#11 (3) {
["label"]=>
string(19) "Mobile Phone Number"
["name"]=>
string(14) "txtMobilePhone"
["value"]=>
bool(false)
}
}
$arr = array();
$i = 0;
foreach($json as $key => $items) {
if($items->value == true) {
$arr[$i]['label'] = $items->label;
$arr[$i]['name'] = $items->name;
$i++;
}
}
You can decode it as an object or an array, in this example I use an array.
First you want to take the JSON encoded information and decode it into a PHP array, you can use json_decode() for this:
$data = json_decode($thejson,true);
//the Boolean argument is to have the function return an array rather than an object
Then you can loop through it as you would a normal array, and build a new array containing only elements where 'value' matches your needs:
foreach($data as $item) {
if($item['value'] == true) {
$result[] = $item;
}
}
You then have the array
$result
at your disposal.
Simplification of the suggestions proposed by users JohnnyFaldo and som:
$data = json_decode($thejson, true);
$result = array_filter($data, function($row) {
return $row['value'] == true;
});
I have this multidimensional array in PHP:
array(4) {
["took"]=> int(2)
["timed_out"]=> bool(false)
["_shards"]=> array(3) {
["total"]=> int(5)
["successful"]=> int(5)
["failed"]=> int(0)
}
["hits"]=> array(3) {
["total"]=> int(3)
["max_score"]=> float(2.3578677)
["hits"]=> array(1) {
[0]=> array(5) {
["_index"]=> string(13) "telephonebook"
["_type"]=> string(6) "person"
["_id"]=> string(22) "M5vJJZasTGG2L_RbCQZcKA"
["_score"]=> float(2.3578677)
["_source"]=> array(8) {
["Mob"]=> string(19) "XXX"
["Title"]=> string(13) "Analyst"
["Department"]=> string(8) "Analysis"
["Country"]=> string(6) "Sweden"
["Tlf"]=> string(0) ""
["Name"]=> string(16) "XXX"
["Email"]=> string(29) "XX#retro.com"
["thumbnailPhoto"]=> string(0) ""
}
}
}
}
}
The array has several "hits" inside "hits" and I want to loop trough and print out the stuff inside "_source". I have tried a few different approaches but I cant figure out any way to do this. Please help me.
foreach ($array['hits']['hits'][0]['_source'] as $key => $value) {
//do stuff
}
Try this
foreach ($arr['hits']['hits'] as $val)
{
echo $val['_source']['Mob'];
}
like this
I think this may handle it for you. Replace $the_array_you_provided with your "main" array variable (you've not specified it in the post).
$hits = $the_array_you_provided['hits']['hits'];
foreach ($hits as $hit) {
echo $hit['_source']['Title'];
//print everything in the array
//print_r($hit['_source']);
}
Any help feel free to ask.
Try this:
foreach ($array['hits']['hits'] as $hit) {
foreach ($hit['_source'] as $source) {
echo $source, '<br>';
}
}