Here is the var_export of $myArray:
Array:array ( '#attributes' =>
array ( 'success' => 'true', ), 'Client' => '1218421234', )
What is the code to get the value of Client into a string?? I tried several constructs but must be having a 'senior' day...
Obviously,
$client = $myArray->Client;
...did not work, neither did...
$client = $myArray->attributes()->Client;
... any help?
It's an array, so you should access to it as:
$myArray['Client']
As $myArray->Client would be if $myArray was an object and Client a property of that object and the same goes for #attributes which is just another array with the key 'success'.
The result you have shown indicates that 'Client' is a String inside an array, so it's $myArray['Client'].
Both your first and second try assumes that $myArray is not an array.
Related
Example
print_r($a)
Array ( [Status] => 100 [RefID] => 12345678 [ExtraDetail] => {"Transaction":{"CardPanHash":"0866A6EAEA5CB08B3AE61837EFE7","CardPanMask":"999999******9999"}} )
i need to take CardPanMask value
An example: I wrote this code but it didn't work
$cardnumber=$a[ExtraDetail]->Transaction->CardPanMask;
the $cardnumber must be 999999******9999
but when i echo $cardnumber; but its empty return noting
Your ExtraDetail key is actually a JSON object, which you can't parse with PHP easily without decoding it.
Your $cardnumber variable should be declared as:
$cardnumber = json_decode($a['ExtraDetail'])->Transaction->CardPanMask;
Or:
$cardnumber = json_decode($a['ExtraDetail'], true)['Transaction']['CardPanMask'];
If you plan on needing multiple values from the $a['ExtraDetail'] key, you may consider decoding the entire value into it's own value first.
//you can use `true` as the second parameter of `json_decode()` if you want it to decode as an array instead of an object.
$transaction = json_decode($a['ExtraDetail'])->Transaction;
$cardnumber = $transaction->CardPanMask;
try this:
$a = [
'Status' => 100,
'RefID' => 12345678,
'ExtraDetail' => json_decode ('{"Transaction":{"CardPanHash":"0866A6EAEA5CB08B3AE61837EFE7","CardPanMask":"999999******9999"}}')
];
print_r($a['ExtraDetail']->Transaction->CardPanMask);
I just created a small program to check JSON and JSON_FORCE_OBJECT
$tree = [
0 => array
(
'id' => 1,
'parent' => '0',
'name' => 'b',
'surname' => 'myfolder/b'
),
1 => array
(
'id' => 2,
'parent' => 1,
'name' => 'ignore',
'surname' => 'myfolder/ignore2'
),
2 => array
(
'id' => 3,
'parent' => 1,
'name' => 'ignore2',
'surname' => 'myfolder/ignore4'
)
];
var_dump($tree);
$try = json_encode($tree);//To print with key. Also if we decode we get result as object
//echo $try;
echo '<br />';
$try2 = json_decode($try,JSON_FORCE_OBJECT);
var_dump($try2);
$try2 is exactly equal to $tree an associative array.
Whereas if I remove JSON_FORCE_OBJECT from this line
$try2 = json_decode($try,JSON_FORCE_OBJECT);
I get an array with child object. Though JSON_FORCE_OBJECT is supposed to be used with json_encode but using it with json_decode, I get a surprising result. I am unable to understand whats going on inside?? I thought when I encode it and decode it I should get same result. But I got the same result only when I used JSON_FORCE_OBJECT. Can anyone please help why this happens?
According to the manual: http://php.net/manual/en/function.json-decode.php
json_decode returns an array of objects if you want to convert them in assoc array you should specify the second param which is a boolean
JSON_FORCE_OBJECT is an int with value 16...
when this is passed as second param php cast/converts it to its bool equivalent which true.
To test the above stated behavior try:
var_dump((bool)1; (bool)2, (bool)16)
//output bool(true) bool(true) bool(true) .
var_dump((bool)0)
//outputs bool(false)
So it's nothing to do with JSON_FORCE_OBJECT...even
json_decode($try,true);
json_decode($try,2);
json_decode($try,3);
json_decode($try,4);
json_decode($try,'someVar');
....
//should return your expected result (assoc array)
Similarly if you pass 0 as second param PHP will cast it into bool (which is false) and returns you an object
json_decode($try,0);
json_decode($try,false);
json_decode($try,null)
json_decode($try)
...
//will return objects
The second parameter to json_decode is a boolean. It accepts true or false. It does not accept JSON_FORCE_OBJECT. You're trying to use the wrong constant for the wrong function.
json_decode's 4th parameter accepts a bitmask of constants, but currently it only supports JSON_BIGINT_AS_STRING.
If you want to return stdClass instances for JSON objects from json_decode, set its second parameter to false (the default). Setting it to any non-falsey value makes it return associative arrays instead of objects. Setting it to JSON_FORCE_OBJECT counts as "not-falsey".
It's all described in the manual: http://php.net/json_decode
I have a PHP application with a function that was built to expect information from an API call. However, I'm trying to use this function by passing in information that mimics the API data.
I struggle a bit with arrays and this seems to be an object within an array.
I can access the array that the api provides, so when I use the following code ($triggers is the array the api call returns):
print("<pre>".print_r($triggers,true)."</pre>");
I get the following output:
Array
(
[0] => stdClass Object
(
[triggerid] => 18186
[status] => 0
[value] => 0
)
This is the beginning of the function:
function iterate_triggers($triggers){
$trigger_id_values = array();
foreach($triggers as $trigger) {
//Necessary to show human readable status messages.
$check_status = array(0=>"Up", 1=>"Down", 2=>"Degraded", 3=>"Maintenance");
array_push ($trigger_id_values, [$trigger->triggerid, $trigger->value]);
So if I wanted to pass this function a [triggerid] => 18186 and [value] => 1 how would i do that?
Currently I'm trying:
iterate_triggers(array(0 => array("triggerid" => 18186,"status" => 0,"value" => 1,)));
but this gives me a "Trying to get property of non-object" error. Please be kind to me, I've done my best to research and structure this on my own to no avail.
The easiest way is to cast the assoc array just to an object
In your case this would be
iterate_triggers(array(0 => (object)array("triggerid" => 18186,"status" => 0,"value" => 1,)));
You are currently creating and passing an array that contains an array, while your function expects an array of objects.
You should create your object beforehand, then construct your parameter array, and pass it to your function.
$obj = new \stdClass();
$obj->triggerid = 18186;
$obj->status = 0;
$obj->value = 1;
$arr = array($obj);
iterate_triggers($arr);
This comment on php.net, and the rest of that object documentation, may be useful to you.
By far the easiest and correct way to instantiate an empty generic php object that you can then modify for whatever purpose you choose: <?php $genericObject = new stdClass(); ?>
The error message you are seeing with your own code is caused by $trigger->triggerid inside the function, when $trigger is an array instead of an object as the function expects. Object properties are accessed using $someObject->propertyName notation, while array elements are accessed using $someArray['keyName']
When I using ajax post data with struct
And I get data from code here:
$_POST["post"]; => result is 979, that's OK
$_POST["href[href]"]; => result is 0, How to fix it?
Bracket notation is used to create an array entry. Use this instead:
$_POST["href"]["href"];
Calling $_POST["href"] will return an associative array:
array(
'commentID' => 297980913637729,
'href' => 'http://dongcam.vn/t3927'
);
It's a multidimensional array, so:
$_POST["href"]["href"];
I'm looking for a function to dump a multi-dimension array so that the output is valid php code.
Suppose I have the following array:
$person = array();
$person['first'] = 'Joe';
$person['last'] = 'Smith';
$person['siblings'] = array('Jane' => 'sister', 'Dan' => 'brother', 'Paul' => 'brother');
Now I want to dump the $person variable so the the dump string output, if parsed, will be valid php code that redefines the $person variable.
So doing something like:
dump_as_php($person);
Will output:
$person = array(
'first' => 'Joe',
'last' => 'Smith',
'siblings' => array(
'Jane' => 'sister',
'Dan' => 'brother',
'Paul' => 'brother'
)
);
var_export()
var_export() gets structured
information about the given variable.
It is similar to var_dump() with one
exception: the returned representation
is valid PHP code.
serialize and unserialize
This is useful for storing or passing PHP values around without losing their type and structure. In contrast to var_export this will handle circular references as well in case you want to dump large objects graphs.
The output will not be PHP code though.